Macro to convert document into PNGs

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Locked
demetalgimp
Posts: 3
Joined: Thu Apr 06, 2017 4:02 am

Macro to convert document into PNGs

Post by demetalgimp »

(I have tried to find any definitive documentation on the API and have found frustratingly little. I want something that defines:
ThisComponent.*
ThisComponent.CurrentController.*
ThisComponent.CurrentController.Frame
It would help tremendously to use an IDE with auto-completion)

I need to be able to convert a Writer document into PNGs. I wrote a book on Hobby Chemistry and was very careful about layout. I want to convert it into a Kindle format. When I convert it into PDF, the images flatten too far and lose color. When I try to use Amazon's conversion, the graphics fly all over the place. I want to convert it into a series of PNGs which I would include in a Word document that would then be imaged as a whole into an electronic book.

This is what I need:
Convert page by page using the document name and page number as unique filenames into a PNG with 300dpi.

This is what I got so far. I got it by tricking the spreadsheet app into recording my keystrokes into a macro. The writer app would not record.

Code: Select all

sub Main
    dim document   as object
    dim dispatcher as object

    document = ThisComponent.CurrentController.Frame
    dispatcher = createUnoService("com.sun.star.frame.DispatchHelper")

    dim args(3) as new com.sun.star.beans.PropertyValue
    args(0).Name = "URL"
rem... this needs to change to "file:///home/user/${doc_name} pg ${page_num}.png"
    args(0).Value = "file:///home/user/asdfghj.png"
    args(1).Name = "FilterName"
rem... this needs to change to Writer
    args(1).Value = "calc_png_Export"
    args(2).Name = "FilterData"
    DV = com.sun.star.beans.PropertyState.DIRECT_VALUE
    args(2).Value = Array(Array("PixelWidth",0,2550,DV),Array("PixelHeight",0,3300,DV))
    args(3).Name = "SelectionOnly"
    args(3).Value = false
    dispatcher.executeDispatch(document, ".uno:ExportTo", "", 0, args())
end sub
This needs to happen for each page, like a foreach().

DeMetalGimp
LibreOffice 5.1.6.2 on Ubuntu
User avatar
robleyd
Moderator
Posts: 5056
Joined: Mon Aug 19, 2013 3:47 am
Location: Murbko, Australia

Re: Macro to convert document into PNGs

Post by robleyd »

You might find that Zamzar is an easier option than writing a macro, especially for a one off job.
Cheers
David
OS - Slackware 15 64 bit
Apache OpenOffice 4.1.15
LibreOffice 24.2.1.2; SlackBuild for 24.2.1 by Eric Hameleers
FJCC
Moderator
Posts: 9248
Joined: Sat Nov 08, 2008 8:08 pm
Location: Colorado, USA

Re: Macro to convert document into PNGs

Post by FJCC »

To answer the first part of your question, you can find the properties and methods of any object with an inspection tool. There is a tutorial on using MRI here. Another similar tool is called XRay. Those tools are for using the API. Recording macros results in dispatcher calls, which are not documented systematically anywhere that I know of.

As for exporting a Writer document to PNG, I don't see a filter for doing that. I stored this macro which is meant to make a list of all of the available filters

Code: Select all

Sub FilterList
oFF = createUnoService( "com.sun.star.document.FilterFactory" ) 
oFilterNames = oFF.getElementNames() 

' Create a Writer doc and save the filter names to it. 
oDoc = StarDesktop.loadComponentFromURL( "private:factory/swriter", "_blank", 0, Array() ) 
oText = oDoc.getText() 
oCursor = oText.createTextCursor() 
oCursor.gotoEnd( False ) 

' Print the filter names into a Writer document. 
For i = LBound( oFilterNames ) To UBound( oFilterNames ) 
  oText.insertString( oCursor, oFilterNames(i), False ) 
  oText.insertControlCharacter( oCursor, com.sun.star.text.ControlCharacter.PARAGRAPH_BREAK, False ) 
Next 
End Sub
OpenOffice 4.1 on Windows 10 and Linux Mint
If your question is answered, please go to your first post, select the Edit button, and add [Solved] to the beginning of the title.
JeJe
Volunteer
Posts: 2764
Joined: Wed Mar 09, 2016 2:40 pm

Re: Macro to convert document into PNGs

Post by JeJe »

If all else fails, there's always the Print Screen key.
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
ThierryT
Posts: 11
Joined: Fri May 13, 2016 8:52 pm

Re: Macro to convert document into PNGs

Post by ThierryT »

With LO you have the filter : writer_png_Export
LibreOffice 5.3.2.1 x 64 and AOO 4.1.3 Windows x64
demetalgimp
Posts: 3
Joined: Thu Apr 06, 2017 4:02 am

Re: Macro to convert document into PNGs

Post by demetalgimp »

robleyd wrote:You might find that Zamzar is an easier option than writing a macro, especially for a one off job.
I'm trying it now. Thanks!

...

Tried it. It won't work. I cannot control the image size so the page images are unsatisfactory. Pity. It looked very promising.
Last edited by demetalgimp on Thu Apr 06, 2017 3:32 pm, edited 1 time in total.
LibreOffice 5.1.6.2 on Ubuntu
demetalgimp
Posts: 3
Joined: Thu Apr 06, 2017 4:02 am

Re: Macro to convert document into PNGs

Post by demetalgimp »

ThierryT wrote:With LO you have the filter : writer_png_Export
Please tell me about it, or point me to the API description.
LibreOffice 5.1.6.2 on Ubuntu
ThierryT
Posts: 11
Joined: Fri May 13, 2016 8:52 pm

Re: Macro to convert document into PNGs

Post by ThierryT »

Try

Code: Select all

    sub Main
        dim document   as object
        dim dispatcher as object

        document = ThisComponent.CurrentController.Frame
        dispatcher = createUnoService("com.sun.star.frame.DispatchHelper")

        dim args(3) as new com.sun.star.beans.PropertyValue
        args(0).Name = "URL"
    rem... this needs to change to "file:///home/user/${doc_name} pg ${page_num}.png"
        args(0).Value = ConvertToURL("C:\Users\XXX\Documents\WriterPNG.png")
        args(1).Name = "FilterName"
    rem... this needs to change to Writer
        args(1).Value = "writer_png_Export"
        args(2).Name = "FilterData"
        DV = com.sun.star.beans.PropertyState.DIRECT_VALUE
        args(2).Value = Array(Array("PixelWidth",0,2550,DV),Array("PixelHeight",0,3300,DV))
        args(3).Name = "SelectionOnly"
        args(3).Value = false
        dispatcher.executeDispatch(document, ".uno:ExportTo", "", 0, args())
    end sub
With this macro, you can export only one page : the selected one.
You have to develop another macro which select in your file page per page and call this convert macro.
LibreOffice 5.3.2.1 x 64 and AOO 4.1.3 Windows x64
User avatar
Sébastien C
Posts: 111
Joined: Mon Jan 04, 2010 5:06 pm
Location: Meymac, France

Re: Macro to convert document into PNGs

Post by Sébastien C »

If I may, the PixelWidth and PixelHeight do not works pretty well with the dispatcher...
This one make all files in the same folder of the Writer’s source file. It begin from the last to the first page. And you can force the size of the PNG(s)...
:D

Code: Select all

' ╔══════════════════════════════════════════════════════════════════════════════╗
' ║ Export all pages of a Writer document in PNG format.                         ║█
' ╚══════════════════════════════════════════════════════════════════════════════╝█
'  ▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀

Sub convertWriter2PNG()
 Dim   myViewCursor As Object
 Dim     myPageName As String, myString(255) As String, myFile As String, myFolder As String, myName As String
 Dim              i As Integer
 Dim   myPixelWidth As Long, myPixelHeight As Long

 Dim propsFiltre (1) As New com.sun.star.beans.PropertyValue
 Dim props       (1) As New com.sun.star.beans.PropertyValue

 myPageName = "_page_"

 ' Resolution choice (for an A4-Portrait document):
   myPixelWidth =  793 : myPixelHeight = 1122 '  96 DPI.
 ' myPixelWidth = 2481 : myPixelHeight = 3507 ' 300 DPI.
 ' myPixelWidth = 4962 : myPixelHeight = 7014 ' 600 DPI.

 propsFiltre(0).Name = "PixelWidth"  : propsFiltre(0).Value = myPixelWidth
 propsFiltre(1).Name = "PixelHeight" : propsFiltre(1).Value = myPixelHeight
 
 props      (0).Name = "FilterName"  : props      (0).Value = "writer_png_Export"
 props      (1).Name = "FilterData"  : props      (1).Value = propsFiltre()

 myString = split(thisComponent.url, "/") :           i = uBound(myString)
   myFile = myString(i)                   : myString(i) = ""
 myFolder = join (myString()       , "/")

 myString = split(           myFile, ".") :           i = uBound(myString)
                                            myString(i) = ""
   myName = join (myString()       , ".") :      myName = left(myName, len(myName) - 1)

 myViewCursor = thisComponent.CurrentController.ViewCursor
 myViewCursor.jumpToLastPage

 For i = myViewCursor.page to 1 step -1
  thisComponent.storeToURL(myFolder & myName & myPageName & Format(i, "000") & ".png", props())
  myViewCursor.jumpToPreviousPage 
 Next i

 myViewCursor.jumpToFirstPage : msgBox("Done!")
End Sub
Attachments
convertWriter2PNG.odt
(67.63 KiB) Downloaded 233 times
LibreOffice v. 7.3.2.2, under GNU-Linux Mint and, in virtualization and just for tests, LibreOffice v. 7.3.2.2 an OpenOffice v. 4.1.12 under M$-W 10 :ouch: .
John_Ha
Volunteer
Posts: 9583
Joined: Fri Sep 18, 2009 5:51 pm
Location: UK

Re: Macro to convert document into PNGs

Post by John_Ha »

demetalgimp wrote:When I convert it into PDF, the images flatten too far and lose color.
That should not happen and I am wondering if it is a problem with your PDF viewer. Could you possibly upload a few pages of the .odt file and the PDF created from them so I can have a look and compare the PDF images with the .odt originals. You will need to use a fileshare site or Google Drive if either is over 128kB.

Have you tried one of the many PDF Virtual Printers such as PrimoPDF?

Have a look at these two files - the images in the PDF look OK to me.
Attachments
images.pdf
PDF created from .odt file
(90.4 KiB) Downloaded 247 times
images.odt
.odt file with photo and graphic
(126.51 KiB) Downloaded 220 times
LO 6.4.4.2, Windows 10 Home 64 bit

See the Writer Guide, the Writer FAQ, the Writer Tutorials and Writer for students.

Remember: Always save your Writer files as .odt files. - see here for the many reasons why.
laara_12
Posts: 15
Joined: Mon Sep 04, 2023 7:03 am

Re: Macro to convert document into PNGs

Post by laara_12 »

How to convert an odt file into PNG using java.
OpenOffice 4.1.14 on X86_64 Ubuntu
User avatar
robleyd
Moderator
Posts: 5056
Joined: Mon Aug 19, 2013 3:47 am
Location: Murbko, Australia

Re: Macro to convert document into PNGs

Post by robleyd »

One issue, one thread Please start a new topic for a new problem.
Cheers
David
OS - Slackware 15 64 bit
Apache OpenOffice 4.1.15
LibreOffice 24.2.1.2; SlackBuild for 24.2.1 by Eric Hameleers
Locked