Writer exported to PDF via script but nav links don't work

When I manually export this writer doc as a PDF all the links work as they should. However, if I use a script to export the writer doc, none of the links work (but they look like links). I need the links to work so how do I do that in the script?

To show you what I mean here are links to both PDFs. First the manual export with working links then the script export in which none of the links work

Also, here is zip with all the files used and the script that makes them.

While the script is included in that zip (as a bas file), it’s also below for easy access:

	REM Making example pdfs based on writer file while pulling data from a calc file
	Dim strPath As String
	
	Dim objWriterDoc As Object
	Dim objCalcDoc As Object
	Dim objCalcSheet as Object
	Dim objCalcCellForID as Object
	Dim objCalcCellForName as Object
	Dim lngRows as Long
	
	Dim args()
	Dim PDFargs(1) As New "com.sun.star.beans.PropertyValue"
	Dim Watermarkargs(4) As New "com.sun.star.beans.PropertyValue"
	
	REM set PDF export args
	PDFargs(0).Name = "FilterName"
	PDFargs(0).Value = "writer_pdf_Export"
	PDFargs(1).Name = "FilterData"
	PDFargs(1).Value = Array(Array("UseLosslessCompression",0,false,com.sun.star.beans.PropertyState.DIRECT_VALUE),_
							 Array("Quality",0,70,com.sun.star.beans.PropertyState.DIRECT_VALUE),_
							 Array("ReduceImageResolution",0,true,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("PDFUACompliance",0,false,com.sun.star.beans.PropertyState.DIRECT_VALUE),_
							 Array("ExportNotes",0,false,com.sun.star.beans.PropertyState.DIRECT_VALUE),_
							 Array("ViewPDFAfterExport",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("ExportPlaceholders",0,false,com.sun.star.beans.PropertyState.DIRECT_VALUE),_
							 Array("IsAddStream",0,false,com.sun.star.beans.PropertyState.DIRECT_VALUE),_
							 Array("ExportFormFields",0,true,com.sun.star.beans.PropertyState.DIRECT_VALUE),_
							 Array("FormsType",0,0,com.sun.star.beans.PropertyState.DIRECT_VALUE),_
							 Array("AllowDuplicateFieldNames",0,false,com.sun.star.beans.PropertyState.DIRECT_VALUE),_
							 Array("HideViewerToolbar",0,true,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,1,com.sun.star.beans.PropertyState.DIRECT_VALUE),_
							 Array("Magnification",0,0,com.sun.star.beans.PropertyState.DIRECT_VALUE),_
							 Array("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,true,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("SignPDF",0,false,com.sun.star.beans.PropertyState.DIRECT_VALUE),_
							 Array("_OkButtonString",0,"",com.sun.star.beans.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("SignatureLocation",0,"",com.sun.star.beans.PropertyState.DIRECT_VALUE),_
							 Array("SignatureReason",0,"",com.sun.star.beans.PropertyState.DIRECT_VALUE),_
							 Array("SignatureContactInfo",0,"",com.sun.star.beans.PropertyState.DIRECT_VALUE),_
							 Array("SignaturePassword",0,"",com.sun.star.beans.PropertyState.DIRECT_VALUE),_
							 Array("SignatureCertificate",0,,com.sun.star.beans.PropertyState.DIRECT_VALUE),_
							 Array("SignatureTSA",0,"",com.sun.star.beans.PropertyState.DIRECT_VALUE),_
							 Array("UseReferenceXObject",0,false,com.sun.star.beans.PropertyState.DIRECT_VALUE))
	
	REM open the writer file
    strPath = ConvertToUrl("/home/wrg/Downloads/example.odt")
    objWriterDoc = StarDesktop.loadComponentFromURL(strPath, "default", 0, args())
    
    REM open the calc list
    strPath = ConvertToUrl("/home/wrg/Downloads/example_list.ods")
    objCalcDoc = StarDesktop.loadComponentFromURL(strPath, "default", 0, args())
    objCalcSheet = objCalcDoc.Sheets(0)
    REM select the cols of all the needed rows
    PTListRanage = objCalcSheet.getCellRangeByName("A1:B10")

	REM set watermark params
	Watermarkargs(0).Name = "Text"
	Watermarkargs(0).Value = "Init" ' Will change this to custom text based on the calc file '
	Watermarkargs(1).Name = "Font"
	Watermarkargs(1).Value = "Malgun Gothic Semilight"
	Watermarkargs(2).Name = "Angle"
	Watermarkargs(2).Value = 315 ' Degrees, int '
	Watermarkargs(3).Name = "Transparency"
	Watermarkargs(3).Value = 88 ' Percent, int '
	Watermarkargs(4).Name = "Color"
	Watermarkargs(4).Value = 12632256 ' FF0000 = Red; number only '
	
	document   = objWriterDoc.CurrentController.Frame
	oDispHelper = createUnoService("com.sun.star.frame.DispatchHelper")
    
    REM main loop of dataset
	lngRows = PTListRanage.Rows.Count - 1
	For r = 0 To lngRows
		objCalcCellForID = objCalcSheet.getCellByPosition(0,r)
		objCalcCellForName = objCalcSheet.getCellByPosition(1,r)
		
		REM process the row if there is a name to process
		If objCalcCellForName.String <> "" Then
			
			REM set the WhoFor text field
			if objWriterDoc.getTextFieldMasters.hasByName("com.sun.star.text.fieldmaster.SetExpression.WhoFor") then
				oVar = objWriterDoc.getTextFieldMasters.getByName("com.sun.star.text.fieldmaster.SetExpression.WhoFor")
				oVar.DependentTextFields(0).content = objCalcCellForName.String
			else
				msgbox "WhoFor not found"
			end if
			
			REM set the WhoForID text field
			if objWriterDoc.getTextFieldMasters.hasByName("com.sun.star.text.fieldmaster.SetExpression.WhoForID") then
				oVar = objWriterDoc.getTextFieldMasters.getByName("com.sun.star.text.fieldmaster.SetExpression.WhoForID")
				oVar.DependentTextFields(0).content = objCalcCellForID.String
			else
				msgbox "WhoForID not found"
			end if
			
			REM add text watermark
			Watermarkargs(0).Value = "For ID "+objCalcCellForID.String ' Which text will be shown as the watermark '
			oDispHelper.executeDispatch(document, ".uno:Watermark", "", 0, Watermarkargs())
			
			REM export to PDF format
		    strPath = ConvertToURL("/home/wrg/Downloads/example("+objCalcCellForID.String+").pdf")
		    objWriterDoc.storeToURL(strPath, PDFargs())
		End If
	Next r
	
	REM close all the files we opened
	objCalcDoc.close(True)
	objWriterDoc.close(True)

[EDIT]
If my link to the zip file isn’t working for you here are copies of the Writer doc and the Calc doc that the above script is using.
example_list.ods (14.2 KB)
example.odt (11.8 KB)

You have linked zero size sample files. (They have zero size when I download them.)

1 Like

It’s HTTP not HTTPS so some browsers will automatically not pull the file so you get a zero length. You should be able to override and force a pull (in firefox open the download manager, right click the entry, and select allow download)

Also edited the post to include a non-zip link for anyone having the same issue you are.

your working code is two things included:

  1. Watermark :add watermark to the docx
  2. Export : convert docx to pdf
    your problem is only add watermark without export?

No, the issue was exporting the docx to pdf with a script made a pdf with bad hyperlinks. But doing the export manually make a pdf with working hyperlinks. So, how do I change the script so it works too?

The export code looks like it was copied from the macro recorder.
The macro recorder does not always get a working code (warning when activated)

I have managed to make a macro for the export with a code of Mike Kaganski found in bugzilla:
https://bugs.documentfoundation.org/show_bug.cgi?id=150600
You must set the properties individually for each option (with the Function PropertyValue) and then pass them to the filter as an array.

The summarized code is as follows.

Rem ############
Function PropertyValue(n, v)
 Dim result as new com.sun.star.beans.PropertyValue
   result.Name = n
   result.Value = v
   PropertyValue = result
End function
Rem #### Your macro settings
 Dim FilterArgs()
   FilterArgs = Array(PropertyValue("UseLosslessCompression",true),_
   PropertyValue("Quality",90),_
   PropertyValue("ReduceImageResolution",true),_
   PropertyValue("MaxImageResolution",300),_
   PropertyValue("Watermark",""),_
   PropertyValue("IsAddStream",false),_
   PropertyValue("SelectPdfVersion",2), OTHER SETTINGS )

 Dim FILE As String, cmdArgs(1) as New com.sun.star.beans.PropertyValue
   FILE = "FILE_NAME"
   cmdArgs(0).Name = "FilterName"
   cmdArgs(0).Value = "writer_pdf_Export"
   cmdArgs(1).Name = "FilterData"
   cmdArgs(1).Value = FilterArgs()

The final command I use is

ThisComponent.storeToURL(FILE, cmdArgs())