[Solved] Writer - Export to PDF section

Creating a macro - Writing a Script - Using the API

[Solved] Writer - Export to PDF section

Postby LUFAVARA » Wed Jan 17, 2018 3:16 pm

good morning,
I have a document of many pages divided into sections. I would like to export each section of the document to a pdf file.
Files can be saved by assigning a number as a name.
I have no programming experience on open office. Does anyone have any ideas?
Thanks in advance
Last edited by Hagar Delest on Sat Jan 20, 2018 10:58 pm, edited 1 time in total.
Reason: tagged [Solved].
LibreOffice 5.4.4
LUFAVARA
 
Posts: 7
Joined: Wed Oct 29, 2014 5:53 pm

Re: WRITER - EXPORT TO PDF SECTION

Postby musikai » Wed Jan 17, 2018 5:33 pm

This macro will export each section to a PDF into the same folder. The PDFs are named: "documentname - sectionname.pdf"
It works by hiding all the other sections before export. (at the End all sections are unhidden again). Text outside of any section will also be exported.
If anybody knows how to select the textrange of a section by macro then he could possibly write an alternative version.

Code: Select all   Expand viewCollapse view
sub save_Sections_as_PDF
if len(ThisComponent.getURL())=0 then
msgbox "Save document first!"
exit sub
end if
If (Not GlobalScope.BasicLibraries.isLibraryLoaded("Tools")) Then GlobalScope.BasicLibraries.LoadLibrary("Tools")
sPath = convertfromurl(DirectoryNameoutofPath(ThisComponent.getURL(),"/"))
docname = convertfromurl(GetFileNameWithoutExtension(ThisComponent.getURL(),"/"))

if ThisComponent.TextSections.count=0 then
msgbox "There are no sections in this document!"
exit sub
end if

for i=0 to ThisComponent.TextSections.count-1
oSection = ThisComponent.TextSections.getByIndex(i)
oSection.isvisible= false
next
i=0
for i=0 to ThisComponent.TextSections.count-1
oSection = ThisComponent.TextSections.getByIndex(i)
SectionName = oSection.name
sName=docname & " - " & SectionName
oSection.isvisible= true
saveaspdf(sPath,sName)
oSection.isvisible= false
next
i=0
for i=0 to ThisComponent.TextSections.count-1
oSection = ThisComponent.TextSections.getByIndex(i)
oSection.isvisible= true
next
end sub

Sub saveaspdf(sPath,sName)
Dim args3(0) as new com.sun.star.beans.PropertyValue
args3(0).Name = "FilterName"
args3(0).Value = "writer_pdf_Export"
thiscomponent.storeToURL(converttourl(sPath & getpathseparator & sName & ".pdf"),args3())
End Sub
Win7 Pro, Lubuntu 15.10, LO 4.4.7, OO 4.1.3
Free Project: LibreOffice Songbook Architect (LOSA)
http://struckkai.blogspot.de/2015/04/libreofficesongbookarchitect.html
musikai
Volunteer
 
Posts: 227
Joined: Wed Nov 11, 2015 12:19 am

Re: Writer - Export to PDF section

Postby LUFAVARA » Thu Jan 18, 2018 10:03 am

Many thanks.


It works perfectly in the case of a document with a single section for each recipient however
is it possible to modify the macro to export a pdf to each manual page break?
Thanks again
LibreOffice 5.4.4
LUFAVARA
 
Posts: 7
Joined: Wed Oct 29, 2014 5:53 pm

Re: Writer - Export to PDF section

Postby musikai » Thu Jan 18, 2018 11:45 am

Don't understand. If you exactly describe the problem, the wanted result and upload an example then chances are higher to get a solution.
Win7 Pro, Lubuntu 15.10, LO 4.4.7, OO 4.1.3
Free Project: LibreOffice Songbook Architect (LOSA)
http://struckkai.blogspot.de/2015/04/libreofficesongbookarchitect.html
musikai
Volunteer
 
Posts: 227
Joined: Wed Nov 11, 2015 12:19 am

Re: Writer - Export to PDF section

Postby LUFAVARA » Thu Jan 18, 2018 12:07 pm

I have a document containing a thousand pages. it is made up of letters to be sent to users who sometimes have one, two or three pages. There is a manual page jump between users. I would need to separate these letters and save them as separate pdf.


Attached the file that contains only the first pages of example ...

Thank
Attachments
sollecito.odt
(23.21 KiB) Downloaded 47 times
LibreOffice 5.4.4
LUFAVARA
 
Posts: 7
Joined: Wed Oct 29, 2014 5:53 pm

Re: Writer - Export to PDF section

Postby musikai » Thu Jan 18, 2018 11:05 pm

Hi LUFAVARA,

this macro works ok on your sample document. It will export the PDFs into a subfolder called "PDFs"

edit: corrected code below
Last edited by musikai on Fri Jan 19, 2018 1:13 pm, edited 2 times in total.
Win7 Pro, Lubuntu 15.10, LO 4.4.7, OO 4.1.3
Free Project: LibreOffice Songbook Architect (LOSA)
http://struckkai.blogspot.de/2015/04/libreofficesongbookarchitect.html
musikai
Volunteer
 
Posts: 227
Joined: Wed Nov 11, 2015 12:19 am

Re: Writer - Export to PDF section

Postby LUFAVARA » Fri Jan 19, 2018 9:14 am

I've tried the macro but it generates a runtime error." Basic error". "object variable not set" in the ViewCursor.gotoRange line (oCurs, true).
I test with libreoffice 5.4.4 on windows
LibreOffice 5.4.4
LUFAVARA
 
Posts: 7
Joined: Wed Oct 29, 2014 5:53 pm

Re: Writer - Export to PDF section

Postby musikai » Fri Jan 19, 2018 1:10 pm

Ah sorry, little typo.

Code: Select all   Expand viewCollapse view
Sub split_at_PageBreaks_and_save_as_PDF
if len(ThisComponent.getURL())=0 then
msgbox "Save document first!"
exit sub
end if
If (Not GlobalScope.BasicLibraries.isLibraryLoaded("Tools")) Then GlobalScope.BasicLibraries.LoadLibrary("Tools")
sPath = convertfromurl(DirectoryNameoutofPath(ThisComponent.getURL(),"/"))
docname = convertfromurl(GetFileNameWithoutExtension(ThisComponent.getURL(),"/"))
sPath=sPath & getpathseparator & "PDFs"
oViewCursor = ThisComponent.CurrentController.getViewCursor()

   oCurs = ThisComponent.getText().createTextCursor()
   oCurs.GoToStart(False)
   i=1
   Do
   oViewCursor.gotoRange(oCurs, False)
      Do
         if oCurs.gotoNextParagraph(false)=0 then
         endofdocument=1
         Exit Do
         end if
       oCurs.gotoEndOfParagraph(false)
      
       If not IsEmpty(oCurs.PageDescName) Then Exit Do
       If oCurs.BreakType <> 0 Then Exit Do
      Loop
      
      if endofdocument=0 then
      oCurs.gotoPreviousParagraph(false)
      end if
   oCurs.gotoEndOfParagraph(false)
   oViewCursor.gotoRange(oCurs, true)
   sName=docname & "_" & i
   save_selection_as_pdf(sPath,sName)
   i=i+1

   Loop while oCurs.gotoNextParagraph(False)

msgbox "Finish!"
End Sub

Sub save_selection_as_pdf(sPath,sName)
oselection=thiscomponent.getcurrentselection
Dim aFilterData(0) as new com.sun.star.beans.PropertyValue
aFilterData(0).Name = "Selection"
aFilterData(0).Value = oselection

Dim args3(1) as new com.sun.star.beans.PropertyValue
args3(0).Name = "FilterName"
args3(0).Value = "writer_pdf_Export"
args3(1).Name = "FilterData"
args3(1).Value = aFilterData
thiscomponent.storeToURL(converttourl(sPath & getpathseparator & sName & ".pdf"),args3())

End Sub
Win7 Pro, Lubuntu 15.10, LO 4.4.7, OO 4.1.3
Free Project: LibreOffice Songbook Architect (LOSA)
http://struckkai.blogspot.de/2015/04/libreofficesongbookarchitect.html
musikai
Volunteer
 
Posts: 227
Joined: Wed Nov 11, 2015 12:19 am

Re: Writer - Export to PDF section

Postby LUFAVARA » Sat Jan 20, 2018 8:21 pm

works perfectly.

Many thanks :super:
LibreOffice 5.4.4
LUFAVARA
 
Posts: 7
Joined: Wed Oct 29, 2014 5:53 pm


Return to Macros and UNO API

Who is online

Users browsing this forum: No registered users and 1 guest