wizards/source/scriptforge/SF_Session.xba        |  113 +++++++++++++++++++++++
 wizards/source/scriptforge/python/scriptforge.py |    3 
 wizards/source/sfdocuments/SF_Calc.xba           |   15 ++-
 wizards/source/sfdocuments/SF_Chart.xba          |    2 
 wizards/source/sfdocuments/SF_Document.xba       |  101 ++++++++++++++++++++
 wizards/source/sfdocuments/SF_Writer.xba         |   11 ++
 6 files changed, 240 insertions(+), 5 deletions(-)

New commits:
commit 407b53dd2246e727d19fe94c82cb357af2e4f8ed
Author:     Jean-Pierre Ledure <j...@ledure.be>
AuthorDate: Thu Aug 19 15:58:22 2021 +0200
Commit:     Jean-Pierre Ledure <j...@ledure.be>
CommitDate: Thu Aug 19 17:16:59 2021 +0200

    ScriptForge - (PDF Export) get/set options, export as PDF file
    
    Next methods are introduced in SF_Session:
      - GetPDFExportOptions to extract a dictionary of the
        40+ existing options for PDF export
      - SetPDFExportOptions to update the existing options
        When applied the options are permanent also for user manual exports
    Those methods are not available in Python.
    
    Next method to export a document to PDF:
      - ExportAsPDF: it uses the options set above and/or takes
        next specific and transitional options:
          pages, password, watermark
    This method is implemented for use from Basic and Python
    
    Change-Id: Ic5c4190cff579e62137930f422638aad98e61a16
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/120740
    Tested-by: Jean-Pierre Ledure <j...@ledure.be>
    Tested-by: Jenkins
    Reviewed-by: Jean-Pierre Ledure <j...@ledure.be>

diff --git a/wizards/source/scriptforge/SF_Session.xba 
b/wizards/source/scriptforge/SF_Session.xba
index 63fd4c57bf0a..db3cb9449889 100644
--- a/wizards/source/scriptforge/SF_Session.xba
+++ b/wizards/source/scriptforge/SF_Session.xba
@@ -318,6 +318,61 @@ Catch:
        GoTo Finally
 End Function   &apos;  ScriptForge.SF_Session.ExecutePythonScript
 
+REM 
-----------------------------------------------------------------------------
+Public Function GetPDFExportOptions() As Variant
+&apos;&apos;&apos;     Return the actual values of the PDF export options
+&apos;&apos;&apos;     The PDF options are described on 
https://wiki.openoffice.org/wiki/API/Tutorials/PDF_export
+&apos;&apos;&apos;     PDF options are set at each use of the Export as ... 
PDF command by the user and kept
+&apos;&apos;&apos;     permanently until their reset by script or by a new 
export 
+&apos;&apos;&apos;     Args:
+&apos;&apos;&apos;     Returns:
+&apos;&apos;&apos;             A ScriptForge dictionary instance listing the 
40+ properties and their value
+&apos;&apos;&apos;     Examples:
+&apos;&apos;&apos;             Dim dict As Object
+&apos;&apos;&apos;             Set dict = session.GetPDFExportOptions()
+&apos;&apos;&apos;             MsgBox dict.Item(&quot;Quality&quot;)
+
+Dim vDict As Variant                           &apos;  Returned value
+Dim oConfig As Object                          &apos;  
com.sun.star.configuration.ConfigurationProvider
+Dim oNodePath As Object                                &apos;  
com.sun.star.beans.PropertyValue
+Dim oOptions As Object                         &apos;  configmgr.RootAccess
+Dim vOptionNames As Variant                    &apos;  Array of PDF options 
names
+Dim vOptionValues As Variant           &apos;  Array of PDF options values
+Dim i As Long
+
+Const cstThisSub = &quot;Session.GetPDFExportOptions&quot;
+Const cstSubArgs = &quot;&quot;
+
+       If SF_Utils._ErrorHandling() Then On Local Error GoTo Catch
+       Set vDict = Nothing
+
+Check:
+       SF_Utils._EnterFunction(cstThisSub, cstSubArgs)
+
+Try:
+       &apos;  Get the (read-only) internal PDF options
+       Set oConfig = SF_Utils._GetUNOService(&quot;ConfigurationProvider&quot;)
+       Set oNodePath = SF_Utils._MakePropertyValue(&quot;nodepath&quot;, 
&quot;/org.openoffice.Office.Common/Filter/PDF/Export/&quot;)
+       Set oOptions = 
oConfig.createInstanceWithArguments(&quot;com.sun.star.configuration.ConfigurationAccess&quot;,
 Array(oNodePath))
+
+       &apos;  Copy the options into a ScriptForge dictionary
+       Set vDict = CreateScriptService(&quot;dictionary&quot;)
+       vOptionNames = oOptions.getElementNames()
+       vOptionValues = oOptions.getPropertyValues(vOptionNames)
+       &apos;
+       For i = 0 To UBound(vOptionNames)
+               vDict.Add(vOptionNames(i), vOptionValues(i))
+       Next i
+
+
+Finally:
+       GetPDFExportOptions = vDict
+       SF_Utils._ExitFunction(cstThisSub)
+       Exit Function
+Catch:
+       GoTo Finally
+End Function   &apos;  ScriptForge.SF_Session.GetPDFExportOptions
+
 REM 
-----------------------------------------------------------------------------
 Public Function GetProperty(Optional ByVal PropertyName As Variant) As Variant
 &apos;&apos;&apos;     Return the actual value of the given property
@@ -665,6 +720,64 @@ CatchMail:
        GoTo Finally
 End Sub                        &apos;  ScriptForge.SF_Session.SendMail
 
+REM 
-----------------------------------------------------------------------------
+Public Function SetPDFExportOptions(Optional ByRef PDFOptions As Variant) As 
Boolean
+&apos;&apos;&apos;     Modify the actual values of the PDF export options from 
an options dictionary
+&apos;&apos;&apos;     The PDF options are described on 
https://wiki.openoffice.org/wiki/API/Tutorials/PDF_export
+&apos;&apos;&apos;     PDF options are set at each use of the Export as ... 
PDF command by the user and kept
+&apos;&apos;&apos;     permanently until their reset by script (like this one) 
or by a new export
+&apos;&apos;&apos;     The changed options are applicable on any subsequent 
ExportToPDF user command or to any SaveAsPDF script execution
+&apos;&apos;&apos;     Args:
+&apos;&apos;&apos;             PDFOptions: a ScriptForge dictionary object
+&apos;&apos;&apos;     Returns:
+&apos;&apos;&apos;             True when successful
+&apos;&apos;&apos;     Examples:
+&apos;&apos;&apos;             Dim dict As Object
+&apos;&apos;&apos;             Set dict = session.GetPDFExportOptions()
+&apos;&apos;&apos;             dict.ReplaceItem(&quot;Quality&quot;, 50)
+&apos;&apos;&apos;             session.SetPDFExportOptions(dict)
+
+Dim bSetPDF As Boolean                         &apos;  Returned value
+Dim oConfig As Object                          &apos;  
com.sun.star.configuration.ConfigurationProvider
+Dim oNodePath As Object                                &apos;  
com.sun.star.beans.PropertyValue
+Dim oOptions As Object                         &apos;  configmgr.RootAccess
+Dim vOptionNames As Variant                    &apos;  Array of PDF options 
names
+Dim vOptionValues As Variant           &apos;  Array of PDF options values
+Dim oDict As Object                                    &apos;  Alias of 
PDFOptions
+Dim i As Long
+
+Const cstThisSub = &quot;Session.SetPDFExportOptions&quot;
+Const cstSubArgs = &quot;PDFOptions&quot;
+
+       If SF_Utils._ErrorHandling() Then On Local Error GoTo Catch
+       bSetPDF = False
+
+Check:
+       If SF_Utils._EnterFunction(cstThisSub, cstSubArgs) Then
+               If Not SF_Utils._Validate(PDFOptions, &quot;PDFOptions&quot;, 
V_OBJECT, , , &quot;DICTIONARY&quot;) Then GoTo Finally
+       End If
+
+Try:
+       &apos;  Get the (updatable) internal PDF options
+       Set oConfig = SF_Utils._GetUNOService(&quot;ConfigurationProvider&quot;)
+       Set oNodePath = SF_Utils._MakePropertyValue(&quot;nodepath&quot;, 
&quot;/org.openoffice.Office.Common/Filter/PDF/Export/&quot;)
+       Set oOptions = 
oConfig.createInstanceWithArguments(&quot;com.sun.star.configuration.ConfigurationUpdateAccess&quot;,
 Array(oNodePath))
+
+       &apos;  Copy the options from the ScriptForge dictionary in argument to 
property values
+       Set oDict = PDFOptions
+       oOptions.setPropertyValues(oDict.Keys, oDict.Items)
+       oOptions.commitChanges()
+       
+       bSetPDF = True
+
+Finally:
+       SetPDFExportOptions = bSetPDF
+       SF_Utils._ExitFunction(cstThisSub)
+       Exit Function
+Catch:
+       GoTo Finally
+End Function   &apos;  ScriptForge.SF_Session.SetPDFExportOptions
+
 REM 
-----------------------------------------------------------------------------
 Public Function SetProperty(Optional ByVal PropertyName As Variant _
                                                                , Optional 
ByRef Value As Variant _
diff --git a/wizards/source/scriptforge/python/scriptforge.py 
b/wizards/source/scriptforge/python/scriptforge.py
index 23b505a36ef7..714418ffd09c 100644
--- a/wizards/source/scriptforge/python/scriptforge.py
+++ b/wizards/source/scriptforge/python/scriptforge.py
@@ -1762,6 +1762,9 @@ class SFDocuments:
         def CloseDocument(self, saveask = True):
             return self.ExecMethod(self.vbMethod, 'CloseDocument', saveask)
 
+        def ExportAsPDF(self, filename, overwrite = False, pages = '', 
password = '', watermark = ''):
+            return self.ExecMethod(self.vbMethod, 'ExportAsPDF', filename, 
overwrite, pages, password, watermark)
+
         def PrintOut(self, pages = '', copies = 1):
             return self.ExecMethod(self.vbMethod, 'PrintOut', pages, copies)
 
diff --git a/wizards/source/sfdocuments/SF_Calc.xba 
b/wizards/source/sfdocuments/SF_Calc.xba
index 89afc16fc6ea..390e4b274165 100644
--- a/wizards/source/sfdocuments/SF_Calc.xba
+++ b/wizards/source/sfdocuments/SF_Calc.xba
@@ -928,7 +928,7 @@ Try:
        &apos;  Initialize sheet and range
        Set oSheet = _Component.getSheets.getByName(SheetName)
        Set oRange = _ParseAddress(Range)
-       &apos;  Create the chart and get the corresponding chart instance
+       &apos;  Create the chart and get ihe corresponding chart instance
        oSheet.getCharts.addNewByName(ChartName, oRectangle, 
Array(oRange.XCellRange.RangeAddress), ColumnHeader, RowHeader)
        Set oChart = Charts(SheetName, ChartName)
        oChart._Shape.Name = ChartName          &apos;  Both used-defined and 
internal names match ChartName
@@ -1525,6 +1525,7 @@ Public Function Methods() As Variant
                                        , &quot;DMax&quot; _
                                        , &quot;DMin&quot; _
                                        , &quot;DSum&quot; _
+                                       , &quot;ExportAsPDF&quot; _
                                        , &quot;GetColumnName&quot; _
                                        , &quot;GetFormula&quot; _
                                        , &quot;GetValue&quot; _
@@ -2425,6 +2426,16 @@ Public Function CloseDocument(Optional ByVal SaveAsk As 
Variant) As Boolean
        CloseDocument = [_Super].CloseDocument(SaveAsk)
 End Function   &apos;   SFDocuments.SF_Calc.CloseDocument
 
+REM 
-----------------------------------------------------------------------------
+Public Function ExportAsPDF(Optional ByVal FileName As Variant _
+                                                       , Optional ByVal 
Overwrite As Variant _
+                                                       , Optional ByVal Pages 
As Variant _
+                                                       , Optional ByVal 
Password As Variant _
+                                                       , Optional ByVal 
Watermark As Variant _
+                                                       ) As Boolean
+       ExportAsPDF = [_Super].ExportAsPDF(FileName, Overwrite, Pages, 
Password, Watermark)
+End Function   &apos;   SFDocuments.SF_Calc.ExportAsPDF
+
 REM 
-----------------------------------------------------------------------------
 Public Sub RunCommand(Optional ByVal Command As Variant)
        [_Super].RunCommand(Command)
@@ -3218,4 +3229,4 @@ CatchDuplicate:
 End Function   &apos;  SFDocuments.SF_Calc._ValidateSheet
 
 REM ============================================ END OF SFDOCUMENTS.SF_CALC
-</script:module>
+</script:module>
\ No newline at end of file
diff --git a/wizards/source/sfdocuments/SF_Chart.xba 
b/wizards/source/sfdocuments/SF_Chart.xba
index 357a2826488b..175fecfccaf8 100644
--- a/wizards/source/sfdocuments/SF_Chart.xba
+++ b/wizards/source/sfdocuments/SF_Chart.xba
@@ -812,4 +812,4 @@ Private Function _Repr() As String
 End Function   &apos;  SFDocuments.SF_Chart._Repr
 
 REM ============================================ END OF SFDOCUMENTS.SF_CHART
-</script:module>
+</script:module>
\ No newline at end of file
diff --git a/wizards/source/sfdocuments/SF_Document.xba 
b/wizards/source/sfdocuments/SF_Document.xba
index 40c39822d531..00aa22fc08b4 100644
--- a/wizards/source/sfdocuments/SF_Document.xba
+++ b/wizards/source/sfdocuments/SF_Document.xba
@@ -459,6 +459,102 @@ Catch:
        GoTo Finally
 End Function   &apos;   SFDocuments.SF_Document.CloseDocument
 
+REM 
-----------------------------------------------------------------------------
+Public Function ExportAsPDF(Optional ByVal FileName As Variant _
+                                                       , Optional ByVal 
Overwrite As Variant _
+                                                       , Optional ByVal Pages 
As Variant _
+                                                       , Optional ByVal 
Password As Variant _
+                                                       , Optional ByVal 
Watermark As Variant _
+                                                       ) As Boolean
+&apos;&apos;&apos;     Store the document to the given file location in PDF 
format
+&apos;&apos;&apos;     Args:
+&apos;&apos;&apos;             FileName: Identifies the file where to save. It 
must follow the SF_FileSystem.FileNaming notation
+&apos;&apos;&apos;             Overwrite: True if the destination file may be 
overwritten (default = False)
+&apos;&apos;&apos;             Pages: the pages to print as a string, like in 
the user interface. Example: &quot;1-4;10;15-18&quot;. Default = all pages
+&apos;&apos;&apos;             Password: password to open the document
+&apos;&apos;&apos;             Watermark: the text for a watermark to be drawn 
on every page of the exported PDF file
+&apos;&apos;&apos;     Returns:
+&apos;&apos;&apos;             False if the document could not be saved
+&apos;&apos;&apos;     Exceptions:
+&apos;&apos;&apos;             DOCUMENTSAVEASERROR             The destination 
has its readonly attribute set or overwriting rejected
+&apos;&apos;&apos;     Examples:
+&apos;&apos;&apos;             oDoc.ExportAsPDF(&quot;C:\Me\myDoc.pdf&quot;, 
Overwrite := True)
+
+Dim bSaved As Boolean                          &apos;  return value
+Dim oSfa As Object                                     &apos;  
com.sun.star.ucb.SimpleFileAccess
+Dim sFile As String                                    &apos;  Alias of 
FileName
+Dim sFilter As String                          &apos;  One of the pdf filter 
names
+Dim vFilterData As Variant                     &apos;  Array of 
com.sun.star.beans.PropertyValue
+Dim vProperties As Variant                     &apos;  Array of 
com.sun.star.beans.PropertyValue
+Dim FSO As Object                                      &apos;  SF_FileSystem
+Const cstThisSub = &quot;SFDocuments.Document.ExportAsPDF&quot;
+Const cstSubArgs = &quot;FileName, [Overwrite=False], 
[Pages=&quot;&quot;&quot;&quot;], [Password=&quot;&quot;&quot;&quot;], 
[Watermark=&quot;&quot;&quot;&quot;]&quot;
+
+       If ScriptForge.SF_Utils._ErrorHandling() Then On Local Error GoTo 
CatchError
+       bSaved = False
+
+Check:
+       If IsMissing(Overwrite) Or IsEmpty(Overwrite) Then Overwrite = False
+       If IsMissing(Pages) Or IsEmpty(Pages) Then Pages = &quot;&quot;
+       If IsMissing(Password) Or IsEmpty(Password) Then Password = &quot;&quot;
+       If IsMissing(Watermark) Or IsEmpty(Watermark) Then Watermark = 
&quot;&quot;
+
+       If ScriptForge.SF_Utils._EnterFunction(cstThisSub, cstSubArgs) Then
+               If Not _IsStillAlive() Then GoTo Finally
+               If Not SF_Utils._ValidateFile(FileName, &quot;FileName&quot;) 
Then GoTo Finally
+               If Not SF_Utils._Validate(Overwrite, &quot;Overwrite&quot;, 
V_BOOLEAN) Then GoTo Finally
+               If Not SF_Utils._Validate(Pages, &quot;Pages&quot;, V_STRING) 
Then GoTo Finally
+               If Not SF_Utils._Validate(Password, &quot;Password&quot;, 
V_STRING) Then GoTo Finally
+               If Not SF_Utils._Validate(Watermark, &quot;Watermark&quot;, 
V_STRING) Then GoTo Finally
+       End If
+
+       &apos;  Check destination file overwriting
+       Set FSO = CreateScriptService(&quot;FileSystem&quot;)
+       sFile = FSO._ConvertToUrl(FileName)
+       If FSO.FileExists(FileName) Then
+               If Overwrite = False Then GoTo CatchError
+               Set oSfa = 
ScriptForge.SF_Utils._GetUNOService(&quot;FileAccess&quot;)
+               If oSfa.isReadonly(sFile) Then GoTo CatchError
+       End If
+
+Try:
+       &apos;  Setup arguments
+       sFilter = LCase(_DocumentType) &amp; &quot;_pdf_Export&quot;
+       &apos;  FilterData parameters are added only if they are meaningful
+       vFilterData = Array()
+       If Len(Pages) &gt; 0 Then
+               vFilterData = ScriptForge.SF_Array.Append(vFilterData _
+                                                               , 
ScriptForge.SF_Utils._MakePropertyValue(&quot;PageRange&quot;, Pages))
+       End If
+       If Len(Password) &gt; 0 Then
+               vFilterData = ScriptForge.SF_Array.Append(vFilterData _
+                                                               , 
ScriptForge.SF_Utils._MakePropertyValue(&quot;EncryptFile&quot;, True) _
+                                                               , 
ScriptForge.SF_Utils._MakePropertyValue(&quot;DocumentOpenPassword&quot;, 
Password))
+       End If
+       If Len(Watermark) &gt; 0 Then
+               vFilterData = ScriptForge.SF_Array.Append(vFilterData _
+                                                               , 
ScriptForge.SF_Utils._MakePropertyValue(&quot;Watermark&quot;, Watermark))
+       End If
+
+       &apos;  Finalize properties and export
+       vProperties = Array( _
+                                               
ScriptForge.SF_Utils._MakePropertyValue(&quot;FilterName&quot;, sFilter) _
+                                               , 
ScriptForge.SF_Utils._MakePropertyValue(&quot;FilterData&quot;, vFilterData))
+       _Component.StoreToURL(sFile, vProperties)
+       bSaved = True
+
+Finally:
+       ExportAsPDF = bSaved
+       ScriptForge.SF_Utils._ExitFunction(cstThisSub)
+       Exit Function
+Catch:
+       GoTo Finally
+CatchError:
+       ScriptForge.SF_Exception.RaiseFatal(DOCUMENTSAVEASERROR, 
&quot;FileName&quot;, FileName, &quot;Overwrite&quot;, Overwrite _
+                                       , &quot;FilterName&quot;, &quot;PDF 
Export&quot;)
+       GoTo Finally
+End Function   &apos;   SFDocuments.SF_Document.ExportAsPDF
+
 REM 
-----------------------------------------------------------------------------
 Public Function GetProperty(Optional ByVal PropertyName As Variant) As Variant
 &apos;&apos;&apos;     Return the actual value of the given property
@@ -500,6 +596,7 @@ Public Function Methods() As Variant
        Methods = Array( _
                                        &quot;Activate&quot; _
                                        , &quot;CloseDocument&quot; _
+                                       , &quot;ExportAsPDF&quot; _
                                        , &quot;PrintOut&quot; _
                                        , &quot;RunCommand&quot; _
                                        , &quot;Save&quot; _
@@ -745,7 +842,7 @@ Try:
                                                                , 
ScriptForge.SF_Utils._MakePropertyValue(&quot;FilterOptions&quot;, 
FilterOptions) _
                                                        )
                If Len(Password) &gt; 0 Then            &apos;  Password is to 
add only if &lt;&gt; &quot;&quot; !?
-                       vProperties = ScriptForge.SF_Array.Append(vproperties _
+                       vProperties = ScriptForge.SF_Array.Append(vProperties _
                                                                , 
ScriptForge.SF_Utils._MakePropertyValue(&quot;Password&quot;, Password))
                End If
        End If
@@ -844,7 +941,7 @@ Try:
                                                                , 
ScriptForge.SF_Utils._MakePropertyValue(&quot;FilterOptions&quot;, 
FilterOptions) _
                                                        )
                If Len(Password) &gt; 0 Then            &apos;  Password is to 
add only if &lt;&gt; &quot;&quot; !?
-                       vProperties = ScriptForge.SF_Array.Append(vproperties _
+                       vProperties = ScriptForge.SF_Array.Append(vProperties _
                                                                , 
ScriptForge.SF_Utils._MakePropertyValue(&quot;Password&quot;, Password))
                End If
        End If
diff --git a/wizards/source/sfdocuments/SF_Writer.xba 
b/wizards/source/sfdocuments/SF_Writer.xba
index 7d839c7b5ba6..4acdd5c750f0 100644
--- a/wizards/source/sfdocuments/SF_Writer.xba
+++ b/wizards/source/sfdocuments/SF_Writer.xba
@@ -219,6 +219,7 @@ Public Function Methods() As Variant
        Methods = Array( _
                                        &quot;Activate&quot; _
                                        , &quot;CloseDocument&quot; _
+                                       , &quot;ExportAsPDF&quot; _
                                        , &quot;Forms&quot; _
                                        , &quot;PrintOut&quot; _
                                        , &quot;RunCommand&quot; _
@@ -491,6 +492,16 @@ Public Function CloseDocument(Optional ByVal SaveAsk As 
Variant) As Boolean
        CloseDocument = [_Super].CloseDocument(SaveAsk)
 End Function   &apos;   SFDocuments.SF_Writer.CloseDocument
 
+REM 
-----------------------------------------------------------------------------
+Public Function ExportAsPDF(Optional ByVal FileName As Variant _
+                                                       , Optional ByVal 
Overwrite As Variant _
+                                                       , Optional ByVal Pages 
As Variant _
+                                                       , Optional ByVal 
Password As Variant _
+                                                       , Optional ByVal 
Watermark As Variant _
+                                                       ) As Boolean
+       ExportAsPDF = [_Super].ExportAsPDF(FileName, Overwrite, Pages, 
Password, Watermark)
+End Function   &apos;   SFDocuments.SF_Writer.ExportAsPDF
+
 REM 
-----------------------------------------------------------------------------
 Public Sub RunCommand(Optional ByVal Command As Variant)
        [_Super].RunCommand(Command)

Reply via email to