Macro to convert document into PNGs

Creating a macro - Writing a Script - Using the API

Macro to convert document into PNGs

Postby demetalgimp » Thu Apr 06, 2017 4:24 am

(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   Expand viewCollapse view
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
demetalgimp
 
Posts: 3
Joined: Thu Apr 06, 2017 4:02 am

Re: Macro to convert document into PNGs

Postby robleyd » Thu Apr 06, 2017 6:04 am

You might find that Zamzar is an easier option than writing a macro, especially for a one off job.
Cheers
David
OpenOffice 4.1.3 - Slackware 14.2 - 64 bit
User avatar
robleyd
Volunteer
 
Posts: 530
Joined: Mon Aug 19, 2013 3:47 am
Location: Murbko, Australia

Re: Macro to convert document into PNGs

Postby FJCC » Thu Apr 06, 2017 6:17 am

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   Expand viewCollapse view
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
AOO 3.4 or 4.1 on MS Windows XP ( before 2013-08-03) or Windows 7
If your question is answered, please go to your first post, select the Edit button, and add [Solved] to the beginning of the title.
FJCC
Moderator
 
Posts: 5810
Joined: Sat Nov 08, 2008 8:08 pm
Location: Colorado, USA

Re: Macro to convert document into PNGs

Postby JeJe » Thu Apr 06, 2017 10:02 am

If all else fails, there's always the Print Screen key.
Openoffice 4.1.2
Windows 8
JeJe
 
Posts: 96
Joined: Wed Mar 09, 2016 2:40 pm

Re: Macro to convert document into PNGs

Postby ThierryT » Thu Apr 06, 2017 11:11 am

With LO you have the filter : writer_png_Export
LibreOffice 5.3.2.1 x 64 and AOO 4.1.3 Windows x64
ThierryT
 
Posts: 11
Joined: Fri May 13, 2016 8:52 pm

Re: Macro to convert document into PNGs

Postby demetalgimp » Thu Apr 06, 2017 2:43 pm

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

Postby demetalgimp » Thu Apr 06, 2017 2:46 pm

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
demetalgimp
 
Posts: 3
Joined: Thu Apr 06, 2017 4:02 am

Re: Macro to convert document into PNGs

Postby ThierryT » Thu Apr 06, 2017 4:30 pm

Try
Code: Select all   Expand viewCollapse view
    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
ThierryT
 
Posts: 11
Joined: Fri May 13, 2016 8:52 pm

Re: Macro to convert document into PNGs

Postby Sébastien C » Fri Apr 07, 2017 7:24 am

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   Expand viewCollapse view
' ╔══════════════════════════════════════════════════════════════════════════════╗
' ║ 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 10 times
LibreOffice 5.x under GNU-Linux Mageia 5.0 and M$-W XP
User avatar
Sébastien C
 
Posts: 21
Joined: Mon Jan 04, 2010 5:06 pm
Location: Meymac, France

Re: Macro to convert document into PNGs

Postby John_Ha » Fri Apr 07, 2017 12:26 pm

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 12 times
images.odt
.odt file with photo and graphic
(126.51 KiB) Downloaded 8 times
AOO 4.1.3, Windows 7 Home 64 bit

See the OOo v3.3 Writer Manual, the Writer FAQ, the Writer Tutorials and the up to date Writer guide for information. Click the Help button on a pop-up window for extensive help on that function.
John_Ha
Volunteer
 
Posts: 3776
Joined: Fri Sep 18, 2009 5:51 pm
Location: UK


Return to Macros and UNO API

Who is online

Users browsing this forum: No registered users and 6 guests