Page 1 of 1
Macro to convert document into PNGs
Posted: Thu Apr 06, 2017 4:24 am
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
Re: Macro to convert document into PNGs
Posted: Thu Apr 06, 2017 6:04 am
by robleyd
You might find that
Zamzar is an easier option than writing a macro, especially for a one off job.
Re: Macro to convert document into PNGs
Posted: Thu Apr 06, 2017 6:17 am
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
Re: Macro to convert document into PNGs
Posted: Thu Apr 06, 2017 10:02 am
by JeJe
If all else fails, there's always the Print Screen key.
Re: Macro to convert document into PNGs
Posted: Thu Apr 06, 2017 11:11 am
by ThierryT
With LO you have the filter : writer_png_Export
Re: Macro to convert document into PNGs
Posted: Thu Apr 06, 2017 2:43 pm
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.
Re: Macro to convert document into PNGs
Posted: Thu Apr 06, 2017 2:46 pm
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.
Re: Macro to convert document into PNGs
Posted: Thu Apr 06, 2017 4:30 pm
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.
Re: Macro to convert document into PNGs
Posted: Fri Apr 07, 2017 7:24 am
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)...
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
Re: Macro to convert document into PNGs
Posted: Fri Apr 07, 2017 12:26 pm
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.
Re: Macro to convert document into PNGs
Posted: Mon Sep 11, 2023 2:07 pm
by laara_12
How to convert an odt file into PNG using java.
Re: Macro to convert document into PNGs
Posted: Mon Sep 11, 2023 2:17 pm
by robleyd
One issue, one thread Please start a new topic for a new problem.