ToC and Bookmarks in PDF export work in one pdf but not the other

I have a problem that I have been trying to understand for a long while and I am very confused as to why it’s not working as it should.

I have a macro where I want to export the current document as a PDF. There are two Functions that get called depending on the mode of operation. Both use the same bit of code to export the PDF. Only one has working bookmarks and ToC’s in the PDF, the other one does not work at all (they are present in the PDF file, but the links do not work)

Here is the PDF export bit that I use in both functions.

	args2(0).Name = "URL"
	args2(0).Value = fullPdfFilePathName
	args2(1).Name = "FilterName"
	args2(1).Value = "writer_pdf_Export"
	args2(2).Name = "ExportBookmarks"
	args2(2).Value = True
	args2(3).Name = "UseTaggedPDF"	'Use document headings
	args2(3).Value = True
	dispatcher.executeDispatch(document, ".uno:ExportDirectToPDF", "", 0, args2())

As both functions use this same bit of code, I even tried using a separate sub for the PDF export part, but that still resulted in non-working bookmarks and ToC.

I have saved the code as text files in the attached zip file rather than paste it here. The extra bit is PDFDoc.txt. I’ve also included a Test.odt file and three PDF’s. The manual export works fine as does the version from SaveFinalFile.txt. The non working pdf file is from SaveActiveFile.txt

The only thing I can think of is maybe how the file is referenced, but I’m clutching at straws here.

Files.zip (141.5 KB)

may be this post can help you

Two considerations about your code:

  • To concatenate text it is better to use the ampersand symbol (&) although you can use the plus sign (+) as macros may confuse it with a mathematical sum.

  • when you lock the screen, lockScreen(True) make sure to unlock it in error checking.
    If an error occurs, it may be blocked and the user cannot continue

Hi, thank you @bantoniof for the reply. I’ll have a look at the link you posted and use the ampersand instead of ‘+’ for clarity.

Yes, some of my code is generated from recording a set of steps, because I’m not that fluent in LO Basic. That same block of PDF code was generated from a macro and is used in three places and only one has working links, hence the thinking that perhaps it is how I reference the document maybe.

Regarding the error checking, I only posted the code of the relevant parts to this problem, my actual module is much larger. I haven’t shown it, but on an error, it jumps to another function where I show the error and unlock the screen as well.

More information. I have gone through the link and bug report and changed my code to the following

Sub PDFDoc(fullPdfFileNameAndPath As string)
Dim document as object
Dim dispatcher as Object
Dim args(2) as new com.sun.star.beans.PropertyValue
Dim FilterArgs()
 	
	document = ThisComponent.CurrentController.Frame
	dispatcher = createUnoService("com.sun.star.frame.DispatchHelper")

	'Set the various options for the pdf file
	FilterArgs = Array(PropertyValue("ExportBookmarks",True), _
		PropertyValue("UseTaggedPDF",True), _
		PropertyValue("OpenBookmarkLevels",2), _
		PropertyValue("ExportPlaceholders",True), _
		PropertyValue("ConvertOOoTargetToPDFTargets",True), _
		PropertyValue("DisplayPDFDocumentTitle",True), _
		PropertyValue("InitialView",1), _
		PropertyValue("ExportBookmarksToPDFDestination",True))

	'Generate the arguments
	args(0).Name = "URL"
	args(0).Value = fullPdfFileNameAndPath
	args(1).Name = "FilterName"
	args(1).Value = "writer_pdf_Export"
	args(2).Name = "FilterData"
	args(2).Value = FilterArgs()
	
	ThisComponent.storeToURL(fullPdfFileNameAndPath, args()) 'save the file as a PDF

and added this supporting function

Function PropertyValue(n, v)
dim result as new com.sun.star.beans.PropertyValue
  
	result.Name = n
	result.Value = v
	PropertyValue = result
  
End Function

This still generates a pdf file with non-working links. I re-recorded the macro of exporting a pdf file that generated a fully working pdf file. Then used that as my export function, this then produced another non-working pdf file.

I believe there is a bug somewhere. When even the macro generated code doesn’t work correctly, there’s a problem. I have added this comment to the bug report
https://bugs.documentfoundation.org/show_bug.cgi?id=150600

Am I missing something else?

There is another instruction "ExportBookmarks" .
(this is different of “ExportBookmarksToPDFDestination”) May be adding this instruction does the trick.

  • The “.uno:ExportDirectToPDF” is subject to the settings of the last export to PDF

Hi, that’s already in the array unfortunately.

FilterArgs = Array(PropertyValue("ExportBookmarks",True), _

sorry i did not see it (and it was the first)

Can you add the file (or portion of file) wich has te troubles?

I have test your macro in my system with the file text.odt (in your zip) and the bookmarks of the TOC are tottally functionals.
LibreOffice 25.8.2.2 and Windows 10