Export as PDF via API ignores all arguments

I am using the API to export a document as PDF.
I have found that none of the arguments that I pass to storeAsURL is taken into account.
The following code should be sufficient to reproduce the bug.
The code adds a comment to the document.
The arguments that are being passed to “storeToURL” indicate that comments may not be exported.
The resulting PDF however does show the comment.

Exporting as PDF via LibreOffice results in the expected behaviour, so I would assume a bug in the API or my misuse of the API.


Public Sub Test()
       
    Dim oSM As Object ' Service manager
    Dim oDesk As Object ' Desktop (used to open documents)
    Dim oDoc As Object ' The document
    
    Set oSM = CreateObject("com.sun.star.ServiceManager")
    Set oDesk = oSM.createInstance("com.sun.star.frame.Desktop")

    Dim noArgs()         'An empty array for the arguments
    Set oDoc = oDesk.loadComponentFromURL("file:///D:/template.odt", "_blank", 0, noArgs())

    AddCommentToLibreOfficeDocument oDoc

    Dim args(0 To 1) As Object
    Set args(0) = oSM.Bridge_GetStruct("com.sun.star.beans.PropertyValue")
    Set args(1) = oSM.Bridge_GetStruct("com.sun.star.beans.PropertyValue")
    
    args(0).Name = "FilterName"
    args(0).value = "writer_pdf_Export"
    
    args(1).Name = "ExportNotes"
    args(1).value = False

    Call oDoc.storeToURL("file:///d:/test.pdf", args)
    
End Sub
Private Sub AddCommentToLibreOfficeDocument(ByRef oDoc As Object)

    Dim oAnnotation As Object
    Set oAnnotation = oDoc.createInstance("com.sun.star.text.textfield.Annotation")

    oAnnotation.Author = "Autor Name"
    oAnnotation.Content = "This is a sample comment."
    
    Dim oText As Object
    Set oText = oDoc.GetText()
    
    Dim oCursor As Object
    Set oCursor = oText.createTextCursor()
    
    oCursor.gotoEnd (False)
    
    Call oText.insertTextContent(oCursor, oAnnotation, False)
    
End Sub

Why do you assume that the filter arguments are passed this way? Did you follow some documentation? Did you check e.g. MediaDescriptor service? Because that’s the service mentioned in the storeToURL.

Public Sub Test()
       
    Dim oDoc As Object ' The document
    
    oDoc=StarDesktop.loadComponentFromURL("file:///D:/template.odt", "_blank", 0, Array())

    AddCommentToLibreOfficeDocument oDoc

    Dim args(1) As New com.sun.star.beans.PropertyValue
    
    args(0).Name = "FilterName"
    args(0).value = "writer_pdf_Export"
    
    args(1).Name = "ExportNotes"
    args(1).value = False

    oDoc.storeToURL("file:///d:/test.pdf", args)
    
End Sub

Which reference do I have to add to VBA or VB6 for this so that the compiler does not throw an error because it doesn’t know com.sun.star.beans.PropertyValue?

I don’t have drive “D”.
In the example below we export with the following Filter options.

ExportNotesInMargin=True
ExportNotes=True

Macros for VBA.

Public Sub Test()
       
    Dim oSM As Object ' Service manager
    Dim oDesk As Object ' Desktop (used to open documents)
    Dim oDoc As Object ' The document
    Dim args(0 To 1) As Object
    Dim args2(0 To 1) As Object
    Set oSM = CreateObject("com.sun.star.ServiceManager")
    Set oDesk = oSM.createInstance("com.sun.star.frame.Desktop")

    Set oDoc = oDesk.loadComponentFromURL("file:///C:/temp/template.odt", "_blank", 0, Array())

    AddCommentToLibreOfficeDocument oDoc

    Set args(0) = oSM.Bridge_GetStruct("com.sun.star.beans.PropertyValue")
    Set args(1) = oSM.Bridge_GetStruct("com.sun.star.beans.PropertyValue")
    Set args2(0) = oSM.Bridge_GetStruct("com.sun.star.beans.PropertyValue")
    Set args2(1) = oSM.Bridge_GetStruct("com.sun.star.beans.PropertyValue")
  
    args2(0).Name = "ExportNotes":   args2(0).Value = True
    args2(1).Name = "ExportNotesInMargin": args2(1).Value = True
    
    args(0).Name = "FilterName": args(0).Value = "writer_pdf_Export"
    args(1).Name = "FilterOptions": args(1).Value = args2

    Call oDoc.storeToURL("file:///C:/temp/test.pdf", args)
    
End Sub

– I need a few more minutes to investigate –

Take your time, I plan to participate in the forum for at least another 10 years. :smile:

Thank you! :slight_smile:

It works fine for Notes, but not for AllowDuplicateFieldNames.
Could you check that?

Here is what I am using:

    Dim args(0 To 1) As Object
    Set args(0) = oSM.Bridge_GetStruct("com.sun.star.beans.PropertyValue")
    Set args(1) = oSM.Bridge_GetStruct("com.sun.star.beans.PropertyValue")
    
    Dim filterArgs(0 To 2) As Object
    Set filterArgs(0) = oSM.Bridge_GetStruct("com.sun.star.beans.PropertyValue")
    Set filterArgs(1) = oSM.Bridge_GetStruct("com.sun.star.beans.PropertyValue")
    Set filterArgs(2) = oSM.Bridge_GetStruct("com.sun.star.beans.PropertyValue")
    
    filterArgs(0).Name = "AllowDuplicateFieldNames":   filterArgs(0).value = True
    filterArgs(1).Name = "ExportNotesInMargin": filterArgs(1).value = True
    filterArgs(2).Name = "ExportFormFields": filterArgs(2).value = True
    
    args(0).Name = "FilterName": args(0).value = "writer_pdf_Export"
    args(1).Name = "FilterOptions": args(1).value = filterArgs

I’m much more familiar with Mr. Calc than I am with Mr. Writer. :slightly_smiling_face:
Could you upload the original .odt document and the full text of the macro?

template.odt (9.5 KB)

Public Sub Test()
       
    Dim oSM As Object ' Service manager
    Dim oDesk As Object ' Desktop (used to open documents)
    Dim oDoc As Object ' The document
    
    Set oSM = CreateObject("com.sun.star.ServiceManager")
    Set oDesk = oSM.createInstance("com.sun.star.frame.Desktop")

    Dim noArgs()         'An empty array for the arguments
    Set oDoc = oDesk.loadComponentFromURL("file:///c:/template.odt", "_blank", 0, noArgs())

    AddCommentToLibreOfficeDocument oDoc

    Dim args(0 To 1) As Object
    Set args(0) = oSM.Bridge_GetStruct("com.sun.star.beans.PropertyValue")
    Set args(1) = oSM.Bridge_GetStruct("com.sun.star.beans.PropertyValue")
    
    Dim filterArgs(0 To 2) As Object
    Set filterArgs(0) = oSM.Bridge_GetStruct("com.sun.star.beans.PropertyValue")
    Set filterArgs(1) = oSM.Bridge_GetStruct("com.sun.star.beans.PropertyValue")
    Set filterArgs(2) = oSM.Bridge_GetStruct("com.sun.star.beans.PropertyValue")
    
    filterArgs(0).Name = "AllowDuplicateFieldNames":   filterArgs(0).value = True
    filterArgs(1).Name = "ExportNotesInMargin": filterArgs(1).value = True
    filterArgs(2).Name = "ExportFormFields": filterArgs(2).value = True
    
    args(0).Name = "FilterName": args(0).value = "writer_pdf_Export"
    args(1).Name = "FilterOptions": args(1).value = filterArgs

    Call oDoc.storeToURL("file:///c:/test.pdf", args)

End Sub
Private Sub AddCommentToLibreOfficeDocument(ByRef oDoc As Object)

    Dim oAnnotation As Object
    Set oAnnotation = oDoc.createInstance("com.sun.star.text.textfield.Annotation")

    oAnnotation.Author = "Autor Name"
    oAnnotation.Content = "This is a sample comment."
    
    Dim oText As Object
    Set oText = oDoc.GetText()
    
    Dim oCursor As Object
    Set oCursor = oText.createTextCursor()
    
    oCursor.gotoEnd (False)
    
    Call oText.insertTextContent(oCursor, oAnnotation, False)
    
End Sub

Thanks for the example!
I propose the following testing methodology.
We export the file to .pdf through the dialog
Menu / File / Export as... / Export as PDF...
using various options, and then we try to do the same with a macro and compare the results.


What is the difference for the option: “Allow Duplicate Field Names”?

Here is a macro that I recorded.

The first section shows export without the option “Allow Duplicate Field Names”, the second one is with this option enabled.

The difference is
Array(“AllowDuplicateFieldNames”,0,false,
Array(“AllowDuplicateFieldNames”,0,true,

sub Main
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:///D:/test.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.beans.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),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,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.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("",0,,com.sun.star.beans.PropertyState.DIRECT_VALUE))

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

rem ----------------------------------------------------------------------
dim args2(2) as new com.sun.star.beans.PropertyValue
args2(0).Name = "URL"
args2(0).Value = "file:///D:/test.pdf"
args2(1).Name = "FilterName"
args2(1).Value = "writer_pdf_Export"
args2(2).Name = "FilterData"
args2(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.beans.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,true,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),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,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.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("",0,,com.sun.star.beans.PropertyState.DIRECT_VALUE))

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


end sub

Sorry, that’s not what I meant:

  1. Export the file (File1.pdf) through the dialog.
  2. Export the file (File2.pdf) using a macro (Test macro in VBA examples). The macro options must match the dialog options.
  3. Compare the results (files File1.pdf and File2.pdf). If there are no visible differences, then the API arguments (your theme header) are processed correctly.

Macro Recorder is not required.

Please explain what you mean by “(Test macro in VBA examples).”!
Thank you!

Run мacro Test in (Excel?) VBA.

Ok, I understood you now.

When I run the macro, it’s faulty: It does not work.

Only when I export via the dialog it works.

So I have to post a bug report, right?

1 Like

tdf#160001.