Date: prev next · Thread: first prev next last
2013 Archives by date, by thread · List index


On 21-04-2013 14:08, Philip Rhoades wrote:
People,

When I record a macro to export to PDF with file-open password
protection, the resulting PDF does in fact have password protection.
However, if I run the previously recorded macro again, the resulting PDF
is NOT password protected!  I have found a mention of this problem with
OO but haven't had any success using the suggested solution there with
LO . .

Another solution would be to use unoconv - but the version on Fedora 18
(0.5.2) does not seem to work with password protection either (although
it is in the help info) . .

Does anyone have a WORKING macro that will export PDFs with file-open
password protection?

Thanks,

Phil.

Ok after some searching.....
i found the answer in this discussion:
http://de.openoffice.info/viewtopic.php?f=1&t=55753&start=15

Here it is :
REM  *****  BASIC  *****


Sub ExportPDF
' Call the export-routine below with optional arguments for passpharases and/or watermark-text: ' argument 1 = target file (path and PDF-filename like required from your file-system)
   '   argument 2 = file open password,
   '   argument 3 = restriction password
   '   argument 4 = watermark text
   '
   ' Examples:
      ' No special settings at all (export only):
      '   PdfExportWithFilter()
      ' Export to a special destination (no further settings):
      '   PdfExportWithFilter("D:\TESTPATH\TESTDOC.pdf")
      ' Export to a special destination with the to-open-password "test":
          PdfExportWithFilter("C:\Temp\TESTDOC.PDF", "test")
' Export using the file-manager (no destination defined) with the restriction-password "test":
      '    PdfExportWithFilter("", "", "test")
      ' Export using the file-manager with no passwords but a watermark:
      '    PdfExportWithFilter("", "", "", "THIS IS AN EXAMPLE!")
End Sub


Sub PdfExportWithFilter(Optional TargetFile As Variant, Optional OpenPassword As String, Optional PermitPassword As String, Optional WatermarkText As String)

   ' Sub "PdfExportWithFilter"
   ' Author: ejomi
   ' Date: 2012/09
   '    Arguments (all optional / can be left empty):
' TargetFile = The path and filename of the PDF-File (syntax as required from your file-system)
   '    OpenPassword   =  Passphrase for opening the ready made PDF-File
' PermitPassword = Passphrases for setting user-restrictions to the PDF-File ' WatermarkText = A text for a watermark to be drawn on every page of the exported PDF file
   ' Details of the PDF-Export specifications and filters, see:
   ' http://wiki.openoffice.org/wiki/API/Tutorials/PDF_export

' --------------------------------- I N I T -------------------------------------

   ' Define some local variables:
   Dim oDoc As Object, Dispatcher As Object
   Dim ExportFilter(33) As New com.sun.star.beans.PropertyValue
   Dim PdfProperty(3) As New com.sun.star.beans.PropertyValue
   Dim SetEncrypt As Boolean, SetRestricts As Boolean

   ' Automatically toggle the encrypt- and restriction-switches
' depending on a given passphrase for "File Open" and/or "User Permissions":
   If IsMissing(TargetFile) Then TargetFile = ""
   If IsMissing(OpenPassword) Then OpenPassword = ""
   If IsMissing(PermitPassword) Then PermitPassword = ""
   If IsMissing(WatermarkText) Then WatermarkText = ""

' The Constant "url_pdf_out" forces a filemanager before exporting the new PDF-File:
   If TargetFile = "" Then
      TargetFile = url_pdf_out
   Else
      TargetFile = ConvertToURL(TargetFile)
   Endif

   If OpenPassword <> "" Then SetEncrypt = True
   If PermitPassword <> "" Then SetRestricts = True

   ' Get access to the current writer document:
   oDoc = ThisComponent.CurrentController.Frame

   ' Initialize the UNO-Dispatcher:
   Dispatcher = createUnoService("com.sun.star.frame.DispatchHelper")

' ------------------- D I S P L A Y - S E T T I N G S ---------------------------

   ExportFilter(0).Name   = "SelectPdfVersion"
   ' Specifies the version of PDF to emit.
   '  0 = PDF 1.4 (default selection).
   '  1 = PDF/A-1 (ISO 19005-1:2005)
   ExportFilter(0).Value  = 0

   ExportFilter(1).Name   = "PDFViewSelection"
   ' Specifies the way the exported PDF will be viewed by the user.
   '  0 = The PDF will be exported with all the links external
   '      to the document treated as URI (= Default).
   '  1 = The PDF will be exported in order to be viewed
   '      through a PDF reader application only.
   '      (Valid only if not exporting to PDF/A-1,
   '      e.g. SelectPdfVersion not set to 1).
' 2 = The PDF will be exported in order to be viewed through an Internet browser, ' using the PDF plug-in provided with it. The bookmark of the URI will be rendered ' compatible with the target bookmark generated with OOo PDF Export feature
   '      (see ExportBookmarksToPDFDestination, below).
   ExportFilter(1).Value  = 0

   ExportFilter(2).Name   = "InitialView"
   ' Specifies how the PDF document should be displayed when opened:
   '  0 = Select the default viewer mode, neither outlines or thumbnails.
   '  1 = The document is opened with outline pane opened
   '  2 = The document is opened with thumbnail pane opened
   ExportFilter(2).Value  = 0

   ExportFilter(3).Name   = "CenterWindow"
   ' Specifies that the PDF viewer window is centered to the screen:
   ExportFilter(3).Value  = True

   ExportFilter(4).Name   = "ResizeWindowToInitialPage"
' Specifies that the PDF viewer window is opened full screen when the document is opened.
   ExportFilter(4).Value  = False

   ExportFilter(5).Name   = "InitialPage"
' Specifies the page on which a PDF document should be opened in the viewer application:
   ExportFilter(5).Value  = 1

   ExportFilter(6).Name   = "PageLayout"
   ' Specifies the page layout to be used when the document is opened.
   '  0= Display the pages according to the reader configuration.
   '  1= Display one page at a time.
   '  2= Display the pages in one column.
   '  3= Display the pages in two columns odd pages on the right,
   '     to have the odd pages on the left the "FirstPageOnLeft" property
   '     should be used as well.
   ExportFilter(6).Value  = 0

   ExportFilter(7).Name   = "FirstPageOnLeft"
   ' Used with the value 3 of the "PageLayout" property above.
   ' TRUE if the first page (odd) should be on the left side of the screen.
   ExportFilter(7).Value  = True

   ExportFilter(8).Name   = "IsSkipEmptyPages"
   ' Specifies that automatically inserted empty pages are suppressed.
   ' This option is active only if storing Writer documents:
   ExportFilter(8).Value  = True

   ExportFilter(9).Name   = "UseTransitionEffects"
   ' Specifies slide transitions are exported to PDF.
   ' This option is active only if storing Impress documents:
   ExportFilter(9).Value  = True

   ExportFilter(10).Name  = "Magnification"
   ' Specifies the action to be performed when the PDF document is opened.
   '  0= Opens with default zoom magnification.
   '  1= Opens magnified to fit the entire page within the window.
   '  2= Opens magnified to fit the entire page width within the window.
' 3= Opens magnified to fit the entire width of its boundig box within the window (cuts out margins).
   '  4= Opens with the zoom level specified in the Zoom property.
   ExportFilter(10).Value = 0

   ExportFilter(11).Name  = "Zoom"
   ' Specifies the zoom level a PDF document is opened with.
   ' Only valid if "Magnification" is set to "4".
   ExportFilter(11).Value = 100

   ExportFilter(12).Name  = "DisplayPDFDocumentTitle"
' Specifies that the title of the document (if present in the document properties)
   ' is displayed in the PDF viewer window title bar.
   ExportFilter(12).Value = True

   ExportFilter(13).Name  = "HideViewerMenubar"
' Specifies whether to hide the PDF viewer menubar when the document is active:
   ExportFilter(13).Value = True

   ExportFilter(14).Name  = "HideViewerToolbar"
' Specifies whether to hide the PDF viewer toolbar when the document is active:
   ExportFilter(14).Value = True

   ExportFilter(15).Name  = "HideViewerWindowControls"
' Specifies whether to hide the PDF viewer controls when the document is active:
   ExportFilter(15).Value = False

   ExportFilter(16).Name  = "OpenBookmarkLevels"
'Specifies how many bookmark levels should be opened in the reader application.
   '  -1        =  all bookmark levels are opened
   '   1 to 10  =  indicate a bookmark level (from 1 to 10)
   ExportFilter(16).Value = -1

' ------------------------ L I N K - B E H A V I O U R -------------------------

   ExportFilter(17).Name  = "ExportBookmarksToPDFDestination"
   ' Specifies that the bookmarks contained in the source file
   ' should be exported to the PDF file as Named Destination:
   ExportFilter(17).Value = False

   ExportFilter(18).Name  = "ExportLinksRelativeFsys"
   ' Specifies that the file system related hyperlinks (file:// protocol)
' present in the document will be exported as relative to the source document location:
   ExportFilter(18).Value = True

   ExportFilter(19).Name  = "ConvertOOoTargetToPDFTarget"
   ' Specifies that the target documents with .od[tpgs] extension,
' will have that extension changed to .pdf when the link is exported to PDF.
   ' The source document remains untouched.
   ExportFilter(19).Value = True

' ------------------------- P A S S W O R D - S E T S ---------------------------

   ExportFilter(20).Name  = "DocumentOpenPassword"
   ' Contains the Password that allows the user
   ' to open the PDF file (only if "EncryptFile" is set to TRUE)
   ExportFilter(20).Value = OpenPassword

   ExportFilter(21).Name  = "EncryptFile"
' If set to TRUE, it selects to encrypt the PDF document with a password. ' The PDF file can be opened only when the user enters the correct password.
   ExportFilter(21).Value = SetEncrypt


   ExportFilter(22).Name  = "PermissionPassword"
   ' Contains the Password that allows the user
' to access some permissions restricted only if "RestrictPermissions" is set to TRUE:
   ExportFilter(22).Value = PermitPassword

   ExportFilter(23).Name  = "RestrictPermissions"
   ' If set to TRUE, selects to restrict some permissions.
' The permissions can be changed only when the user enters the correct password.
   ExportFilter(23).Value = SetRestricts

' -------------------------- P E R M I S S I O N S ------------------------------

   ExportFilter(24).Name  = "EnableCopyingOfContent"
   ' Specifies that the pages and the document content
   ' can be extracted to be used in other documents (copy and paste)
   ExportFilter(24).Value = False

   ExportFilter(25).Name  = "Printing"
   ' Printing allowed from the document:
   '  0 = The document cannot be printed.
   '  1 = The document can be printed at low resolution only.
   '  2 = The document can be printed at maximum resolution.
   ExportFilter(25).Value = 1

   ExportFilter(26).Name  = "Changes"
   ' Changes allowed to the document:
   '  0 = The document cannot be changed.
   '  1 = Inserting deleting and rotating pages is allowed.
   '  2 = Filling of form field is allowed.
   '  3 = Both filling of form field and commenting is allowed.
   '  4 = All the changes of the previous selections are permitted,
   '      with the only exclusion of page extraction (copy).
   ExportFilter(26).Value = 0

   ExportFilter(27).Name  = "ExportNotes"
   ' Specifies if notes are exported to PDF:
   ExportFilter(27).Value = False

   ExportFilter(28).Name  = "ExportBookmarksToPDFDestination"
   ' Specifies if notes pages are exported to PDF.
   ' (Notes pages are available in Impress documents only).
   ExportFilter(28).Value = False

   ExportFilter(29).Name  = "ExportFormFields"
   ' Specifies whether form fields are exported as widgets
   ' or only their fixed print representation is exported.
   ExportFilter(29).Value = False

   ExportFilter(30).Name  = "EnableTextAccessForAccessibilityTools"
' Document content can be extracted to be used in accessibility applications.
   ExportFilter(30).Value = False

   ExportFilter(31).Name  = "IsAddStream"
   ' Specifies that a stream is inserted to the PDF file
   ' which contains the original document for archiving purposes.
   ' (This option is active only if the PDF Import extension is installed):
   ExportFilter(31).Value = False

   If WatermarkText <> "" Then
      ExportFilter(32).Name  = "Watermark"
' Specifies the text for a watermark to be drawn on every page of the exported PDF file.
      ExportFilter(32).Value = WatermarkText
   EndIf

' -------------------- PUT PROPERTIES TO THE PDF-DOCUMENT ------------------------

   PdfProperty(0).Name  = "URL"
   PdfProperty(0).Value = TargetFile
   PdfProperty(1).Name  = "FilterName"
   PdfProperty(1).Value = "writer_pdf_Export"
   PdfProperty(2).Name  = "FilterData"
   PdfProperty(2).Value = ExportFilter

   ' Export the customized PDF-Document:
Dispatcher.executeDispatch(oDoc, ".uno:ExportDirectToPDF", "", 0, PdfProperty())

End Sub


--
For unsubscribe instructions e-mail to: users+help@global.libreoffice.org
Problems? http://www.libreoffice.org/get-help/mailing-lists/how-to-unsubscribe/
Posting guidelines + more: http://wiki.documentfoundation.org/Netiquette
List archive: http://listarchives.libreoffice.org/global/users/
All messages sent to this list will be publicly archived and cannot be deleted

Context


Privacy Policy | Impressum (Legal Info) | Copyright information: Unless otherwise specified, all text and images on this website are licensed under the Creative Commons Attribution-Share Alike 3.0 License. This does not include the source code of LibreOffice, which is licensed under the Mozilla Public License (MPLv2). "LibreOffice" and "The Document Foundation" are registered trademarks of their corresponding registered owners or are in actual use as trademarks in one or more countries. Their respective logos and icons are also subject to international copyright laws. Use thereof is explained in our trademark policy.