Exporting Text File to PDF (One Page At A time)

I'm exporting ODT files to PDF, but I want to export only 1 page at a time, so I'm using a BASIC macro to handle this.

I've been searching, but since somewhere around 3-4am EST, I have not been able to access oooforum.org and most links lead to there.

I can't find examples elsewhere that show me how to export a PDF file from in BASIC or how to specify, again, from BASIC, what pages to export.

Can anyone give me links for examples that are not on oooforum.org? Or tell me what classes I would be using for this so I can look them up in the IDL to find out how to specify the page (or pages) to export and how to use the PDF exporter?

Thank you.

Hal

Hal Vaughan wrote:

I'm exporting ODT files to PDF, but I want to export only 1 page at a time, so I'm using a BASIC macro to handle this.

I've been searching, but since somewhere around 3-4am EST, I have not been able to access oooforum.org and most links lead to there.

I can't find examples elsewhere that show me how to export a PDF file from in BASIC or how to specify, again, from BASIC, what pages to export.

Can anyone give me links for examples that are not on oooforum.org? Or tell me what classes I would be using for this so I can look them up in the IDL to find out how to specify the page (or pages) to export and how to use the PDF exporter?

Thank you.

Hal
  

Hal,
I just checked on my LO 3.6.4 and discovered there is a way to export only certain page(s). Don't use the button on the toolbar, but use File -> Export as PDF.
A dialog comes up with "Range" in the top left corner of the "General" tab. There, you can select "All", "Pages", or "Selection" (if pre-selected). The "Pages" option allows you to specify which range of pages, or a single page number, you want to be included in the export. This dialog has many other PDF options that the toolbar button does not offer.
However, I may have mis-read your problem. If you want to have an automated method of creating a separate file for each page, then this may not help. If your document is long, using this method can be tedious and an automated method would be preferable.

Hope this helps.
Girvin Herr

Hi to all

Hal Vaughan wrote:

I'm exporting ODT files to PDF, but I want to export only 1 page at a
time, so I'm using a BASIC macro to handle this.

I've been searching, but since somewhere around 3-4am EST, I have not
been able to access oooforum.org and most links lead to there.

I can't find examples elsewhere that show me how to export a PDF file
from in BASIC or how to specify, again, from BASIC, what pages to export.

Can anyone give me links for examples that are not on oooforum.org?
Or tell me what classes I would be using for this so I can look them
up in the IDL to find out how to specify the page (or pages) to export
and how to use the PDF exporter?

Thank you.

Hal

Hal,
I just checked on my LO 3.6.4 and discovered there is a way to export
only certain page(s). Don't use the button on the toolbar, but use File
-> Export as PDF.
A dialog comes up with "Range" in the top left corner of the "General"
tab. There, you can select "All", "Pages", or "Selection" (if
pre-selected). The "Pages" option allows you to specify which range of
pages, or a single page number, you want to be included in the export.
This dialog has many other PDF options that the toolbar button does not
offer.
However, I may have mis-read your problem. If you want to have an
automated method of creating a separate file for each page, then this
may not help. If your document is long, using this method can be
tedious and an automated method would be preferable.

Hope this helps.
Girvin Herr

If you need a single pdf for every page of your document it's better to create a single pdf and then separate every page with an external program specialized in this.

Something like below, to yust save page 3 as 'c:\temp\3.pdf'

How did i get this:
1) Tools/Options/LibreOffice/General: enable "Enable experimental (unstable) features" and "Enable macro recording (limited)"
2) Tools/Macros/Record Macro
3) Save page 3 as pdf via File/Export PDF
4) Click 'Stop recording', and save your macro
5) Via Tools/Macros/Orginize Macros, you should be able to find what you yust saved :wink:

REM ***** BASIC *****

Sub Main

End Sub

sub testPDF
rem ----------------------------------------------------------------------
rem define variables
dim document as object
dim dispatcher as object
rem ----------------------------------------------------------------------
rem get access to the document
document = ThisComponent.CurrentController.Frame
dispatcher = createUnoService("com.sun.star.frame.DispatchHelper")

rem ----------------------------------------------------------------------
dim args1(2) as new com.sun.star.beans.PropertyValue
args1(0).Name = "URL"
args1(0).Value = "file:///c:/temp/3.pdf"
args1(1).Name = "FilterName"
args1(1).Value = "writer_pdf_Export"
args1(2).Name = "FilterData"
args1(2).Value = Array(Array("UseLosslessCompression",0,false,com.sun.star.beans.PropertyState.DIRECT_VALUE),Array("Quality",0,90,com.sun.star.beans.PropertyState.DIRECT_VALUE),Array("ReduceImageResolution",0,false,com.sun.star.beans.PropertyState.DIRECT_VALUE),Array("MaxImageResolution",0,300,com.sun.star.beans.PropertyState.DIRECT_VALUE),Array("UseTaggedPDF",0,false,com.sun.star.beans.PropertyState.DIRECT_VALUE),Array("SelectPdfVersion",0,0,com.sun.star.beans.PropertyState.DIRECT_VALUE),Array("ExportNotes",0,false,com.sun.star.beans.PropertyState.DIRECT_VALUE),Array("ExportBookmarks",0,true,com.sun.star.beans.PropertyState.DIRECT_VALUE),Array("OpenBookmarkLevels",0,-1,com.sun.star.beans.PropertyState.DIRECT_VALUE),Array("UseTransitionEffects",0,true,com.sun.star.beans.PropertyState.DIRECT_VALUE),Array("IsSkipEmptyPages",0,true,com.sun.star.beans.PropertyState.DIRECT_VALUE),Array("IsAddStream",0,false,com.sun.star.beans.PropertyState.DIRECT_VALUE),Array("EmbedStandardFonts",0,false,com.sun.star.bea
ns.PropertyState.DIRECT_VALUE),Array("FormsType",0,0,com.sun.star.beans.PropertyState.DIRECT_VALUE),Array("ExportFormFields",0,true,com.sun.star.beans.PropertyState.DIRECT_VALUE),Array("AllowDuplicateFieldNames",0,false,com.sun.star.beans.PropertyState.DIRECT_VALUE),Array("HideViewerToolbar",0,false,com.sun.star.beans.PropertyState.DIRECT_VALUE),Array("HideViewerMenubar",0,false,com.sun.star.beans.PropertyState.DIRECT_VALUE),Array("HideViewerWindowControls",0,false,com.sun.star.beans.PropertyState.DIRECT_VALUE),Array("ResizeWindowToInitialPage",0,false,com.sun.star.beans.PropertyState.DIRECT_VALUE),Array("CenterWindow",0,false,com.sun.star.beans.PropertyState.DIRECT_VALUE),Array("OpenInFullScreenMode",0,false,com.sun.star.beans.PropertyState.DIRECT_VALUE),Array("DisplayPDFDocumentTitle",0,true,com.sun.star.beans.PropertyState.DIRECT_VALUE),Array("InitialView",0,0,com.sun.star.beans.PropertyState.DIRECT_VALUE),Array("Magnification",0,0,com.sun.star.beans.PropertyState.DIRECT_VALUE),Ar
ray("Zoom",0,100,com.sun.star.beans.PropertyState.DIRECT_VALUE),Array("PageLayout",0,0,com.sun.star.beans.PropertyState.DIRECT_VALUE),Array("FirstPageOnLeft",0,false,com.sun.star.beans.PropertyState.DIRECT_VALUE),Array("InitialPage",0,1,com.sun.star.beans.PropertyState.DIRECT_VALUE),Array("Printing",0,2,com.sun.star.beans.PropertyState.DIRECT_VALUE),Array("Changes",0,4,com.sun.star.beans.PropertyState.DIRECT_VALUE),Array("EnableCopyingOfContent",0,true,com.sun.star.beans.PropertyState.DIRECT_VALUE),Array("EnableTextAccessForAccessibilityTools",0,true,com.sun.star.beans.PropertyState.DIRECT_VALUE),Array("ExportLinksRelativeFsys",0,false,com.sun.star.beans.PropertyState.DIRECT_VALUE),Array("PDFViewSelection",0,0,com.sun.star.beans.PropertyState.DIRECT_VALUE),Array("ConvertOOoTargetToPDFTarget",0,false,com.sun.star.beans.PropertyState.DIRECT_VALUE),Array("ExportBookmarksToPDFDestination",0,false,com.sun.star.beans.PropertyState.DIRECT_VALUE),Array("_OkButtonString",0,"",com.sun.star.bea
ns.PropertyState.DIRECT_VALUE),Array("Watermark",0,"",com.sun.star.beans.PropertyState.DIRECT_VALUE),Array("EncryptFile",0,false,com.sun.star.beans.PropertyState.DIRECT_VALUE),Array("PreparedPasswords",0,com.sun.star.beans.PropertyState.DIRECT_VALUE),Array("RestrictPermissions",0,false,com.sun.star.beans.PropertyState.DIRECT_VALUE),Array("PreparedPermissionPassword",0,Array(),com.sun.star.beans.PropertyState.DIRECT_VALUE),Array("PageRange",0,"3",com.sun.star.beans.PropertyState.DIRECT_VALUE))

dispatcher.executeDispatch(document, ".uno:ExportToPDF", "", 0, args1())

end sub

I haven't been able to get back to this for a while.

So I have to ask why one would say this? In many cases the purpose of doing something from a macro inside LibreOffice is to have an integrated solution so one doesn't depend on having to run external programs.

I've been looking into this and this is quite possible - granted, it took me about 6-8 hours to find all the details, but it's possible. I'll be posting a sample within the next few days.

It just seems to me saying it's better to do something else is a "can't do" attitude that defeats much of what one would want to do with macros.

Hal

Hi :) 
Perhaps "Print to file" and choose Pdf instead of Ps as the file-type and restrict to just "Selected pages".

However OpenSource is set-up to have specialist programs that can work together to produce output for a wide range of different requirements.

If you were planting flowers would you use a butter knife to dig the holes?  Would you use a trowel to cut your hedges?  Would you use a hedge strimmer to spread butter on a slice of bread?

Typically the proprietary route is to make 1 program that does everything.  It is not a specialist in those extra rarely used things so does a fairly lousy job.  Also when i want a midnight snack i have to get my boots on and the trowel and the hedge strimmer and a ton of other things i wont need when all i wanted was a little slice of toast with a bit of jam. The OpenSource route is to put only the right tool in my hand (well, a choice of right tools).  The OpenSource is not a "can't do" attitude.  It's a "use the right tool for the job" approach.

Regards from
Tom :slight_smile:

Hi :slight_smile:
Perhaps "Print to file" and choose Pdf instead of Ps as the file-type and restrict to just "Selected pages".

I've been looking at that - it's one thing I found as a possibility, but I did find out how to control the PDF export so I can set all the options. The part I found confusing was the wording on one reference page that led me down a blind alley for 5-6 hours. But that showed me it was possible, so when I finally found a different example, I was able to do it through the export filters.

However OpenSource is set-up to have specialist programs that can work together to produce output for a wide range of different requirements.

If you were planting flowers would you use a butter knife to dig the holes? Would you use a trowel to cut your hedges? Would you use a hedge strimmer to spread butter on a slice of bread?

Yes, I understand the analogy, but the tools are completely there in LibreOffice, so it's not like using a trowel to cut hedges. It's using a different kind of clipper.

Everything to do it is there, perfectly accessible to a BASIC macro, the only problem is that documentation for this kind of thing can often be quite difficult to find.

There are times when we won't use a trowel to cut hedges, but we may use another sharp edged tool that originally wasn't designed for hedges. There could be any number of reasons for that, it doesn't matter what they are, it does matter that a person knows their yard and their hedges and the setup and has a valid reason for not using a hedge trimmer. For instance, I do have power hedge trimmers, but there are some hedges in my yard where a sickle works much better.

So there are times when one may use a different tool - and there can be plenty of valid reasons for doing so.

But, again, the tools are right there in LibreOffice, and, fro a programming perspective, they're easy to access (it was finding the information that took a lot of searching and using just the right search terms to get different search results). So using a macro in LibreOffice is nothing like using a butter knife to dig holes. LibreOffice is the right tool for the right job. I still have to clean up my code and add comments, but when I do, I'll be posting it.

And, honestly, I think it's a good thing that I didn't accept that answer (that there's better tools) because if I hadn't, we wouldn't know this can be done so easily. One can not only export a PDF, but have full control over every setting in the PDF export through a BASIC macro.

The documentation and examples of this are for Java, but it's all accessible.

Typically the proprietary route is to make 1 program that does everything. It is not a specialist in those extra rarely used things so does a fairly lousy job. Also when i want a midnight snack i have to get my boots on and the trowel and the hedge strimmer and a ton of other things i wont need when all i wanted was a little slice of toast with a bit of jam. The OpenSource route is to put only the right tool in my hand (well, a choice of right tools). The OpenSource is not a "can't do" attitude. It's a "use the right tool for the job" approach.

Again, not a valid analogy in this case - there than the point about wanting a midnight snack, but having to get your boots on and the trowel and hedge trimmer and a ton of other things.

If LibreOffice will do this job and do it well, then all I need is a midnight snack. I don't have to add other tools to my system to do the job. I have one tool that will do this task and do it well. It's customizable and it means processing the file one time - and not dealing with issues that might come up if I use multiple programs where each might make other changes to the files or make adaptations requiring finding what I call a "settings cocktail" (in other words, having to find just the right settings on everything).

One other point: The answer I was responding to was, as I see it, quite useless. One big reason was because he said LO was not the best tool - but then did not give one suggestion on what to use instead.

I think, if you're starting with an ODT file, that's already in LO, if LO will convert it to a multi-page PDF with a macro, then that's the best tool for the job. It saves steps in the long run and does the job directly, without the need of any more programs or tools.

Hal

Hi :slight_smile:
Brilliant!  It's good to hear you worked out the answer.  Sorry we were very unhelpful there.  Uploading as an Extension or something would be superb.
Many thanks and apols from
Tom :slight_smile:

Main Issue: How to export PDF files using one PDF per page from a LibreOffice BASIC macro
Related Issue: How to change any values with a macro when exporting to PDF files

I've left my original question intact, but this includes the answer - this code and the two links will show you what you need to know to write a macro for LibreOffice that can export a PDF with whatever settings you desire. I'm saving the code to last (right before the text of the original question).

These web pages are geared toward Java, and things do work differently in Java, in many cases, than they do in BASIC. The first is a tutorial on how to export a PDF file from LO in Java:

http://wiki.openoffice.org/wiki/API/Tutorials/PDF_export

A few issues to note if you're looking through this:
- You do not need most of the objects created in the Java program. You can use ThisComponent in a BASIC macro and that gives you access to the interfaces and models and so forth needed to get the information or to do what you need to do.
- "PDF Export filter data" is the main section you'll need. It includes information about all the PDF settings that can be used in the PropertyValue object "FilterData" They're the same in Java as in BASIC.

The second page contains a Java example of someone setting almost any property possible for exporting a PDF:

http://forum.openoffice.org/en/forum/viewtopic.php?t=1804

This page helped me because, even after multiple readings, I still misunderstood (and didn't know it was a misunderstanding) one paragraph in the first linked page, so it clarified a major point for me.

About My Code: The code below is a macro that will export an ODT file as multiple PDFs - one PDF per page of the original document, all stored in a directory that is the same name as the original document's name but with "-PDF" instead of ".odt" at the end. PDF File names are in the format of "Page-0001.pdf" with the page number always as 4 digits, with leading zeros. I've added a number of comments that should explain most of what's needed. I don't know how to also include, here, the dialog box I use. It has a text label that can be set to whatever you want and has a progress bar (we see how that's accessed in the code) and the progress bar is updated each time a page is done.

There are two issues with the progress bar. The first is that I had to put in a 1 millisecond wait after it is updated, or it wouldn't update (I guess LO gives priority to running the macro code than in updating the GUI). The other is that, on my dialog, I added a "Cancel" button, but I have two problems there. One is that it doesn't seem to respond, and the other is that I wish I could pass a pointer to the progress bar so I can keep the dialog in my library and easily tell it, from any macro, what method to use when Cancel is clicked on.

Here is the source code:

I believe you can print your ODT files to PDF either as one single file or as a collection of one-per-page files.

jdh

I believe you can print your ODT files to PDF either as one single file or as a collection of one-per-page files.

The trick is finding the settings so you can specify which page or pages from within a macro.

But I've got it all worked out for an export.

Hal