Difference between revisions of "API/Tutorials/PDF export"
m (→Security: Typo) |
|||
(57 intermediate revisions by 7 users not shown) | |||
Line 1: | Line 1: | ||
+ | PDF export is an important and useful feature of {{AOo}} that can completely be automatized using {{AOo}} API, quite every aspect of exporting to PDF in the graphical user interface is also available for programming: documents can be exported directly with default settings just passing the proper filter name and an URL, every option the user can set in the GUI can also be set programmatically in the filter options, also these settings are configurable via the API (they can be accessed, changed, or reset to their default values), and even the options dialog for exporting to PDF can be used through pure API calls. | ||
+ | |||
+ | This tutorial will show how to use {{AOo}} API to export documents to PDF. The reader should have at least a basic knowledge about handling office documents (how to load and store a document), and should also know how to access {{AOo}} configuration. | ||
+ | |||
+ | This tutorial also aims to provide a full up-to-date documentation about the PDF export filter options, as these specific properties are usually not documented and only available in the core {{AOo}} source code (thus not accessible for developers aiming to work only with its API). | ||
+ | |||
+ | The sources for this tutorial can be found [https://www.openoffice.org/api/ here]. They are not intended to provide a ready-to-use PDF export application, but to give you a tool for doing your own experiments. | ||
+ | |||
+ | |||
+ | = PDF Export basics = | ||
+ | |||
+ | |||
+ | A document can be exported by saving it to a different location and using a special filter capable to write the document to the desired format. In the API, documents are storable through their interface <idl>com.sun.star.frame.XStorable</idl>, as discussed in detail in [[Documentation/DevGuide/OfficeDev/Storing Documents|Developer's Guide - Office Development - Storing Documents]]. | ||
+ | |||
+ | For exporting, besides the location (in URL notation) where the exported document is to be stored, a filter name must be passed to <code>XStorable.storeAsURL()</code> and <code>XStorable.storeToURL()</code>. The property needed for this purpose is the string argument <idlm>com.sun.star.document.MediaDescriptor:FilterName</idlm> that takes the name of a filter already known by {{AOo}}. | ||
+ | |||
+ | In the simplest case, all you need for exporting to PDF is the URL where you want to export the document and the filter name; this way, the document will be exported using the current PDF export settings (the ones used the last time, or the default values stipulated by the filter configuration, if this is first time). | ||
+ | |||
+ | The following code shows the basic procedure: | ||
+ | |||
+ | <syntaxhighlight lang="java"> | ||
+ | |||
+ | XComponent xComponent = null; | ||
+ | // create a new Writer document using a helper | ||
+ | xComponent = Helper.createNewDoc(m_xContext, "swriter"); | ||
+ | |||
+ | // access its XTextDocument interface | ||
+ | XTextDocument xTextDocument = (XTextDocument) UnoRuntime.queryInterface( | ||
+ | XTextDocument.class, xComponent); | ||
+ | |||
+ | // access the text body and set a string | ||
+ | XText xText = xTextDocument.getText(); | ||
+ | xText.setString("Simple PDF export demo."); | ||
+ | |||
+ | // XStorable to store the document | ||
+ | XStorable xStorable = (XStorable) UnoRuntime.queryInterface( | ||
+ | XStorable.class, xComponent); | ||
+ | |||
+ | // XStorable.storeToURL() expects an URL telling where to store the document | ||
+ | // and an array of PropertyValue indicating how to store it | ||
+ | |||
+ | // URL = use helper method to get the home directory | ||
+ | String sURL = Helper.getHomeDir(m_xContext) + "/Simple_PDF_EXPORT_demo.pdf"; | ||
+ | |||
+ | // Exporting to PDF consists of giving the proper | ||
+ | // filter name in the property "FilterName" | ||
+ | // With only this, the document will be exported | ||
+ | // using the existing PDF export settings | ||
+ | // (the one used the last time, or the default if the first time) | ||
+ | PropertyValue[] aMediaDescriptor = new PropertyValue[1]; | ||
+ | aMediaDescriptor[0] = new PropertyValue(); | ||
+ | aMediaDescriptor[0].Name = "FilterName"; | ||
+ | aMediaDescriptor[0].Value = "writer_pdf_Export"; | ||
+ | |||
+ | xStorable.storeToURL(sURL, aMediaDescriptor); | ||
+ | |||
+ | </syntaxhighlight> | ||
+ | |||
+ | |||
+ | As the code shows, <code>XStorable.storeToURL()</code> expects an URL as first parameter, and an array of <idl>com.sun.star.beans.PropertyValue</idl> structs as second parameter, which implements the <idl>com.sun.star.document.MediaDescriptor</idl> service, consisting of property definitions. The media descriptor is used for loading/importing and saving/exporting documents, please refer to the [[Documentation/DevGuide/OfficeDev/Handling_Documents#MediaDescriptor|Developer's Guide]] for a detailed explanation. In this case, it includes only the struct for the <idlm>com.sun.star.document.MediaDescriptor:FilterName</idlm>, indicating the name of the filter for exporting Writer documents to PDF. | ||
+ | |||
+ | You can fully customize the PDF export process by adding another <code>PropertyValue</code> struct to the MediaDescriptor array: <idlm>com.sun.star.document.MediaDescriptor:FilterData</idlm>. This parameter is used to pass properties specific for a special filter type. In the case of the PDF export filter, another array of <code>PropertyValue</code> structs is expected. | ||
+ | |||
+ | In the next section, we will describe the complete list of specific properties available at the present for the PDF export filter. | ||
= PDF Export filter data = | = PDF Export filter data = | ||
− | The following tables contain all the properties available in the PDF export filter implementation | + | The following tables contain all the properties available in the PDF export filter implementation since OpenOffice.org 2.4 up to OpenOffice.org 3.2. You must be aware of the fact that they are ''implementation specific'', so you may not find all of them in previous versions (nor expect that they remain the same in the future). |
The tables follow the order of the PDF Export dialog tab pages, so that you can easily understand their meaning just comparing them with this dialog. | The tables follow the order of the PDF Export dialog tab pages, so that you can easily understand their meaning just comparing them with this dialog. | ||
Line 8: | Line 72: | ||
Notes: | Notes: | ||
− | * The '''Description''' column contains descriptions taken mainly from the PDF Export Dialog configuration, found in <code><nowiki><</nowiki>opt/openoffice.org2. | + | * The '''Description''' column contains descriptions taken mainly from the PDF Export Dialog configuration, found in |
− | + | <code><nowiki><</nowiki>opt/openoffice.org2.X<nowiki>></nowiki>/share/registry/schema/org/openoffice/Office/Common.xcs</code> (for OOo 2.x) | |
− | + | ||
+ | <code><nowiki><</nowiki>opt/openoffice.org/basis3.X<nowiki>></nowiki>/share/registry/schema/org/openoffice/Office/Common.xcs</code> (for OpenOffice.org 3.x) | ||
+ | * The '''Type''' column refers to the UNO IDL data types (see [[Documentation/DevGuide/ProUNO/Simple Types | Developer's Guide - Professional UNO - Simple Types]]), not the {{AOo}} Registry data types (see [[Documentation/DevGuide/Config/Object Model | Developer's Guide - Configuration Management - Object Model]] and [https://wiki.openoffice.org/wiki/OOR-document-format#Registry_Object_Model OpenOffice Registry Format - Registry Object Model]). For the '''mapping''' refer to [http://util.openoffice.org/common/configuration/api_spec.html#Types%20Objects%20and%20Values Configuration API Overview - Types, Objects and Values]. | ||
+ | * The '''Default value''' column contains the default value as indicated in the PDF Export Dialog configuration mentioned above. | ||
== General properties == | == General properties == | ||
Line 24: | Line 90: | ||
|- | |- | ||
| <code>PageRange</code> | | <code>PageRange</code> | ||
− | | If this property is set, it indicates the range of pages to be printed. | + | | <span id="PageRange"></span>If this property is set, it indicates the range of pages to be printed. |
If you want to print all the pages, leave this property unset. | If you want to print all the pages, leave this property unset. | ||
Line 36: | Line 102: | ||
|- | |- | ||
| <code>Selection</code> | | <code>Selection</code> | ||
− | | An '''any''' corresponding to the current selection in the document. | + | | <span id="Selection"></span>An '''any''' corresponding to the current selection in the document. |
| <center>any</center> | | <center>any</center> | ||
| | | | ||
Line 42: | Line 108: | ||
|- | |- | ||
| <code>UseLosslessCompression</code> | | <code>UseLosslessCompression</code> | ||
− | | Specifies if graphics are exported to PDF using a lossless compression eg. '''PNG''' or if they are compressed using the '''JPEG''' format. | + | | <span id="UseLosslessCompression"></span>Specifies if graphics are exported to PDF using a lossless compression eg. '''PNG''' or if they are compressed using the '''JPEG''' format. |
| <center>boolean</center> | | <center>boolean</center> | ||
| <center>false</center> | | <center>false</center> | ||
Line 48: | Line 114: | ||
|- | |- | ||
| <code>Quality</code> | | <code>Quality</code> | ||
− | | Specifies quality of the JPG export. A higher value results in higher quality and file. | + | | <span id="Quality"></span>Specifies quality of the JPG export. A higher value results in higher quality and file. |
''Minimum inclusive value'': 1. Represents lowest value that can be used. The lower the value, the less good is the compression quality and the bigger is be the file size. | ''Minimum inclusive value'': 1. Represents lowest value that can be used. The lower the value, the less good is the compression quality and the bigger is be the file size. | ||
− | '' | + | ''Maximum inclusive value'':100. Represents highest value that can be used. The higher the value, the better is the compression quality and the smaller is the file size. |
| <center>long</center> | | <center>long</center> | ||
| <center>90</center> | | <center>90</center> | ||
Line 58: | Line 124: | ||
|- | |- | ||
| <code>ReduceImageResolution</code> | | <code>ReduceImageResolution</code> | ||
− | | Specifies if the resolution of each image is reduced to the resolution specified by the property MaxImageResolution. | + | | <span id="ReduceImageResolution"></span>Specifies if the resolution of each image is reduced to the resolution specified by the property MaxImageResolution. |
| <center>boolean</center> | | <center>boolean</center> | ||
| <center>false</center> | | <center>false</center> | ||
Line 64: | Line 130: | ||
|- | |- | ||
| <code>MaxImageResolution</code> | | <code>MaxImageResolution</code> | ||
− | | If the property ReduceImageResolution is set to true all images will be reduced to the given value in DPI. | + | | <span id="MaxImageResolution"></span>If the property ReduceImageResolution is set to true all images will be reduced to the given value in DPI. |
Posible values: | Posible values: | ||
Line 80: | Line 146: | ||
|- | |- | ||
| <code>SelectPdfVersion</code> | | <code>SelectPdfVersion</code> | ||
− | | Specifies the version of PDF to emit. | + | | <span id="SelectPdfVersion"></span>Specifies the version of PDF to emit. |
Possible values: | Possible values: | ||
Line 93: | Line 159: | ||
|- | |- | ||
| <code>UseTaggedPDF</code> | | <code>UseTaggedPDF</code> | ||
− | | Determines if PDF are created by using special tags also known as Tagged PDF. | + | | <span id="UseTaggedPDF"></span>Determines if PDF are created by using special tags also known as Tagged PDF. |
| <center>boolean</center> | | <center>boolean</center> | ||
| <center>false</center> | | <center>false</center> | ||
Line 99: | Line 165: | ||
|- | |- | ||
| <code>ExportFormFields</code> | | <code>ExportFormFields</code> | ||
− | | Specifies whether form fields are exported as widgets or only their fixed print representation is exported. | + | | <span id="ExportFormFields"></span>Specifies whether form fields are exported as widgets or only their fixed print representation is exported. |
| <center>boolean</center> | | <center>boolean</center> | ||
| <center>true</center> | | <center>true</center> | ||
Line 105: | Line 171: | ||
|- | |- | ||
| <code>FormsType</code> | | <code>FormsType</code> | ||
− | | Specifies the submitted format of a PDF form. | + | | <span id="FormsType"></span>Specifies the submitted format of a PDF form. |
Possible values: | Possible values: | ||
Line 117: | Line 183: | ||
| <center>long</center> | | <center>long</center> | ||
| <center>0</center> | | <center>0</center> | ||
+ | |||
+ | |- | ||
+ | | <code>AllowDuplicateFieldNames</code> | ||
+ | | <span id="AllowDuplicateFieldNames"></span>Specifies whether multiple form fields exported are allowed to have the same field name. | ||
+ | |||
+ | '''Since''': OOo 3.3 <nowiki>[DEV300_m65]</nowiki> (See [https://bz.apache.org/ooo/show_bug.cgi?id=103932 i103932]) | ||
+ | |||
+ | | <center>boolean</center> | ||
+ | | <center>false</center> | ||
|- | |- | ||
| <code>ExportBookmarks</code> | | <code>ExportBookmarks</code> | ||
− | | Specifies if bookmarks are exported to PDF. | + | | <span id="ExportBookmarks"></span>Specifies if bookmarks are exported to PDF. |
| <center>boolean</center> | | <center>boolean</center> | ||
| <center>true</center> | | <center>true</center> | ||
Line 126: | Line 201: | ||
|- | |- | ||
| <code>ExportNotes</code> | | <code>ExportNotes</code> | ||
− | | Specifies if notes are exported to PDF. | + | | <span id="ExportNotes"></span>Specifies if notes are exported to PDF. |
| <center>boolean</center> | | <center>boolean</center> | ||
| <center>false</center> | | <center>false</center> | ||
Line 132: | Line 207: | ||
|- | |- | ||
| <code>ExportNotesPages</code> | | <code>ExportNotesPages</code> | ||
− | | Specifies if notes pages are exported to PDF. (Notes pages are available in '''Impress''' documents only). | + | | <span id="ExportNotesPages"></span>Specifies if notes pages are exported to PDF. (Notes pages are available in '''Impress''' documents only). |
| <center>boolean</center> | | <center>boolean</center> | ||
| <center>false</center> | | <center>false</center> | ||
Line 138: | Line 213: | ||
|- | |- | ||
| <code>IsSkipEmptyPages</code> | | <code>IsSkipEmptyPages</code> | ||
− | | Specifies that automatically inserted empty pages are suppressed. This option is active only if storing Writer documents. | + | | <span id="IsSkipEmptyPages"></span>Specifies that automatically inserted empty pages are suppressed. This option is active only if storing Writer documents. |
| <center>boolean</center> | | <center>boolean</center> | ||
| <center>false</center> | | <center>false</center> | ||
|- | |- | ||
− | | <code> | + | | <code>EmbedStandardFonts</code> |
− | | Specifies | + | | <span id="EmbedStandardFonts"></span>Specifies whether to embed the 14 standard PDF fonts or not. |
+ | |||
+ | '''Since''': OOo 3.3 <nowiki>[DEV300_m78]</nowiki> (See [https://bz.apache.org/ooo/show_bug.cgi?id=87968 i87968]) | ||
+ | |||
| <center>boolean</center> | | <center>boolean</center> | ||
| <center>false</center> | | <center>false</center> | ||
− | | | + | |- |
+ | | <code>IsAddStream</code> | ||
+ | | <span id="IsAddStream"></span>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. | ||
+ | | <center>boolean</center> | ||
+ | | <center>false</center> | ||
+ | |- | ||
+ | | <code>Watermark</code> | ||
+ | | <span id="Watermark"></span>Specifies the text for a watermark to be drawn on every page of the exported PDF file. | ||
+ | | <center>any (string)</center> | ||
+ | | <center>empty</center> | ||
+ | |||
+ | |} | ||
== Initial view == | == Initial view == | ||
Line 164: | Line 254: | ||
|- | |- | ||
| <code>InitialView</code> | | <code>InitialView</code> | ||
− | | Specifies how the PDF document should be displayed when opened. | + | | <span id="InitialView"></span>Specifies how the PDF document should be displayed when opened. |
Possible values: | Possible values: | ||
Line 178: | Line 268: | ||
|- | |- | ||
| <code>InitialPage</code> | | <code>InitialPage</code> | ||
− | | Specifies the page on which a PDF document should be opened in the viewer application. | + | | <span id="InitialPage"></span>Specifies the page on which a PDF document should be opened in the viewer application. |
| <center>long</center> | | <center>long</center> | ||
| <center>1</center> | | <center>1</center> | ||
Line 184: | Line 274: | ||
|- | |- | ||
| <code>Magnification</code> | | <code>Magnification</code> | ||
− | | Specifies the action to be performed when the PDF document is opened. | + | | <span id="Magnification"></span>Specifies the action to be performed when the PDF document is opened. |
Possible values: | Possible values: | ||
Line 200: | Line 290: | ||
|- | |- | ||
| <code>Zoom</code> | | <code>Zoom</code> | ||
− | | specifies the zoom level a PDF document is opened with. Only valid if "Magnification" is set to "4". | + | | <span id="Zoom"></span>specifies the zoom level a PDF document is opened with. Only valid if "Magnification" is set to "4". |
| <center>long</center> | | <center>long</center> | ||
| <center>100</center> | | <center>100</center> | ||
Line 206: | Line 296: | ||
|- | |- | ||
| <code>PageLayout</code> | | <code>PageLayout</code> | ||
− | | Specifies the page layout to be used when the document is opened. | + | | <span id="PageLayout"></span>Specifies the page layout to be used when the document is opened. |
Possible values: | Possible values: | ||
Line 221: | Line 311: | ||
|- | |- | ||
| <code>FirstPageOnLeft</code> | | <code>FirstPageOnLeft</code> | ||
− | | 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. | + | | <span id="FirstPageOnLeft"></span>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. |
| <center>boolean</center> | | <center>boolean</center> | ||
| <center>false</center> | | <center>false</center> | ||
|} | |} | ||
− | |||
− | |||
== User interface == | == User interface == | ||
Line 241: | Line 329: | ||
|- | |- | ||
| <code>ResizeWindowToInitialPage</code> | | <code>ResizeWindowToInitialPage</code> | ||
− | | Specifies that the PDF viewer window is opened '''full screen''' when the document is opened. | + | | <span id="ResizeWindowToInitialPage"></span>Specifies that the PDF viewer window is opened '''full screen''' when the document is opened. |
| <center>boolean</center> | | <center>boolean</center> | ||
| <center>false</center> | | <center>false</center> | ||
Line 247: | Line 335: | ||
|- | |- | ||
| <code>CenterWindow</code> | | <code>CenterWindow</code> | ||
− | | Specifies that the PDF viewer window is centered to the screen when the PDF document is opened. | + | | <span id="CenterWindow"></span>Specifies that the PDF viewer window is centered to the screen when the PDF document is opened. |
| <center>boolean</center> | | <center>boolean</center> | ||
| <center>false</center> | | <center>false</center> | ||
Line 253: | Line 341: | ||
|- | |- | ||
| <code>OpenInFullScreenMode</code> | | <code>OpenInFullScreenMode</code> | ||
− | | Specifies that the PDF viewer window is opened full screen, on top of all windows. | + | | <span id="OpenInFullScreenMode"></span>Specifies that the PDF viewer window is opened full screen, on top of all windows. |
| <center>boolean</center> | | <center>boolean</center> | ||
| <center>false</center> | | <center>false</center> | ||
Line 259: | Line 347: | ||
|- | |- | ||
| <code>DisplayPDFDocumentTitle</code> | | <code>DisplayPDFDocumentTitle</code> | ||
− | | Specifies that the title of the document, if present in the document properties, is displayed in the PDF viewer window title bar. | + | | <span id="DisplayPDFDocumentTitle"></span>Specifies that the title of the document, if present in the document properties, is displayed in the PDF viewer window title bar. |
| <center>boolean</center> | | <center>boolean</center> | ||
| <center>true</center> | | <center>true</center> | ||
Line 265: | Line 353: | ||
|- | |- | ||
| <code>HideViewerMenubar</code> | | <code>HideViewerMenubar</code> | ||
− | | Specifies whether to hide the PDF viewer menubar when the document is active. | + | | <span id="HideViewerMenubar"></span>Specifies whether to hide the PDF viewer menubar when the document is active. |
| <center>boolean</center> | | <center>boolean</center> | ||
| <center>false</center> | | <center>false</center> | ||
Line 271: | Line 359: | ||
|- | |- | ||
| <code>HideViewerToolbar</code> | | <code>HideViewerToolbar</code> | ||
− | | Specifies whether to hide the PDF viewer toolbar when the document is active. | + | | <span id="HideViewerToolbar"></span>Specifies whether to hide the PDF viewer toolbar when the document is active. |
| <center>boolean</center> | | <center>boolean</center> | ||
| <center>false</center> | | <center>false</center> | ||
Line 277: | Line 365: | ||
|- | |- | ||
| <code>HideViewerWindowControls</code> | | <code>HideViewerWindowControls</code> | ||
− | | Specifies whether to hide the PDF viewer controls when the document is active. | + | | <span id="HideViewerWindowControls"></span>Specifies whether to hide the PDF viewer controls when the document is active. |
| <center>boolean</center> | | <center>boolean</center> | ||
| <center>false</center> | | <center>false</center> | ||
Line 283: | Line 371: | ||
|- | |- | ||
| <code>UseTransitionEffects</code> | | <code>UseTransitionEffects</code> | ||
− | | Specifies slide transitions are exported to PDF. This option is active only if storing '''Impress''' documents. | + | | <span id="UseTransitionEffects"></span>Specifies slide transitions are exported to PDF. This option is active only if storing '''Impress''' documents. |
| <center>boolean</center> | | <center>boolean</center> | ||
| <center>true</center> | | <center>true</center> | ||
Line 289: | Line 377: | ||
|- | |- | ||
| <code>OpenBookmarkLevels</code> | | <code>OpenBookmarkLevels</code> | ||
− | | Specifies how many bookmark levels should be opened in the reader application when the PDF gets opened. | + | | <span id="OpenBookmarkLevels"></span>Specifies how many bookmark levels should be opened in the reader application when the PDF gets opened. |
Possible values: | Possible values: | ||
Line 316: | Line 404: | ||
|- | |- | ||
| <code>ExportBookmarksToPDFDestination</code> | | <code>ExportBookmarksToPDFDestination</code> | ||
− | | Specifies that the bookmarks contained in the source | + | | <span id="ExportBookmarksToPDFDestination"></span>Specifies that the bookmarks contained in the source {{AOo}} file should be exported to the PDF file as Named Destination (see PDF 1.4 section 8.2.1). |
| <center>boolean</center> | | <center>boolean</center> | ||
| <center>false</center> | | <center>false</center> | ||
Line 322: | Line 410: | ||
|- | |- | ||
| <code>ConvertOOoTargetToPDFTarget</code> | | <code>ConvertOOoTargetToPDFTarget</code> | ||
− | | <nowiki>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.</nowiki> | + | | <span id="ConvertOOoTargetToPDFTarget"></span><nowiki>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.</nowiki> |
| <center>boolean</center> | | <center>boolean</center> | ||
| <center>false</center> | | <center>false</center> | ||
Line 328: | Line 416: | ||
|- | |- | ||
| <code>ExportLinksRelativeFsys</code> | | <code>ExportLinksRelativeFsys</code> | ||
− | | Specifies that the file system related hyperlinks (file:// protocol) present in the document will be exported as relative to the source document location. | + | | <span id="ExportLinksRelativeFsys"></span>Specifies that the file system related hyperlinks (file:// protocol) present in the document will be exported as relative to the source document location. |
| <center>boolean</center> | | <center>boolean</center> | ||
| <center>false</center> | | <center>false</center> | ||
Line 334: | Line 422: | ||
|- | |- | ||
| <code>PDFViewSelection</code> | | <code>PDFViewSelection</code> | ||
− | | Specifies the way the exported PDF will be viewed (experienced) by the user. | + | | <span id="PDFViewSelection"></span>Specifies the way the exported PDF will be viewed (experienced) by the user. |
Possible values: | Possible values: | ||
Line 362: | Line 450: | ||
|- | |- | ||
| <code>EncryptFile</code> | | <code>EncryptFile</code> | ||
− | | If true, selects to encrypt the PDF document with a password. The PDF file can be opened only when the user enters the correct password. | + | | <span id="EncryptFile"></span>If true, selects to encrypt the PDF document with a password. The PDF file can be opened only when the user enters the correct password. |
| <center>boolean</center> | | <center>boolean</center> | ||
| <center>false</center> | | <center>false</center> | ||
Line 368: | Line 456: | ||
|- | |- | ||
| <code>DocumentOpenPassword</code> | | <code>DocumentOpenPassword</code> | ||
− | | This is the password that allows the user to open the PDF file | + | | <span id="DocumentOpenPassword"></span>This is the password that allows the user to open the PDF file if "EncryptFile" is set to true. |
| <center>string</center> | | <center>string</center> | ||
| | | | ||
Line 374: | Line 462: | ||
|- | |- | ||
| <code>RestrictPermissions</code> | | <code>RestrictPermissions</code> | ||
− | | If true, selects to restrict some permissions. The permissions can be changed only when the user enters the correct password. | + | | <span id="RestrictPermissions"></span>If true, selects to restrict some permissions. The permissions can be changed only when the user enters the correct password. |
| <center>boolean</center> | | <center>boolean</center> | ||
| <center>false</center> | | <center>false</center> | ||
Line 380: | Line 468: | ||
|- | |- | ||
| <code>PermissionPassword</code> | | <code>PermissionPassword</code> | ||
− | | This is the password that allows the user to access some permissions restricted if "RestrictPermissions" is set to true. | + | | <span id="PermissionPassword"></span>This is the password that allows the user to access some permissions restricted if "RestrictPermissions" is set to true. |
| <center>string</center> | | <center>string</center> | ||
| | | | ||
Line 386: | Line 474: | ||
|- | |- | ||
| <code>Printing</code> | | <code>Printing</code> | ||
− | | Specifies what printing is allowed. | + | | <span id="Printing"></span>Specifies what printing is allowed. |
Possible values: | Possible values: | ||
Line 400: | Line 488: | ||
|- | |- | ||
| <code>Changes</code> | | <code>Changes</code> | ||
− | | Specifies the change allowed to the document. | + | | <span id="Changes"></span>Specifies the change allowed to the document. |
Possible values: | Possible values: | ||
Line 416: | Line 504: | ||
|- | |- | ||
| <code>EnableCopyingOfContent</code> | | <code>EnableCopyingOfContent</code> | ||
− | | Specifies that the pages and the document content can be extracted to be used in other documents (copy and paste). | + | | <span id="EnableCopyingOfContent"></span>Specifies that the pages and the document content can be extracted to be used in other documents (copy and paste). |
| <center>boolean</center> | | <center>boolean</center> | ||
| <center>true</center> | | <center>true</center> | ||
Line 422: | Line 510: | ||
|- | |- | ||
| <code>EnableTextAccessForAccessibilityTools</code> | | <code>EnableTextAccessForAccessibilityTools</code> | ||
− | | Specifies that the document content can be extracted to be used in accessibility applications. | + | | <span id="EnableTextAccessForAccessibilityTools"></span>Specifies that the document content can be extracted to be used in accessibility applications. |
| <center>boolean</center> | | <center>boolean</center> | ||
| <center>true</center> | | <center>true</center> | ||
|} | |} | ||
+ | |||
+ | = Filter data demo = | ||
+ | |||
+ | |||
+ | The following sample shows how to use the filer data to set some PDF export filter options. | ||
+ | |||
+ | We will create a new Writer document, insert some text in it and draw two shapes. Then we will show how to export only these two shapes, setting also some view preferences (the PDF will open in full screen mode). | ||
+ | |||
+ | <syntaxhighlight lang="java"> | ||
+ | |||
+ | // create a new Writer document using a helper | ||
+ | xComponent = Helper.createNewDoc(m_xContext, "swriter"); | ||
+ | |||
+ | // access its XTextDocument interface | ||
+ | XTextDocument xTextDocument = (XTextDocument) UnoRuntime.queryInterface( | ||
+ | XTextDocument.class, xComponent); | ||
+ | |||
+ | // draw a group of shapes | ||
+ | // so that then we can select them and export ONLY them to PDF | ||
+ | XShapeGroup xShapes = drawShapeGroup(xTextDocument); | ||
+ | |||
+ | // XStorable to store the document | ||
+ | XStorable xStorable = (XStorable) UnoRuntime.queryInterface( | ||
+ | XStorable.class, xComponent); | ||
+ | |||
+ | // XStorable.storeToURL() expects an URL telling where to store the document | ||
+ | // and an array of PropertyValue indicating how to store it | ||
+ | |||
+ | // URL = use helper method to get the home directory | ||
+ | String sURL = Helper.getHomeDir(m_xContext) + "/PDF_EXPORT_FilterData_demo.pdf"; | ||
+ | |||
+ | // Exporting to PDF consists of giving the proper | ||
+ | // filter name in the property "FilterName" | ||
+ | // With only this, the WHOLE document will be exported | ||
+ | // using the existing PDF export settings | ||
+ | // (the one used the last time, or the default if the first time) | ||
+ | PropertyValue[] aMediaDescriptor = new PropertyValue[2]; | ||
+ | aMediaDescriptor[0] = new PropertyValue(); | ||
+ | aMediaDescriptor[0].Name = "FilterName"; | ||
+ | aMediaDescriptor[0].Value = "writer_pdf_Export"; | ||
+ | |||
+ | // By adding the "FilterData" we can control what is exported and how | ||
+ | // For example, we will export ONLY the current selection | ||
+ | |||
+ | PropertyValue[] aFilterData = new PropertyValue[3]; | ||
+ | aFilterData[0] = new PropertyValue(); | ||
+ | aFilterData[0].Name = "Selection"; | ||
+ | aFilterData[0].Value = select(xTextDocument, xShapes); | ||
+ | // there was really no need to retrieve the selection | ||
+ | // we could have used the XShapeGroup reference | ||
+ | |||
+ | aFilterData[1] = new PropertyValue(); | ||
+ | aFilterData[1].Name = "DisplayPDFDocumentTitle"; | ||
+ | aFilterData[1].Value = Boolean.FALSE; | ||
+ | |||
+ | aFilterData[2] = new PropertyValue(); | ||
+ | aFilterData[2].Name = "OpenInFullScreenMode"; | ||
+ | aFilterData[2].Value = Boolean.TRUE; | ||
+ | |||
+ | aMediaDescriptor[1] = new PropertyValue(); | ||
+ | aMediaDescriptor[1].Name = "FilterData"; | ||
+ | aMediaDescriptor[1].Value = aFilterData; | ||
+ | |||
+ | xStorable.storeToURL(sURL, aMediaDescriptor); | ||
+ | |||
+ | // open the PDF with your default PDF viewer | ||
+ | // to check how it worked! | ||
+ | Helper.executeSystemCommand(m_xContext, sURL); | ||
+ | |||
+ | |||
+ | </syntaxhighlight> | ||
+ | |||
+ | To export the selection, there must be something currently selected in the document's ''view''; that is, if you "select" for example a text range by means of expanding a text cursor | ||
+ | |||
+ | <syntaxhighlight lang="java"> | ||
+ | XTextCursor xCursor = xTextDocument.getText().createTextCursor(); | ||
+ | |||
+ | // The text cursor can travel through the text | ||
+ | // as a "collapsed" text range | ||
+ | // with identical start and end as a point in text, | ||
+ | // or it can expand while it moves to contain a target string. | ||
+ | // This is controlled with the XTextCursor methods | ||
+ | // having a boolean parameter, for example XTextCursor.gotoEnd(bExpand) | ||
+ | // If bExpand is true, the cursor expands while it travels | ||
+ | |||
+ | xCursor.gotoStart(false); // go to the start | ||
+ | xCursor.gotoEnd(true); // go to the end, expanding the cursor's text range | ||
+ | |||
+ | // now the cursor's text range covers all the document's text | ||
+ | |||
+ | //... | ||
+ | |||
+ | aFilterData[0] = new PropertyValue(); | ||
+ | aFilterData[0].Name = "Selection"; | ||
+ | aFilterData[0].Value = xCursor; | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | the PDF export filter will not recognize this as a selection: the text cursor is not collapsed (start and end are not the same), but nothing is selected in the document's view. | ||
+ | |||
+ | To select an object in the document's view is really simple: just get the current <idl>com.sun.star.frame.XController</idl> from the document <idl>com.sun.star.frame.XModel</idl>, query for the <code>XController</code>'s <idl>com.sun.star.view.XSelectionSupplier</idl>,and select the object (a cursor's text, a group of shapes, etc.): | ||
+ | |||
+ | <syntaxhighlight lang="java"> | ||
+ | |||
+ | private Object select(XInterface xInterface, Object aAny){ | ||
+ | Object oSelection = null; | ||
+ | try { | ||
+ | XModel xModel = (XModel) UnoRuntime.queryInterface( | ||
+ | XModel.class, xInterface); | ||
+ | if (xModel != null){ | ||
+ | XController xController = xModel.getCurrentController(); | ||
+ | XSelectionSupplier xSelectionSupplier = | ||
+ | (XSelectionSupplier) UnoRuntime.queryInterface( | ||
+ | XSelectionSupplier.class, xController); | ||
+ | xSelectionSupplier.select(aAny); | ||
+ | // there is really no need to retrieve the selection | ||
+ | oSelection = xSelectionSupplier.getSelection(); | ||
+ | } | ||
+ | } finally { | ||
+ | return oSelection; | ||
+ | } | ||
+ | } | ||
+ | |||
+ | </syntaxhighlight> | ||
= PDF Export Configuration = | = PDF Export Configuration = | ||
− | |||
− | The following is a list of the properties available in the | + | Most of the PDF export filter properties seen in the tables above are stored in the {{AOo}} configuration. |
+ | |||
+ | When the user clicks the toolbar icon to export the current document (command <code>".uno:ExportDirectToPDF"</code>), s/he will only see the "Export" dialog, not the "PDF Options" dialog. In this case, the filter will export reading the settings the user defined the last time in the PDF Options dialog (or the default settings if the dialog has never been used before). | ||
+ | |||
+ | If the user chooses the command in the "File" menu (<code>".uno:ExportToPDF"</code>), s/he will see two dialogs in succession: first the PDF Options dialog, where s/he chooses the export settings; then the "Export" dialog, where s/he enters a file name. The settings entered int the PDF Options dialog are made persistent. | ||
+ | |||
+ | The PDF export filter configuration is stored in the path <code>org.openoffice.Office/Common/PDF/Export</code>, its schema can be found in the office installation inside | ||
+ | |||
+ | <code><nowiki><</nowiki>opt/openoffice.org2.X<nowiki>></nowiki>/share/registry/schema/org/openoffice/Office/Common.xcs</code> (for OOo 2.x) | ||
+ | |||
+ | <code><nowiki><</nowiki>opt/openoffice.org/basis3.X<nowiki>></nowiki>/share/registry/schema/org/openoffice/Office/Common.xcs</code> (for OOo 3.x) | ||
+ | |||
+ | Not all properties passed to the PDF filter are stored in the registry (as it makes no sense, for example to make persistent the page range to be exported, or the passwords for protecting the document). The following is a list of the properties not stored in the OpenOffice registry: | ||
+ | |||
+ | # <code>DocumentOpenPassword</code> | ||
+ | # <code>EncryptFile</code> | ||
+ | # <code>PageRange</code> | ||
+ | # <code>PermissionPassword</code> | ||
+ | # <code>RestrictPermissions</code> | ||
+ | # <code>Selection</code> | ||
+ | # <code>Watermark</code> | ||
+ | |||
+ | The following property is present, but deprecated (that's why we didn't include in the tables above): | ||
+ | |||
+ | # <code>CompressMode</code> <nowiki>[deprecated]</nowiki> | ||
+ | |||
+ | In the next sections, we will show how to access this configuration, change it, and restore it to its default values. The reader should be familiar with {{AOo}} configuration API, as explained in [[Documentation/DevGuide/Config/Configuration Management|Developer's Guide – Configuration Management]]. | ||
+ | |||
+ | |||
+ | ==Accessing the configuration== | ||
+ | |||
+ | |||
+ | To access the PDF export configuration, we must follow two steps, as explained in [[Documentation/DevGuide/Config/Configuration_Data_Sources|Developer's Guide – Configuration Management - Configuration Data Sources]]: | ||
+ | |||
+ | # connect to the {{AOo}} registry that stores the user configuration data by creating an instance of a <idl>com.sun.star.configuration.ConfigurationProvider</idl> at the global service manager | ||
+ | # ask the <code>ConfigurationProvider</code> to create an access object for the nodepath <code>"/org.openoffice.Office/Common/PDF/Export"</code>, using <idlm>com.sun.star.lang.XMultiServiceFactory:createInstanceWithArguments</idlm>. Depending on if we want a read-only view of the configuration data, or we want it to be modifiable, we must pass to <code>createInstanceWithArguments()</code> the name of the service <idl>com.sun.star.configuration.ConfigurationAccess</idl>, for read-only access, or <idl>com.sun.star.configuration.ConfigurationUpdateAccess</idl>, for updatable access. | ||
+ | |||
+ | |||
+ | To create a '''read-only''' view on the PDF export filter configuration data in the user layer, the service <idl>com.sun.star.configuration.ConfigurationAccess</idl> is requested to the <idls>com.sun.star.configuration.ConfigurationProvider</idls>: | ||
+ | |||
+ | <syntaxhighlight lang="java"> | ||
+ | |||
+ | public static Object getPDFConfigAccess(XComponentContext xContext){ | ||
+ | |||
+ | Object oPDFExportConfig = null; | ||
+ | try { | ||
+ | XMultiComponentFactory xMCF = xContext.getServiceManager(); | ||
+ | |||
+ | // instantiate the ConfigurationProvider at the ServiceManager | ||
+ | Object oConfigProvider = xMCF.createInstanceWithContext( | ||
+ | "com.sun.star.configuration.ConfigurationProvider", xContext); | ||
+ | |||
+ | // query its XMultiServiceFactory: | ||
+ | // the ConfigurationProvider serves as a FACTORY for objects | ||
+ | // that provide access to a subset of the configuration. | ||
+ | XMultiServiceFactory oConfigProvMSF = | ||
+ | (XMultiServiceFactory) UnoRuntime.queryInterface( | ||
+ | XMultiServiceFactory.class, oConfigProvider); | ||
+ | |||
+ | // createInstanceWithArguments(sServiceSpecifier, aArguments) | ||
+ | // first parameter is the service specifier, in this case for read-only access: | ||
+ | String sServiceName = "com.sun.star.configuration.ConfigurationAccess"; | ||
+ | |||
+ | // createInstanceWithArguments(sServiceSpecifier, aArguments) | ||
+ | // second parameter takes a sequence< any > for the arguments, | ||
+ | // mapped in Java to Object[] | ||
+ | Object[] args = new Object[1]; | ||
+ | |||
+ | // The arguments passed to createInstanceWithArguments() | ||
+ | // in parameter aArguments specify the view of the configuration | ||
+ | // that should be created. | ||
+ | // Each element of the argument sequence should be a | ||
+ | // PropertyValue or a NamedValue. | ||
+ | |||
+ | // The "nodepath" argument contains the absolute path to | ||
+ | // the element of the configuration we want to create a view for. | ||
+ | NamedValue aNodepath= new NamedValue(); | ||
+ | aNodepath.Name = "nodepath"; | ||
+ | aNodepath.Value = "/org.openoffice.Office.Common/Filter/PDF/Export/"; | ||
+ | |||
+ | args[0] = aNodepath; | ||
+ | |||
+ | // create the view | ||
+ | oPDFExportConfig = oConfigProvMSF.createInstanceWithArguments( | ||
+ | sServiceName, args); | ||
+ | |||
+ | } catch (Exception ex) { | ||
+ | ex.printStackTrace(); | ||
+ | } finally { | ||
+ | return oPDFExportConfig; | ||
+ | } | ||
+ | } | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | |||
+ | The method for creating an '''updatable''' view is quite the same as the above, we only have to change the service specifier, so that <idl>com.sun.star.configuration.ConfigurationUpdateAccess</idl> is requested. In this case, there are other additional creation parameters available that control the caching behavior of the configuration management component, refer to [[Documentation/DevGuide/Config/Using_a_Data_Source|Developer's Guide - Configuration Mamagement - Using a Data Source]]. In the following sample, we use the defaults: | ||
+ | |||
+ | <syntaxhighlight lang="java"> | ||
+ | public static Object getPDFConfigUpdateAccess(XComponentContext xContext){ | ||
+ | |||
+ | Object oPDFExportConfig = null; | ||
+ | try { | ||
+ | XMultiComponentFactory xMCF = xContext.getServiceManager(); | ||
+ | |||
+ | // instantiate the ConfigurationProvider at the ServiceManager | ||
+ | Object oConfigProvider = xMCF.createInstanceWithContext( | ||
+ | "com.sun.star.configuration.ConfigurationProvider", xContext); | ||
+ | |||
+ | // query its XMultiServiceFactory: | ||
+ | // the ConfigurationProvider serves as a FACTORY for objects | ||
+ | // that provide access to a subset of the configuration. | ||
+ | XMultiServiceFactory oConfigProvMSF = | ||
+ | (XMultiServiceFactory) UnoRuntime.queryInterface( | ||
+ | XMultiServiceFactory.class, oConfigProvider); | ||
+ | |||
+ | // createInstanceWithArguments(sServiceSpecifier, aArguments) | ||
+ | // first parameter is the service specifier, in this case for UPDATE access: | ||
+ | String sServiceName = "com.sun.star.configuration.ConfigurationUpdateAccess"; | ||
+ | |||
+ | // createInstanceWithArguments(sServiceSpecifier, aArguments) | ||
+ | // second parameter takes a sequence< any > for the arguments, | ||
+ | // mapped in Java to Object[] | ||
+ | Object[] args = new Object[1]; | ||
+ | |||
+ | // The "nodepath" argument contains the absolute path to | ||
+ | // the element of the configuration we want to create a view for. | ||
+ | NamedValue aNodepath= new NamedValue(); | ||
+ | aNodepath.Name = "nodepath"; | ||
+ | aNodepath.Value = "/org.openoffice.Office.Common/Filter/PDF/Export/"; | ||
+ | |||
+ | args[0] = aNodepath; | ||
+ | |||
+ | // create the view | ||
+ | oPDFExportConfig = oConfigProvMSF.createInstanceWithArguments( | ||
+ | sServiceName, args); | ||
+ | |||
+ | } catch (Exception ex) { | ||
+ | ex.printStackTrace(); | ||
+ | } finally { | ||
+ | return oPDFExportConfig; | ||
+ | } | ||
+ | } | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | |||
+ | Both methods return in the Java sample a <code>java.lang.Object</code> (use <idl>com.sun.star.uno.XInterface</idl> in C++, and <code>Object</code> in {{AOo}} Basic), as the view implements different interfaces you must query according to your needs. | ||
+ | |||
+ | ==Read-only access== | ||
+ | |||
+ | The read-only view of the PDF export filter configuration we accessed with our method, supports the following services: | ||
+ | |||
+ | # <idl>com.sun.star.configuration.ConfigurationAccess</idl> | ||
+ | # <idl>com.sun.star.configuration.AccessRootElement</idl> | ||
+ | # <idl>com.sun.star.configuration.GroupAccess</idl> | ||
+ | # <idl>com.sun.star.configuration.HierarchyAccess</idl> | ||
+ | # <idl>com.sun.star.configuration.HierarchyElement</idl> | ||
+ | # <idl>com.sun.star.configuration.PropertyHierarchy</idl> | ||
+ | |||
+ | To access the PDF export filter properties we will query <idl>com.sun.star.beans.XPropertySet</idl>, and use its methods <idlm>com.sun.star.beans.XPropertySet:getPropertyValue</idlm> and <idlm>com.sun.star.beans.XPropertySet:getPropertySetInfo</idlm>. Notice that any attempt to invoke <idlm>com.sun.star.beans.XPropertySet:setPropertyValue</idlm> will throw an exception, as this is a read-only view. | ||
+ | |||
+ | <syntaxhighlight lang="java"> | ||
+ | public void run(){ | ||
+ | |||
+ | Object oConfig = getPDFConfigAccess(m_xContext); | ||
+ | |||
+ | if (oConfig == null ) { | ||
+ | System.out.println("Impossible to access the PDF configuration"); | ||
+ | return; | ||
+ | } | ||
+ | |||
+ | XPropertySet xPropertySet = (XPropertySet) UnoRuntime.queryInterface( | ||
+ | XPropertySet.class, oConfig); | ||
+ | |||
+ | XPropertySetInfo xPropertySetInfo = xPropertySet.getPropertySetInfo(); | ||
+ | |||
+ | Property[] properties = xPropertySetInfo.getProperties(); | ||
+ | |||
+ | System.out.println("\nProperties and values\n"); | ||
+ | |||
+ | for (Property property : properties) { | ||
+ | String sValue = ""; | ||
+ | try { | ||
+ | Object aValue = xPropertySet.getPropertyValue( property.Name ); | ||
+ | sValue = String.valueOf( aValue ); | ||
+ | } catch (Exception ex) { | ||
+ | ex.printStackTrace(); | ||
+ | } | ||
+ | System.out.println(property.Name + " =\t" + sValue); | ||
+ | } | ||
+ | } | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | ==Update access== | ||
+ | |||
+ | The '''updatable''' view of the PDF export filter configuration we accessed with our method, supports the following services: | ||
+ | |||
+ | # <idl>com.sun.star.configuration.ConfigurationAccess</idl> | ||
+ | # <idl>com.sun.star.configuration.ConfigurationUpdateAccess</idl> | ||
+ | # <idl>com.sun.star.configuration.AccessRootElement</idl> | ||
+ | # <idl>com.sun.star.configuration.UpdateRootElement</idl> | ||
+ | # <idl>com.sun.star.configuration.GroupAccess</idl> | ||
+ | # <idl>com.sun.star.configuration.GroupUpdate</idl> | ||
+ | # <idl>com.sun.star.configuration.HierarchyAccess</idl> | ||
+ | # <idl>com.sun.star.configuration.HierarchyElement</idl> | ||
+ | # <idl>com.sun.star.configuration.PropertyHierarchy</idl> | ||
+ | |||
+ | To change the configuration properties we proceed as above: query <idl>com.sun.star.beans.XPropertySet</idl>, but in this case we are able to invoke <idlm>com.sun.star.beans.XPropertySet:setPropertyValue</idlm>. Note that for better performance, you should try to use <idlm>com.sun.star.beans.XMultiPropertySet:setPropertyValues</idlm> instead. | ||
+ | |||
+ | <syntaxhighlight lang="java"> | ||
+ | public void run() { | ||
+ | |||
+ | Object oConfig = getPDFConfigUpdateAccess(m_xContext); | ||
+ | |||
+ | if (oConfig == null) { | ||
+ | System.out.println("Impossible to access the PDF configuration"); | ||
+ | return; | ||
+ | } | ||
+ | |||
+ | XPropertySet xPropertySet = (XPropertySet) UnoRuntime.queryInterface( | ||
+ | XPropertySet.class, oConfig); | ||
+ | |||
+ | // for better performance use XMultiPropertySet | ||
+ | XMultiPropertySet xMultiPropertySet = | ||
+ | (XMultiPropertySet) UnoRuntime.queryInterface( | ||
+ | XMultiPropertySet.class, oConfig); | ||
+ | |||
+ | try { | ||
+ | // change some properties | ||
+ | |||
+ | /** | ||
+ | * The first property specifies that the resolution of each image | ||
+ | * must be reduced to the resolution specified by the second property | ||
+ | * The value is given in DPI. | ||
+ | */ | ||
+ | xPropertySet.setPropertyValue("ReduceImageResolution", Boolean.TRUE); | ||
+ | xPropertySet.setPropertyValue("MaxImageResolution", new Integer(600)); | ||
+ | |||
+ | /** | ||
+ | * When exporting a document to PDF, the default is PDF 1.4 | ||
+ | * Since OOo 2.4 the PDF/A-1a is also supported. | ||
+ | * PDF/A-1a is an ISO 19005-1:2005 International Standard. | ||
+ | * | ||
+ | * NOTE that some other options WILL NOT be available if this format | ||
+ | * is selected (UseTaggedPDF, ExportFormFileds, FormsType, | ||
+ | * all security options) | ||
+ | */ | ||
+ | xPropertySet.setPropertyValue("SelectPdfVersion", new Integer(1)); | ||
+ | |||
+ | /** | ||
+ | * "Magnification" specifies the action to be performed | ||
+ | * when the PDF document is opened. | ||
+ | * A value of 4 opens with the zoom level specified in the | ||
+ | * "Zoom" property. | ||
+ | */ | ||
+ | xPropertySet.setPropertyValue("Magnification", new Integer(4)); | ||
+ | xPropertySet.setPropertyValue("Zoom", new Integer(60)); | ||
+ | |||
+ | // When using XMultiPropertySet:setPropertyValues(), | ||
+ | // properties must be alphabetically sorted! | ||
+ | String[] aProperties = { "CenterWindow", "DisplayPDFDocumentTitle", | ||
+ | "InitialView", "PageLayout" }; | ||
+ | Object[] aValues = { Boolean.TRUE, Boolean.FALSE, | ||
+ | new Integer(2), new Integer(1)}; | ||
+ | |||
+ | xMultiPropertySet.setPropertyValues(aProperties, aValues); | ||
+ | |||
+ | // commit the changes, otherwise they will be lost! | ||
+ | XChangesBatch xChangesBatch = (XChangesBatch) UnoRuntime.queryInterface( | ||
+ | XChangesBatch.class, oConfig); | ||
+ | |||
+ | xChangesBatch.commitChanges(); | ||
+ | |||
+ | } catch (UnknownPropertyException ex) { | ||
+ | ex.printStackTrace(); | ||
+ | } catch (PropertyVetoException ex) { | ||
+ | ex.printStackTrace(); | ||
+ | } catch (IllegalArgumentException ex) { | ||
+ | ex.printStackTrace(); | ||
+ | } catch (WrappedTargetException ex) { | ||
+ | ex.printStackTrace(); | ||
+ | } | ||
+ | } | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | ==Resetting the configuration== | ||
+ | |||
+ | |||
+ | Setting the configuration to its default values is very simple, just query the <idls>com.sun.star.configuration.ConfigurationUpdateAccess</idls>'s <idl>com.sun.star.beans.XMultiPropertyStates</idl>, and use its method <idlm>com.sun.star.beans.XMultiPropertyStates:setAllPropertiesToDefault</idlm>: | ||
+ | |||
+ | <syntaxhighlight lang="java"> | ||
+ | // get an updatable view | ||
+ | Object oConfig = ConfigUpdateAccess.getPDFConfigUpdateAccess(m_xContext); | ||
+ | |||
+ | if (oConfig == null) { | ||
+ | System.out.println("Impossible to access the PDF configuration"); | ||
+ | return; | ||
+ | } | ||
+ | |||
+ | XMultiPropertyStates xMultiPropertyStates = | ||
+ | (XMultiPropertyStates) UnoRuntime.queryInterface( | ||
+ | XMultiPropertyStates.class, oConfig); | ||
+ | try { | ||
+ | // set all the properties to default values | ||
+ | xMultiPropertyStates.setAllPropertiesToDefault(); | ||
+ | |||
+ | //you must commit the changes, if not they will be lost | ||
+ | XChangesBatch xChangesBatch = (XChangesBatch) UnoRuntime.queryInterface( | ||
+ | XChangesBatch.class, xMultiPropertyStates); | ||
+ | |||
+ | xChangesBatch.commitChanges(); | ||
+ | |||
+ | } catch (WrappedTargetException ex) { | ||
+ | ex.printStackTrace(); | ||
+ | } | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | =PDF Export Dialog= | ||
+ | |||
+ | |||
+ | ==How to use it from Java== | ||
+ | |||
+ | <syntaxhighlight lang="java"> | ||
+ | |||
+ | import com.sun.star.beans.PropertyValue; | ||
+ | import com.sun.star.beans.XPropertyAccess; | ||
+ | import com.sun.star.io.IOException; | ||
+ | import com.sun.star.uno.XComponentContext; | ||
+ | import com.sun.star.comp.helper.Bootstrap; | ||
+ | import com.sun.star.document.XExporter; | ||
+ | import com.sun.star.frame.XComponentLoader; | ||
+ | import com.sun.star.frame.XStorable; | ||
+ | import com.sun.star.lang.XComponent; | ||
+ | import com.sun.star.lang.XMultiComponentFactory; | ||
+ | import com.sun.star.ui.dialogs.ExecutableDialogResults; | ||
+ | import com.sun.star.ui.dialogs.XExecutableDialog; | ||
+ | import com.sun.star.uno.AnyConverter; | ||
+ | import com.sun.star.uno.UnoRuntime; | ||
+ | import com.sun.star.util.CloseVetoException; | ||
+ | import com.sun.star.util.XCloseable; | ||
+ | import com.sun.star.util.XStringSubstitution; | ||
+ | |||
+ | public class PDFExportDialog { | ||
+ | |||
+ | private final XComponentContext m_xContext; | ||
+ | |||
+ | public PDFExportDialog(XComponentContext xContext){ | ||
+ | m_xContext = xContext; | ||
+ | } | ||
+ | |||
+ | public static void main(String[] args) { | ||
+ | try { | ||
+ | // get the remote office component context | ||
+ | XComponentContext xContext = Bootstrap.bootstrap(); | ||
+ | |||
+ | PDFExportDialog demo = new PDFExportDialog(xContext); | ||
+ | demo.run(); | ||
+ | |||
+ | } catch (java.lang.Exception e){ | ||
+ | e.printStackTrace(); | ||
+ | } | ||
+ | finally { | ||
+ | System.exit( 0 ); | ||
+ | } | ||
+ | } | ||
+ | |||
+ | public void run() { | ||
+ | |||
+ | XMultiComponentFactory xMCF = m_xContext.getServiceManager(); | ||
+ | |||
+ | Object oPDFDialog = null; | ||
+ | |||
+ | try { | ||
+ | oPDFDialog = xMCF.createInstanceWithContext( | ||
+ | "com.sun.star.document.PDFDialog", m_xContext); | ||
+ | } catch (com.sun.star.uno.Exception ex) { | ||
+ | ex.printStackTrace(); | ||
+ | } | ||
+ | |||
+ | // get all interface references | ||
+ | XExecutableDialog xExecutableDialog = | ||
+ | (XExecutableDialog) UnoRuntime.queryInterface( | ||
+ | XExecutableDialog.class, oPDFDialog); | ||
+ | |||
+ | XExporter xExporter = (XExporter) UnoRuntime.queryInterface( | ||
+ | XExporter.class, xExecutableDialog); | ||
+ | |||
+ | XPropertyAccess xPropertyAccess = | ||
+ | (XPropertyAccess) UnoRuntime.queryInterface( | ||
+ | XPropertyAccess.class, xExporter); | ||
+ | |||
+ | // check them all | ||
+ | if (xExecutableDialog == null || | ||
+ | xExporter == null || xPropertyAccess == null) { | ||
+ | System.out.println("something went wrong with the PDFDialog!"); | ||
+ | return; | ||
+ | } | ||
+ | |||
+ | XComponent xComponent = null; | ||
+ | try { | ||
+ | // change the following to test with different doc. types | ||
+ | // but remember to change the filter name below | ||
+ | xComponent = createNewDoc(m_xContext, "swriter"); | ||
+ | |||
+ | // set the source document | ||
+ | xExporter.setSourceDocument(xComponent); | ||
+ | |||
+ | // set the title | ||
+ | xExecutableDialog.setTitle("Demo Export PDF Dialog OOo API"); | ||
+ | |||
+ | // execute the dialog | ||
+ | int nResult = xExecutableDialog.execute(); | ||
+ | |||
+ | if (nResult != ExecutableDialogResults.OK) { | ||
+ | System.out.println("you didn't press OK! so do nothing..."); | ||
+ | return; | ||
+ | } | ||
+ | |||
+ | // get the options the user has choosen | ||
+ | PropertyValue[] aFilterData = null; | ||
+ | |||
+ | PropertyValue[] aPropertyValues = xPropertyAccess.getPropertyValues(); | ||
+ | for (int i = 0; i < aPropertyValues.length; i++) { | ||
+ | PropertyValue propertyValue = aPropertyValues[i]; | ||
+ | if (propertyValue.Name.equals("FilterData")) { | ||
+ | try { | ||
+ | aFilterData = (PropertyValue[]) AnyConverter.toObject( | ||
+ | PropertyValue[].class, propertyValue.Value); | ||
+ | |||
+ | } catch (com.sun.star.lang.IllegalArgumentException ex) { | ||
+ | ex.printStackTrace(); | ||
+ | } | ||
+ | break; | ||
+ | } | ||
+ | } | ||
+ | |||
+ | if (aFilterData != null) { | ||
+ | // print the properties and their values | ||
+ | printPropertyValues(aFilterData); | ||
+ | |||
+ | // finally export! | ||
+ | // location where to export the doc. | ||
+ | String sURL = getHomeDir(m_xContext) + "/PDF_EXPORT_DIALOG_demo.pdf"; | ||
+ | // choose the proper filter | ||
+ | String sFilter = "writer_pdf_Export"; | ||
+ | |||
+ | boolean bExport = doExport(xComponent, sURL, sFilter, aFilterData); | ||
+ | |||
+ | System.out.println( (bExport) ? "\nExported!" : | ||
+ | "\nCouldn't export the document!" ); | ||
+ | } | ||
+ | |||
+ | } catch (java.lang.Exception ex) { | ||
+ | ex.printStackTrace(); | ||
+ | } finally { | ||
+ | if (xComponent != null) { | ||
+ | try { | ||
+ | XCloseable xCloseable = (XCloseable) UnoRuntime.queryInterface( | ||
+ | XCloseable.class, xComponent); | ||
+ | xCloseable.close(true); | ||
+ | } catch (CloseVetoException ex) { | ||
+ | ex.printStackTrace(); | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | |||
+ | private boolean doExport( | ||
+ | XComponent xComponent, | ||
+ | String aURL, | ||
+ | String sFilterName, | ||
+ | PropertyValue[] aFilterData) | ||
+ | { | ||
+ | XStorable xStorable = (XStorable) UnoRuntime.queryInterface( | ||
+ | XStorable.class, xComponent); | ||
+ | if (xStorable == null) { | ||
+ | return false; | ||
+ | } else { | ||
+ | try { | ||
+ | PropertyValue[] aMediaDescriptor = new PropertyValue[2]; | ||
+ | |||
+ | aMediaDescriptor[0] = new PropertyValue(); | ||
+ | aMediaDescriptor[0].Name = "FilterName"; | ||
+ | aMediaDescriptor[0].Value = sFilterName; | ||
+ | |||
+ | aMediaDescriptor[1] = new PropertyValue(); | ||
+ | aMediaDescriptor[1].Name = "FilterData"; | ||
+ | aMediaDescriptor[1].Value = aFilterData; | ||
+ | |||
+ | xStorable.storeToURL(aURL, aMediaDescriptor); | ||
+ | return true; | ||
+ | |||
+ | } catch (IOException ex) { | ||
+ | ex.printStackTrace(); | ||
+ | } | ||
+ | } | ||
+ | return false; | ||
+ | } | ||
+ | |||
+ | public static void printPropertyValues(PropertyValue[] aPropertyValues) | ||
+ | { | ||
+ | System.out.println("\nProperties and values\n"); | ||
+ | |||
+ | for (PropertyValue property : aPropertyValues) { | ||
+ | String sValue = ""; | ||
+ | try { | ||
+ | sValue = String.valueOf(property.Value); | ||
+ | } catch (Exception ex) { | ||
+ | ex.printStackTrace(); | ||
+ | } | ||
+ | System.out.println(property.Name + " = " + sValue); | ||
+ | } | ||
+ | } | ||
+ | |||
+ | |||
+ | //************************************************************************* | ||
+ | |||
+ | public static XComponent loadComponent( | ||
+ | XComponentContext xContext, | ||
+ | String sURL, | ||
+ | PropertyValue[] aMediaDescriptor) | ||
+ | { | ||
+ | XComponent xComp = null; | ||
+ | try { | ||
+ | XComponentLoader xComponentLoader = (XComponentLoader) UnoRuntime.queryInterface( | ||
+ | XComponentLoader.class, | ||
+ | xContext.getServiceManager().createInstanceWithContext( | ||
+ | "com.sun.star.frame.Desktop", xContext)); | ||
+ | xComp = xComponentLoader.loadComponentFromURL( | ||
+ | sURL, "_default", 0, aMediaDescriptor); | ||
+ | } catch (Exception ex) { | ||
+ | ex.printStackTrace(); | ||
+ | } | ||
+ | return xComp; | ||
+ | } | ||
+ | |||
+ | public static XComponent createNewDoc( | ||
+ | XComponentContext xContext, | ||
+ | String sDocType, | ||
+ | PropertyValue[] aMediaDescriptor) | ||
+ | { | ||
+ | XComponent xComp = null; | ||
+ | try { | ||
+ | xComp = loadComponent(xContext, "private:factory/" + | ||
+ | sDocType, aMediaDescriptor); | ||
+ | } catch (Exception ex) { | ||
+ | ex.printStackTrace(); | ||
+ | } | ||
+ | return xComp; | ||
+ | } | ||
+ | |||
+ | public static XComponent createNewDoc( | ||
+ | XComponentContext xContext, | ||
+ | String sDocType) | ||
+ | { | ||
+ | XComponent xComp = null; | ||
+ | try { | ||
+ | xComp = createNewDoc(xContext, sDocType, new PropertyValue[0]); | ||
+ | } catch (Exception ex) { | ||
+ | ex.printStackTrace(); | ||
+ | } | ||
+ | return xComp; | ||
+ | } | ||
+ | |||
+ | public static XComponent createNewHiddenDoc( | ||
+ | XComponentContext xContext, | ||
+ | String sDocType) | ||
+ | { | ||
+ | XComponent xComp = null; | ||
+ | try { | ||
+ | PropertyValue[] aMediaDescriptor = new PropertyValue[1]; | ||
+ | aMediaDescriptor[0] = new PropertyValue(); | ||
+ | aMediaDescriptor[0].Name = "Hidden"; | ||
+ | aMediaDescriptor[0].Value = Boolean.TRUE; | ||
+ | |||
+ | xComp = createNewDoc(xContext, sDocType, aMediaDescriptor); | ||
+ | |||
+ | } catch (Exception ex) { | ||
+ | ex.printStackTrace(); | ||
+ | } | ||
+ | return xComp; | ||
+ | } | ||
+ | |||
+ | //************************************************************************ | ||
+ | //************************************************************************* | ||
+ | |||
+ | public static String getHomeDir(XComponentContext xContext) { | ||
+ | return getPathSubstitution(xContext, "$(home)"); | ||
+ | } | ||
+ | |||
+ | public static String getWorkDir(XComponentContext xContext) { | ||
+ | return getPathSubstitution(xContext, "$(work)"); | ||
+ | } | ||
+ | |||
+ | public static String getUserDir(XComponentContext xContext) { | ||
+ | return getPathSubstitution(xContext, "$(user)"); | ||
+ | } | ||
+ | |||
+ | /** | ||
+ | * | ||
+ | * com.sun.star.util.PathSubstitution | ||
+ | * | ||
+ | * $(inst) Installation path of the Office. | ||
+ | * $(prog) Program path of the Office. | ||
+ | * $(user) The user installation directory. | ||
+ | * $(work) The work directory of the user. | ||
+ | * Under Windows this would be the "MyDocuments" subdirectory. | ||
+ | * Under Unix this would be the home-directory | ||
+ | * $(home) The home directory of the user. | ||
+ | * Under Unix this would be the home- directory. | ||
+ | * Under Windows this would be the "Documents and Settings\ " subdirectory. | ||
+ | * $(temp) The current temporary directory. | ||
+ | * $(path) The value of PATH environment variable. | ||
+ | * $(lang) The country code used by the Office, like | ||
+ | * 01=english, | ||
+ | * 49=german. | ||
+ | * $(langid) The language code used by the Office, like | ||
+ | * 0x0009=english, | ||
+ | * 0x0409=english us. | ||
+ | * $(vlang) The language used by the Office as a string. Like | ||
+ | * "german" for a german Office. | ||
+ | * | ||
+ | */ | ||
+ | public static String getPathSubstitution( | ||
+ | XComponentContext xContext, String aVariable) | ||
+ | { | ||
+ | String variableSubst = null; | ||
+ | try { | ||
+ | XStringSubstitution xStringSubstitution = getPathSubstitution(xContext); | ||
+ | variableSubst = xStringSubstitution.getSubstituteVariableValue(aVariable); | ||
+ | |||
+ | } catch (com.sun.star.container.NoSuchElementException e) { | ||
+ | e.printStackTrace(); | ||
+ | } catch (com.sun.star.uno.Exception ex) { | ||
+ | ex.printStackTrace(); | ||
+ | } finally { | ||
+ | return variableSubst; | ||
+ | } | ||
+ | } | ||
+ | |||
+ | public static String getPathSubstitution( | ||
+ | XComponentContext xContext, String aText, boolean bSubstRequired) | ||
+ | { | ||
+ | String variableSubst = null; | ||
+ | try { | ||
+ | XStringSubstitution xStringSubstitution = getPathSubstitution(xContext); | ||
+ | variableSubst = xStringSubstitution.substituteVariables( | ||
+ | aText, bSubstRequired); | ||
+ | |||
+ | } catch (com.sun.star.container.NoSuchElementException e) { | ||
+ | e.printStackTrace(); | ||
+ | } catch (Exception ex) { | ||
+ | ex.printStackTrace(); | ||
+ | } finally { | ||
+ | return variableSubst; | ||
+ | } | ||
+ | } | ||
+ | |||
+ | public static XStringSubstitution getPathSubstitution( | ||
+ | XComponentContext xContext) | ||
+ | { | ||
+ | XStringSubstitution xPathSubstitution = null; | ||
+ | XMultiComponentFactory xMCF = xContext.getServiceManager(); | ||
+ | try { | ||
+ | xPathSubstitution = (XStringSubstitution) UnoRuntime.queryInterface( | ||
+ | XStringSubstitution.class, | ||
+ | xMCF.createInstanceWithContext( | ||
+ | "com.sun.star.util.PathSubstitution", xContext)); | ||
+ | } catch (com.sun.star.uno.Exception ex) { | ||
+ | ex.printStackTrace(); | ||
+ | } finally { | ||
+ | return xPathSubstitution; | ||
+ | } | ||
+ | } | ||
+ | |||
+ | } | ||
+ | |||
+ | |||
+ | </syntaxhighlight> | ||
+ | |||
+ | ==How to use it from C++== | ||
+ | |||
+ | ToDo | ||
+ | |||
+ | |||
+ | ==How to use it from OOo Basic== | ||
− | + | ToDo | |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | |||
− | + | {{PDL1}} | |
− | + | [[Category:API]] | |
− | + | [[Category:Tutorial]] | |
− | + | ||
− | + | ||
− | + |
Latest revision as of 16:55, 18 August 2023
PDF export is an important and useful feature of Apache OpenOffice that can completely be automatized using Apache OpenOffice API, quite every aspect of exporting to PDF in the graphical user interface is also available for programming: documents can be exported directly with default settings just passing the proper filter name and an URL, every option the user can set in the GUI can also be set programmatically in the filter options, also these settings are configurable via the API (they can be accessed, changed, or reset to their default values), and even the options dialog for exporting to PDF can be used through pure API calls.
This tutorial will show how to use Apache OpenOffice API to export documents to PDF. The reader should have at least a basic knowledge about handling office documents (how to load and store a document), and should also know how to access Apache OpenOffice configuration.
This tutorial also aims to provide a full up-to-date documentation about the PDF export filter options, as these specific properties are usually not documented and only available in the core Apache OpenOffice source code (thus not accessible for developers aiming to work only with its API).
The sources for this tutorial can be found here. They are not intended to provide a ready-to-use PDF export application, but to give you a tool for doing your own experiments.
Contents
PDF Export basics
A document can be exported by saving it to a different location and using a special filter capable to write the document to the desired format. In the API, documents are storable through their interface com.sun.star.frame.XStorable, as discussed in detail in Developer's Guide - Office Development - Storing Documents.
For exporting, besides the location (in URL notation) where the exported document is to be stored, a filter name must be passed to XStorable.storeAsURL()
and XStorable.storeToURL()
. The property needed for this purpose is the string argument FilterName that takes the name of a filter already known by Apache OpenOffice.
In the simplest case, all you need for exporting to PDF is the URL where you want to export the document and the filter name; this way, the document will be exported using the current PDF export settings (the ones used the last time, or the default values stipulated by the filter configuration, if this is first time).
The following code shows the basic procedure:
XComponent xComponent = null; // create a new Writer document using a helper xComponent = Helper.createNewDoc(m_xContext, "swriter"); // access its XTextDocument interface XTextDocument xTextDocument = (XTextDocument) UnoRuntime.queryInterface( XTextDocument.class, xComponent); // access the text body and set a string XText xText = xTextDocument.getText(); xText.setString("Simple PDF export demo."); // XStorable to store the document XStorable xStorable = (XStorable) UnoRuntime.queryInterface( XStorable.class, xComponent); // XStorable.storeToURL() expects an URL telling where to store the document // and an array of PropertyValue indicating how to store it // URL = use helper method to get the home directory String sURL = Helper.getHomeDir(m_xContext) + "/Simple_PDF_EXPORT_demo.pdf"; // Exporting to PDF consists of giving the proper // filter name in the property "FilterName" // With only this, the document will be exported // using the existing PDF export settings // (the one used the last time, or the default if the first time) PropertyValue[] aMediaDescriptor = new PropertyValue[1]; aMediaDescriptor[0] = new PropertyValue(); aMediaDescriptor[0].Name = "FilterName"; aMediaDescriptor[0].Value = "writer_pdf_Export"; xStorable.storeToURL(sURL, aMediaDescriptor);
As the code shows, XStorable.storeToURL()
expects an URL as first parameter, and an array of com.sun.star.beans.PropertyValue structs as second parameter, which implements the com.sun.star.document.MediaDescriptor service, consisting of property definitions. The media descriptor is used for loading/importing and saving/exporting documents, please refer to the Developer's Guide for a detailed explanation. In this case, it includes only the struct for the FilterName, indicating the name of the filter for exporting Writer documents to PDF.
You can fully customize the PDF export process by adding another PropertyValue
struct to the MediaDescriptor array: FilterData. This parameter is used to pass properties specific for a special filter type. In the case of the PDF export filter, another array of PropertyValue
structs is expected.
In the next section, we will describe the complete list of specific properties available at the present for the PDF export filter.
PDF Export filter data
The following tables contain all the properties available in the PDF export filter implementation since OpenOffice.org 2.4 up to OpenOffice.org 3.2. You must be aware of the fact that they are implementation specific, so you may not find all of them in previous versions (nor expect that they remain the same in the future).
The tables follow the order of the PDF Export dialog tab pages, so that you can easily understand their meaning just comparing them with this dialog.
Notes:
- The Description column contains descriptions taken mainly from the PDF Export Dialog configuration, found in
<opt/openoffice.org2.X>/share/registry/schema/org/openoffice/Office/Common.xcs
(for OOo 2.x)
<opt/openoffice.org/basis3.X>/share/registry/schema/org/openoffice/Office/Common.xcs
(for OpenOffice.org 3.x)
- The Type column refers to the UNO IDL data types (see Developer's Guide - Professional UNO - Simple Types), not the Apache OpenOffice Registry data types (see Developer's Guide - Configuration Management - Object Model and OpenOffice Registry Format - Registry Object Model). For the mapping refer to Configuration API Overview - Types, Objects and Values.
- The Default value column contains the default value as indicated in the PDF Export Dialog configuration mentioned above.
General properties
|
|
|
|
PageRange
|
If this property is set, it indicates the range of pages to be printed.
If you want to print all the pages, leave this property unset. If you want to export a selection, leave this property unset, setting only the property Selection. |
|
|
Selection
|
An any corresponding to the current selection in the document. | |
|
UseLosslessCompression
|
Specifies if graphics are exported to PDF using a lossless compression eg. PNG or if they are compressed using the JPEG format. | |
|
Quality
|
Specifies quality of the JPG export. A higher value results in higher quality and file.
Minimum inclusive value: 1. Represents lowest value that can be used. The lower the value, the less good is the compression quality and the bigger is be the file size. Maximum inclusive value:100. Represents highest value that can be used. The higher the value, the better is the compression quality and the smaller is the file size. |
|
|
ReduceImageResolution
|
Specifies if the resolution of each image is reduced to the resolution specified by the property MaxImageResolution. | |
|
MaxImageResolution
|
If the property ReduceImageResolution is set to true all images will be reduced to the given value in DPI.
Posible values:
|
|
|
SelectPdfVersion
|
Specifies the version of PDF to emit.
Possible values:
|
|
|
UseTaggedPDF
|
Determines if PDF are created by using special tags also known as Tagged PDF. | |
|
ExportFormFields
|
Specifies whether form fields are exported as widgets or only their fixed print representation is exported. | |
|
FormsType
|
Specifies the submitted format of a PDF form.
Possible values:
|
|
|
AllowDuplicateFieldNames
|
Specifies whether multiple form fields exported are allowed to have the same field name.
Since: OOo 3.3 [DEV300_m65] (See i103932) |
|
|
ExportBookmarks
|
Specifies if bookmarks are exported to PDF. | |
|
ExportNotes
|
Specifies if notes are exported to PDF. | |
|
ExportNotesPages
|
Specifies if notes pages are exported to PDF. (Notes pages are available in Impress documents only). | |
|
IsSkipEmptyPages
|
Specifies that automatically inserted empty pages are suppressed. This option is active only if storing Writer documents. | |
|
EmbedStandardFonts
|
Specifies whether to embed the 14 standard PDF fonts or not.
Since: OOo 3.3 [DEV300_m78] (See i87968) |
|
|
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. |
|
|
Watermark
|
Specifies the text for a watermark to be drawn on every page of the exported PDF file. | |
|
Initial view
|
|
|
|
---|---|---|---|
InitialView
|
Specifies how the PDF document should be displayed when opened.
Possible values:
|
|
|
InitialPage
|
Specifies the page on which a PDF document should be opened in the viewer application. | |
|
Magnification
|
Specifies the action to be performed when the PDF document is opened.
Possible values:
|
|
|
Zoom
|
specifies the zoom level a PDF document is opened with. Only valid if "Magnification" is set to "4". | |
|
PageLayout
|
Specifies the page layout to be used when the document is opened.
Possible values:
|
|
|
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. | |
|
User interface
|
|
|
|
---|---|---|---|
ResizeWindowToInitialPage
|
Specifies that the PDF viewer window is opened full screen when the document is opened. | |
|
CenterWindow
|
Specifies that the PDF viewer window is centered to the screen when the PDF document is opened. | |
|
OpenInFullScreenMode
|
Specifies that the PDF viewer window is opened full screen, on top of all windows. | |
|
DisplayPDFDocumentTitle
|
Specifies that the title of the document, if present in the document properties, is displayed in the PDF viewer window title bar. | |
|
HideViewerMenubar
|
Specifies whether to hide the PDF viewer menubar when the document is active. | |
|
HideViewerToolbar
|
Specifies whether to hide the PDF viewer toolbar when the document is active. | |
|
HideViewerWindowControls
|
Specifies whether to hide the PDF viewer controls when the document is active. | |
|
UseTransitionEffects
|
Specifies slide transitions are exported to PDF. This option is active only if storing Impress documents. | |
|
OpenBookmarkLevels
|
Specifies how many bookmark levels should be opened in the reader application when the PDF gets opened.
Possible values:
|
|
|
Links
|
|
|
|
---|---|---|---|
ExportBookmarksToPDFDestination
|
Specifies that the bookmarks contained in the source Apache OpenOffice file should be exported to the PDF file as Named Destination (see PDF 1.4 section 8.2.1). | |
|
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. | |
|
ExportLinksRelativeFsys
|
Specifies that the file system related hyperlinks (file:// protocol) present in the document will be exported as relative to the source document location. | |
|
PDFViewSelection
|
Specifies the way the exported PDF will be viewed (experienced) by the user.
Possible values:
|
|
|
Security
|
|
|
|
---|---|---|---|
EncryptFile
|
If true, selects to encrypt the PDF document with a password. The PDF file can be opened only when the user enters the correct password. | |
|
DocumentOpenPassword
|
This is the password that allows the user to open the PDF file if "EncryptFile" is set to true. | |
|
RestrictPermissions
|
If true, selects to restrict some permissions. The permissions can be changed only when the user enters the correct password. | |
|
PermissionPassword
|
This is the password that allows the user to access some permissions restricted if "RestrictPermissions" is set to true. | |
|
Printing
|
Specifies what printing is allowed.
Possible values:
|
|
|
Changes
|
Specifies the change allowed to the document.
Possible values:
|
|
|
EnableCopyingOfContent
|
Specifies that the pages and the document content can be extracted to be used in other documents (copy and paste). | |
|
EnableTextAccessForAccessibilityTools
|
Specifies that the document content can be extracted to be used in accessibility applications. | |
|
Filter data demo
The following sample shows how to use the filer data to set some PDF export filter options.
We will create a new Writer document, insert some text in it and draw two shapes. Then we will show how to export only these two shapes, setting also some view preferences (the PDF will open in full screen mode).
// create a new Writer document using a helper xComponent = Helper.createNewDoc(m_xContext, "swriter"); // access its XTextDocument interface XTextDocument xTextDocument = (XTextDocument) UnoRuntime.queryInterface( XTextDocument.class, xComponent); // draw a group of shapes // so that then we can select them and export ONLY them to PDF XShapeGroup xShapes = drawShapeGroup(xTextDocument); // XStorable to store the document XStorable xStorable = (XStorable) UnoRuntime.queryInterface( XStorable.class, xComponent); // XStorable.storeToURL() expects an URL telling where to store the document // and an array of PropertyValue indicating how to store it // URL = use helper method to get the home directory String sURL = Helper.getHomeDir(m_xContext) + "/PDF_EXPORT_FilterData_demo.pdf"; // Exporting to PDF consists of giving the proper // filter name in the property "FilterName" // With only this, the WHOLE document will be exported // using the existing PDF export settings // (the one used the last time, or the default if the first time) PropertyValue[] aMediaDescriptor = new PropertyValue[2]; aMediaDescriptor[0] = new PropertyValue(); aMediaDescriptor[0].Name = "FilterName"; aMediaDescriptor[0].Value = "writer_pdf_Export"; // By adding the "FilterData" we can control what is exported and how // For example, we will export ONLY the current selection PropertyValue[] aFilterData = new PropertyValue[3]; aFilterData[0] = new PropertyValue(); aFilterData[0].Name = "Selection"; aFilterData[0].Value = select(xTextDocument, xShapes); // there was really no need to retrieve the selection // we could have used the XShapeGroup reference aFilterData[1] = new PropertyValue(); aFilterData[1].Name = "DisplayPDFDocumentTitle"; aFilterData[1].Value = Boolean.FALSE; aFilterData[2] = new PropertyValue(); aFilterData[2].Name = "OpenInFullScreenMode"; aFilterData[2].Value = Boolean.TRUE; aMediaDescriptor[1] = new PropertyValue(); aMediaDescriptor[1].Name = "FilterData"; aMediaDescriptor[1].Value = aFilterData; xStorable.storeToURL(sURL, aMediaDescriptor); // open the PDF with your default PDF viewer // to check how it worked! Helper.executeSystemCommand(m_xContext, sURL);
To export the selection, there must be something currently selected in the document's view; that is, if you "select" for example a text range by means of expanding a text cursor
XTextCursor xCursor = xTextDocument.getText().createTextCursor(); // The text cursor can travel through the text // as a "collapsed" text range // with identical start and end as a point in text, // or it can expand while it moves to contain a target string. // This is controlled with the XTextCursor methods // having a boolean parameter, for example XTextCursor.gotoEnd(bExpand) // If bExpand is true, the cursor expands while it travels xCursor.gotoStart(false); // go to the start xCursor.gotoEnd(true); // go to the end, expanding the cursor's text range // now the cursor's text range covers all the document's text //... aFilterData[0] = new PropertyValue(); aFilterData[0].Name = "Selection"; aFilterData[0].Value = xCursor;
the PDF export filter will not recognize this as a selection: the text cursor is not collapsed (start and end are not the same), but nothing is selected in the document's view.
To select an object in the document's view is really simple: just get the current com.sun.star.frame.XController from the document com.sun.star.frame.XModel, query for the XController
's com.sun.star.view.XSelectionSupplier,and select the object (a cursor's text, a group of shapes, etc.):
private Object select(XInterface xInterface, Object aAny){ Object oSelection = null; try { XModel xModel = (XModel) UnoRuntime.queryInterface( XModel.class, xInterface); if (xModel != null){ XController xController = xModel.getCurrentController(); XSelectionSupplier xSelectionSupplier = (XSelectionSupplier) UnoRuntime.queryInterface( XSelectionSupplier.class, xController); xSelectionSupplier.select(aAny); // there is really no need to retrieve the selection oSelection = xSelectionSupplier.getSelection(); } } finally { return oSelection; } }
PDF Export Configuration
Most of the PDF export filter properties seen in the tables above are stored in the Apache OpenOffice configuration.
When the user clicks the toolbar icon to export the current document (command ".uno:ExportDirectToPDF"
), s/he will only see the "Export" dialog, not the "PDF Options" dialog. In this case, the filter will export reading the settings the user defined the last time in the PDF Options dialog (or the default settings if the dialog has never been used before).
If the user chooses the command in the "File" menu (".uno:ExportToPDF"
), s/he will see two dialogs in succession: first the PDF Options dialog, where s/he chooses the export settings; then the "Export" dialog, where s/he enters a file name. The settings entered int the PDF Options dialog are made persistent.
The PDF export filter configuration is stored in the path org.openoffice.Office/Common/PDF/Export
, its schema can be found in the office installation inside
<opt/openoffice.org2.X>/share/registry/schema/org/openoffice/Office/Common.xcs
(for OOo 2.x)
<opt/openoffice.org/basis3.X>/share/registry/schema/org/openoffice/Office/Common.xcs
(for OOo 3.x)
Not all properties passed to the PDF filter are stored in the registry (as it makes no sense, for example to make persistent the page range to be exported, or the passwords for protecting the document). The following is a list of the properties not stored in the OpenOffice registry:
-
DocumentOpenPassword
-
EncryptFile
-
PageRange
-
PermissionPassword
-
RestrictPermissions
-
Selection
-
Watermark
The following property is present, but deprecated (that's why we didn't include in the tables above):
-
CompressMode
[deprecated]
In the next sections, we will show how to access this configuration, change it, and restore it to its default values. The reader should be familiar with Apache OpenOffice configuration API, as explained in Developer's Guide – Configuration Management.
Accessing the configuration
To access the PDF export configuration, we must follow two steps, as explained in Developer's Guide – Configuration Management - Configuration Data Sources:
- connect to the Apache OpenOffice registry that stores the user configuration data by creating an instance of a com.sun.star.configuration.ConfigurationProvider at the global service manager
- ask the
ConfigurationProvider
to create an access object for the nodepath"/org.openoffice.Office/Common/PDF/Export"
, using createInstanceWithArguments. Depending on if we want a read-only view of the configuration data, or we want it to be modifiable, we must pass tocreateInstanceWithArguments()
the name of the service com.sun.star.configuration.ConfigurationAccess, for read-only access, or com.sun.star.configuration.ConfigurationUpdateAccess, for updatable access.
To create a read-only view on the PDF export filter configuration data in the user layer, the service com.sun.star.configuration.ConfigurationAccess is requested to the ConfigurationProvider:
public static Object getPDFConfigAccess(XComponentContext xContext){ Object oPDFExportConfig = null; try { XMultiComponentFactory xMCF = xContext.getServiceManager(); // instantiate the ConfigurationProvider at the ServiceManager Object oConfigProvider = xMCF.createInstanceWithContext( "com.sun.star.configuration.ConfigurationProvider", xContext); // query its XMultiServiceFactory: // the ConfigurationProvider serves as a FACTORY for objects // that provide access to a subset of the configuration. XMultiServiceFactory oConfigProvMSF = (XMultiServiceFactory) UnoRuntime.queryInterface( XMultiServiceFactory.class, oConfigProvider); // createInstanceWithArguments(sServiceSpecifier, aArguments) // first parameter is the service specifier, in this case for read-only access: String sServiceName = "com.sun.star.configuration.ConfigurationAccess"; // createInstanceWithArguments(sServiceSpecifier, aArguments) // second parameter takes a sequence< any > for the arguments, // mapped in Java to Object[] Object[] args = new Object[1]; // The arguments passed to createInstanceWithArguments() // in parameter aArguments specify the view of the configuration // that should be created. // Each element of the argument sequence should be a // PropertyValue or a NamedValue. // The "nodepath" argument contains the absolute path to // the element of the configuration we want to create a view for. NamedValue aNodepath= new NamedValue(); aNodepath.Name = "nodepath"; aNodepath.Value = "/org.openoffice.Office.Common/Filter/PDF/Export/"; args[0] = aNodepath; // create the view oPDFExportConfig = oConfigProvMSF.createInstanceWithArguments( sServiceName, args); } catch (Exception ex) { ex.printStackTrace(); } finally { return oPDFExportConfig; } }
The method for creating an updatable view is quite the same as the above, we only have to change the service specifier, so that com.sun.star.configuration.ConfigurationUpdateAccess is requested. In this case, there are other additional creation parameters available that control the caching behavior of the configuration management component, refer to Developer's Guide - Configuration Mamagement - Using a Data Source. In the following sample, we use the defaults:
public static Object getPDFConfigUpdateAccess(XComponentContext xContext){ Object oPDFExportConfig = null; try { XMultiComponentFactory xMCF = xContext.getServiceManager(); // instantiate the ConfigurationProvider at the ServiceManager Object oConfigProvider = xMCF.createInstanceWithContext( "com.sun.star.configuration.ConfigurationProvider", xContext); // query its XMultiServiceFactory: // the ConfigurationProvider serves as a FACTORY for objects // that provide access to a subset of the configuration. XMultiServiceFactory oConfigProvMSF = (XMultiServiceFactory) UnoRuntime.queryInterface( XMultiServiceFactory.class, oConfigProvider); // createInstanceWithArguments(sServiceSpecifier, aArguments) // first parameter is the service specifier, in this case for UPDATE access: String sServiceName = "com.sun.star.configuration.ConfigurationUpdateAccess"; // createInstanceWithArguments(sServiceSpecifier, aArguments) // second parameter takes a sequence< any > for the arguments, // mapped in Java to Object[] Object[] args = new Object[1]; // The "nodepath" argument contains the absolute path to // the element of the configuration we want to create a view for. NamedValue aNodepath= new NamedValue(); aNodepath.Name = "nodepath"; aNodepath.Value = "/org.openoffice.Office.Common/Filter/PDF/Export/"; args[0] = aNodepath; // create the view oPDFExportConfig = oConfigProvMSF.createInstanceWithArguments( sServiceName, args); } catch (Exception ex) { ex.printStackTrace(); } finally { return oPDFExportConfig; } }
Both methods return in the Java sample a java.lang.Object
(use com.sun.star.uno.XInterface in C++, and Object
in Apache OpenOffice Basic), as the view implements different interfaces you must query according to your needs.
Read-only access
The read-only view of the PDF export filter configuration we accessed with our method, supports the following services:
- com.sun.star.configuration.ConfigurationAccess
- com.sun.star.configuration.AccessRootElement
- com.sun.star.configuration.GroupAccess
- com.sun.star.configuration.HierarchyAccess
- com.sun.star.configuration.HierarchyElement
- com.sun.star.configuration.PropertyHierarchy
To access the PDF export filter properties we will query com.sun.star.beans.XPropertySet, and use its methods getPropertyValue and getPropertySetInfo. Notice that any attempt to invoke setPropertyValue will throw an exception, as this is a read-only view.
public void run(){ Object oConfig = getPDFConfigAccess(m_xContext); if (oConfig == null ) { System.out.println("Impossible to access the PDF configuration"); return; } XPropertySet xPropertySet = (XPropertySet) UnoRuntime.queryInterface( XPropertySet.class, oConfig); XPropertySetInfo xPropertySetInfo = xPropertySet.getPropertySetInfo(); Property[] properties = xPropertySetInfo.getProperties(); System.out.println("\nProperties and values\n"); for (Property property : properties) { String sValue = ""; try { Object aValue = xPropertySet.getPropertyValue( property.Name ); sValue = String.valueOf( aValue ); } catch (Exception ex) { ex.printStackTrace(); } System.out.println(property.Name + " =\t" + sValue); } }
Update access
The updatable view of the PDF export filter configuration we accessed with our method, supports the following services:
- com.sun.star.configuration.ConfigurationAccess
- com.sun.star.configuration.ConfigurationUpdateAccess
- com.sun.star.configuration.AccessRootElement
- com.sun.star.configuration.UpdateRootElement
- com.sun.star.configuration.GroupAccess
- com.sun.star.configuration.GroupUpdate
- com.sun.star.configuration.HierarchyAccess
- com.sun.star.configuration.HierarchyElement
- com.sun.star.configuration.PropertyHierarchy
To change the configuration properties we proceed as above: query com.sun.star.beans.XPropertySet, but in this case we are able to invoke setPropertyValue. Note that for better performance, you should try to use setPropertyValues instead.
public void run() { Object oConfig = getPDFConfigUpdateAccess(m_xContext); if (oConfig == null) { System.out.println("Impossible to access the PDF configuration"); return; } XPropertySet xPropertySet = (XPropertySet) UnoRuntime.queryInterface( XPropertySet.class, oConfig); // for better performance use XMultiPropertySet XMultiPropertySet xMultiPropertySet = (XMultiPropertySet) UnoRuntime.queryInterface( XMultiPropertySet.class, oConfig); try { // change some properties /** * The first property specifies that the resolution of each image * must be reduced to the resolution specified by the second property * The value is given in DPI. */ xPropertySet.setPropertyValue("ReduceImageResolution", Boolean.TRUE); xPropertySet.setPropertyValue("MaxImageResolution", new Integer(600)); /** * When exporting a document to PDF, the default is PDF 1.4 * Since OOo 2.4 the PDF/A-1a is also supported. * PDF/A-1a is an ISO 19005-1:2005 International Standard. * * NOTE that some other options WILL NOT be available if this format * is selected (UseTaggedPDF, ExportFormFileds, FormsType, * all security options) */ xPropertySet.setPropertyValue("SelectPdfVersion", new Integer(1)); /** * "Magnification" specifies the action to be performed * when the PDF document is opened. * A value of 4 opens with the zoom level specified in the * "Zoom" property. */ xPropertySet.setPropertyValue("Magnification", new Integer(4)); xPropertySet.setPropertyValue("Zoom", new Integer(60)); // When using XMultiPropertySet:setPropertyValues(), // properties must be alphabetically sorted! String[] aProperties = { "CenterWindow", "DisplayPDFDocumentTitle", "InitialView", "PageLayout" }; Object[] aValues = { Boolean.TRUE, Boolean.FALSE, new Integer(2), new Integer(1)}; xMultiPropertySet.setPropertyValues(aProperties, aValues); // commit the changes, otherwise they will be lost! XChangesBatch xChangesBatch = (XChangesBatch) UnoRuntime.queryInterface( XChangesBatch.class, oConfig); xChangesBatch.commitChanges(); } catch (UnknownPropertyException ex) { ex.printStackTrace(); } catch (PropertyVetoException ex) { ex.printStackTrace(); } catch (IllegalArgumentException ex) { ex.printStackTrace(); } catch (WrappedTargetException ex) { ex.printStackTrace(); } }
Resetting the configuration
Setting the configuration to its default values is very simple, just query the ConfigurationUpdateAccess's com.sun.star.beans.XMultiPropertyStates, and use its method setAllPropertiesToDefault:
// get an updatable view Object oConfig = ConfigUpdateAccess.getPDFConfigUpdateAccess(m_xContext); if (oConfig == null) { System.out.println("Impossible to access the PDF configuration"); return; } XMultiPropertyStates xMultiPropertyStates = (XMultiPropertyStates) UnoRuntime.queryInterface( XMultiPropertyStates.class, oConfig); try { // set all the properties to default values xMultiPropertyStates.setAllPropertiesToDefault(); //you must commit the changes, if not they will be lost XChangesBatch xChangesBatch = (XChangesBatch) UnoRuntime.queryInterface( XChangesBatch.class, xMultiPropertyStates); xChangesBatch.commitChanges(); } catch (WrappedTargetException ex) { ex.printStackTrace(); }
PDF Export Dialog
How to use it from Java
import com.sun.star.beans.PropertyValue; import com.sun.star.beans.XPropertyAccess; import com.sun.star.io.IOException; import com.sun.star.uno.XComponentContext; import com.sun.star.comp.helper.Bootstrap; import com.sun.star.document.XExporter; import com.sun.star.frame.XComponentLoader; import com.sun.star.frame.XStorable; import com.sun.star.lang.XComponent; import com.sun.star.lang.XMultiComponentFactory; import com.sun.star.ui.dialogs.ExecutableDialogResults; import com.sun.star.ui.dialogs.XExecutableDialog; import com.sun.star.uno.AnyConverter; import com.sun.star.uno.UnoRuntime; import com.sun.star.util.CloseVetoException; import com.sun.star.util.XCloseable; import com.sun.star.util.XStringSubstitution; public class PDFExportDialog { private final XComponentContext m_xContext; public PDFExportDialog(XComponentContext xContext){ m_xContext = xContext; } public static void main(String[] args) { try { // get the remote office component context XComponentContext xContext = Bootstrap.bootstrap(); PDFExportDialog demo = new PDFExportDialog(xContext); demo.run(); } catch (java.lang.Exception e){ e.printStackTrace(); } finally { System.exit( 0 ); } } public void run() { XMultiComponentFactory xMCF = m_xContext.getServiceManager(); Object oPDFDialog = null; try { oPDFDialog = xMCF.createInstanceWithContext( "com.sun.star.document.PDFDialog", m_xContext); } catch (com.sun.star.uno.Exception ex) { ex.printStackTrace(); } // get all interface references XExecutableDialog xExecutableDialog = (XExecutableDialog) UnoRuntime.queryInterface( XExecutableDialog.class, oPDFDialog); XExporter xExporter = (XExporter) UnoRuntime.queryInterface( XExporter.class, xExecutableDialog); XPropertyAccess xPropertyAccess = (XPropertyAccess) UnoRuntime.queryInterface( XPropertyAccess.class, xExporter); // check them all if (xExecutableDialog == null || xExporter == null || xPropertyAccess == null) { System.out.println("something went wrong with the PDFDialog!"); return; } XComponent xComponent = null; try { // change the following to test with different doc. types // but remember to change the filter name below xComponent = createNewDoc(m_xContext, "swriter"); // set the source document xExporter.setSourceDocument(xComponent); // set the title xExecutableDialog.setTitle("Demo Export PDF Dialog OOo API"); // execute the dialog int nResult = xExecutableDialog.execute(); if (nResult != ExecutableDialogResults.OK) { System.out.println("you didn't press OK! so do nothing..."); return; } // get the options the user has choosen PropertyValue[] aFilterData = null; PropertyValue[] aPropertyValues = xPropertyAccess.getPropertyValues(); for (int i = 0; i < aPropertyValues.length; i++) { PropertyValue propertyValue = aPropertyValues[i]; if (propertyValue.Name.equals("FilterData")) { try { aFilterData = (PropertyValue[]) AnyConverter.toObject( PropertyValue[].class, propertyValue.Value); } catch (com.sun.star.lang.IllegalArgumentException ex) { ex.printStackTrace(); } break; } } if (aFilterData != null) { // print the properties and their values printPropertyValues(aFilterData); // finally export! // location where to export the doc. String sURL = getHomeDir(m_xContext) + "/PDF_EXPORT_DIALOG_demo.pdf"; // choose the proper filter String sFilter = "writer_pdf_Export"; boolean bExport = doExport(xComponent, sURL, sFilter, aFilterData); System.out.println( (bExport) ? "\nExported!" : "\nCouldn't export the document!" ); } } catch (java.lang.Exception ex) { ex.printStackTrace(); } finally { if (xComponent != null) { try { XCloseable xCloseable = (XCloseable) UnoRuntime.queryInterface( XCloseable.class, xComponent); xCloseable.close(true); } catch (CloseVetoException ex) { ex.printStackTrace(); } } } } private boolean doExport( XComponent xComponent, String aURL, String sFilterName, PropertyValue[] aFilterData) { XStorable xStorable = (XStorable) UnoRuntime.queryInterface( XStorable.class, xComponent); if (xStorable == null) { return false; } else { try { PropertyValue[] aMediaDescriptor = new PropertyValue[2]; aMediaDescriptor[0] = new PropertyValue(); aMediaDescriptor[0].Name = "FilterName"; aMediaDescriptor[0].Value = sFilterName; aMediaDescriptor[1] = new PropertyValue(); aMediaDescriptor[1].Name = "FilterData"; aMediaDescriptor[1].Value = aFilterData; xStorable.storeToURL(aURL, aMediaDescriptor); return true; } catch (IOException ex) { ex.printStackTrace(); } } return false; } public static void printPropertyValues(PropertyValue[] aPropertyValues) { System.out.println("\nProperties and values\n"); for (PropertyValue property : aPropertyValues) { String sValue = ""; try { sValue = String.valueOf(property.Value); } catch (Exception ex) { ex.printStackTrace(); } System.out.println(property.Name + " = " + sValue); } } //************************************************************************* public static XComponent loadComponent( XComponentContext xContext, String sURL, PropertyValue[] aMediaDescriptor) { XComponent xComp = null; try { XComponentLoader xComponentLoader = (XComponentLoader) UnoRuntime.queryInterface( XComponentLoader.class, xContext.getServiceManager().createInstanceWithContext( "com.sun.star.frame.Desktop", xContext)); xComp = xComponentLoader.loadComponentFromURL( sURL, "_default", 0, aMediaDescriptor); } catch (Exception ex) { ex.printStackTrace(); } return xComp; } public static XComponent createNewDoc( XComponentContext xContext, String sDocType, PropertyValue[] aMediaDescriptor) { XComponent xComp = null; try { xComp = loadComponent(xContext, "private:factory/" + sDocType, aMediaDescriptor); } catch (Exception ex) { ex.printStackTrace(); } return xComp; } public static XComponent createNewDoc( XComponentContext xContext, String sDocType) { XComponent xComp = null; try { xComp = createNewDoc(xContext, sDocType, new PropertyValue[0]); } catch (Exception ex) { ex.printStackTrace(); } return xComp; } public static XComponent createNewHiddenDoc( XComponentContext xContext, String sDocType) { XComponent xComp = null; try { PropertyValue[] aMediaDescriptor = new PropertyValue[1]; aMediaDescriptor[0] = new PropertyValue(); aMediaDescriptor[0].Name = "Hidden"; aMediaDescriptor[0].Value = Boolean.TRUE; xComp = createNewDoc(xContext, sDocType, aMediaDescriptor); } catch (Exception ex) { ex.printStackTrace(); } return xComp; } //************************************************************************ //************************************************************************* public static String getHomeDir(XComponentContext xContext) { return getPathSubstitution(xContext, "$(home)"); } public static String getWorkDir(XComponentContext xContext) { return getPathSubstitution(xContext, "$(work)"); } public static String getUserDir(XComponentContext xContext) { return getPathSubstitution(xContext, "$(user)"); } /** * * com.sun.star.util.PathSubstitution * * $(inst) Installation path of the Office. * $(prog) Program path of the Office. * $(user) The user installation directory. * $(work) The work directory of the user. * Under Windows this would be the "MyDocuments" subdirectory. * Under Unix this would be the home-directory * $(home) The home directory of the user. * Under Unix this would be the home- directory. * Under Windows this would be the "Documents and Settings\ " subdirectory. * $(temp) The current temporary directory. * $(path) The value of PATH environment variable. * $(lang) The country code used by the Office, like * 01=english, * 49=german. * $(langid) The language code used by the Office, like * 0x0009=english, * 0x0409=english us. * $(vlang) The language used by the Office as a string. Like * "german" for a german Office. * */ public static String getPathSubstitution( XComponentContext xContext, String aVariable) { String variableSubst = null; try { XStringSubstitution xStringSubstitution = getPathSubstitution(xContext); variableSubst = xStringSubstitution.getSubstituteVariableValue(aVariable); } catch (com.sun.star.container.NoSuchElementException e) { e.printStackTrace(); } catch (com.sun.star.uno.Exception ex) { ex.printStackTrace(); } finally { return variableSubst; } } public static String getPathSubstitution( XComponentContext xContext, String aText, boolean bSubstRequired) { String variableSubst = null; try { XStringSubstitution xStringSubstitution = getPathSubstitution(xContext); variableSubst = xStringSubstitution.substituteVariables( aText, bSubstRequired); } catch (com.sun.star.container.NoSuchElementException e) { e.printStackTrace(); } catch (Exception ex) { ex.printStackTrace(); } finally { return variableSubst; } } public static XStringSubstitution getPathSubstitution( XComponentContext xContext) { XStringSubstitution xPathSubstitution = null; XMultiComponentFactory xMCF = xContext.getServiceManager(); try { xPathSubstitution = (XStringSubstitution) UnoRuntime.queryInterface( XStringSubstitution.class, xMCF.createInstanceWithContext( "com.sun.star.util.PathSubstitution", xContext)); } catch (com.sun.star.uno.Exception ex) { ex.printStackTrace(); } finally { return xPathSubstitution; } } }
How to use it from C++
ToDo
How to use it from OOo Basic
ToDo
Content on this page is licensed under the Public Documentation License (PDL). |