control PDF display properties

The PDF files have a unique feature to hold the view preferences that enable us in controlling how the viewer application will display the PDF document. Therefore, the viewer preferences dictionary of a PDF document allows you to control the way the document shall be presented on the screen or in print.

Please note that we can control the display properties of the PDF viewers. It includes controlling the display of PDF document title, hiding or showing Menubar, hide/show Status bar, hide/display Window UI. Control how to set up the page layout, page mode, and page layout. The majority of PDF processing applications support these operations. But, for bulk update operations, a programmatic solution is an effective solution. In this article, we are going to discuss the details and steps on how to control PDF display properties using Python.

PDF Manipulation SDK

Aspose.PDF Cloud is a REST-based API enabling the developers to implement PDF creation, manipulation, and rendering features in their applications. With fewer code lines, you can develop impressive PDF processing applications. No specific software installation is required and you can perform all PDF processing operations in the Cloud. To further facilitate our developers, we have created Aspose.PDF Cloud SDK for Python which is a wrapper around Aspose.PDF Cloud. So in order to get started with this SDK, the first step is its installation. The SDK is available for free download over PIP and GitHub repository. Now we need to execute the following command in the terminal/command prompt to perform the installation.

pip install asposepdfcloud

Free Cloud Dashboard Account

The next major step is a free subscription to our cloud services via Aspose.Cloud dashboard. The purpose of this subscription is to only allow authorized persons to access our file processing services. If you have GitHub or Google account, simply Sign Up or, click on the Create a new Account button and provide the required information. Now login to the dashboard using credentials and expand the Applications section from the dashboard and scroll down towards the Client Credentials section to see Client ID and Client Secret details.

Client credentials

Image 1:- Client Credentials on Aspose.Cloud Dashboard.

Read PDF Display Properties using Python

Please use the code snippet given below to read PDF viewer preferences and display them in the console.

  • Firstly, create an instance of ApiClient class while providing Client ID Client Secret as arguments
  • Secondly, create an instance of PdfApi class which takes ApiClient object as input argument
  • Finally, call the get_document_display_properties(…) method to retrieve PDF viewere preferences

The sample PDF document used in the above example can be downloaded from PdfWithEmbeddedFiles.pdf.

Update PDF Display Properties using Python

In this section, we are going to discuss the steps and related details on how to set PDF display properties using Python SDK. However, before proceeding further, we need to first learn the properties and the specific values that can be assigned to them.

Supported properties

Property Description
HideToolbar A flag specifying whether to hide the pdf viewer application’s toolbars when the document is active
HideMenuBar A flag specifying whether to hide the pdf viewer application’s menu bar when the document is active.
HideWindowUI A flag specifying whether to hide user interface elements in the document’s window (such as scroll bars and navigation controls), leaving only the document’s contents displayed.
FitWindow A flag specifying whether to resize the document’s window to fit the size of the first displayed page.
CenterWindow A flag specifying whether to position the document’s window in the center of the screen.
DisplayDocTitle A flag specifying whether the window’s title bar should display the document title.
PageMode The document page mode when the pdf document is opened in a pdf viewer. Possible values: UseNone – Neither document outline (bookmarks) nor thumbnail images are visible. UseOutlines – Document outline (bookmarks) are visible. UseThumbs – Thumbnail images are visible. FullScreenFull – screen mode, with no menu bar, window controls, or any other window visible. UseOC – Optional content group panel is visible. UseAttachments – Document attachments are visible.
PageLayout The page layout is to be used when the document is opened. Possible values: SinglePage – Displays one page at a time. OneColumn – Displays the pages in one column. TwoColumnLeft – Displays the pages in two columns, with odd-numbered pages on the left. TwoColumnRight – Displays the pages in two columns, with odd-numbered pages on the right.
NonFullScreenPageMode The document page mode when the pdf viewer application exits the full-screen mode. Possible values: UseNone – Neither document outline (bookmarks) nor thumbnail images are visible. UseOutlines – Document outline (bookmarks) are visible. UseThumbs – Thumbnail images are visible. UseOC – Optional content group panel is visible.
  • Create an instance of ApiClient class while providing Client ID Client Secret as arguments
  • Then create an instance of PdfApi class which takes ApiClient object as input argument
  • Now create an instance of DocumentProperties which defines new display properties for PDF display
  • Finally, call the put_document_display_properties(…) to set new display properties for PDF file
updated pdf display preferences

Image 2:- Preview of updated PDF display preferences.

Read PDF Display Properties using cURL

The REST APIs can also be accessed via cURL commands and since Aspose.PDF Cloud is developed as per REST architecture, so we can also access the API using the cURL command. However, before performing any document manipulation operation, we need to generate a JSON Web Token (JWT) based on your individual client credentials specified over Aspose.Cloud dashboard. It is mandatory because our APIs are only accessible to registered users. Please execute the following command to generate the JWT token.

curl -v "https://api.aspose.cloud/connect/token" \
-X POST \
-d "grant_type=client_credentials&client_id=bbf94a2c-6d7e-4020-b4d2-b9809741374e&client_secret=1c9379bb7d701c26cc87e741a29987bb" \
-H "Content-Type: application/x-www-form-urlencoded" \
-H "Accept: application/json"

Once the JWT token is generated, we can need to execute the following cURL command to read PDF display properties.

curl -v -X GET "https://api.aspose.cloud/v3.0/pdf/PdfWithEmbeddedFiles.pdf/displayproperties" \
-H  "accept: application/json" \
-H  "authorization: Bearer <JWT Token>"

Update PDF Display Properties using cURL

The cURL command can also be used to update the display properties of PDF documents and the resultant file is again saved in cloud storage.

curl -v -X PUT "https://api.aspose.cloud/v3.0/pdf/PdfWithEmbeddedFiles.pdf/displayproperties" \
-H  "accept: application/json" \
-H  "authorization: Bearer <JWT Token>" \
-H  "Content-Type: application/json" \
-d "{  \"Links\": [    {       \"Href\": \"/PdfWithEmbeddedFiles.pdf/displayproperties\",        \"Rel\": \"self\",        \"Type\": null,        \"Title\": null    }  ],  \"CenterWindow\": true,  \"Direction\": \"L2R\",  \"DisplayDocTitle\": true,  \"HideMenuBar\": true,  \"HideToolBar\": false,  \"HideWindowUI\": true,  \"NonFullScreenPageMode\": \"UseOutlines\",  \"PageLayout\": \"TwoColumnRight\",  \"PageMode\": \"UseThumbs\"}"

Conclusion

In this article, we have explored the details and steps on how to read and control PDF display properties. These requirements can be accomplished using Python code snippets as well as using the cURL commands. We also recommend exploring the Developer Guide to learn about other exciting features offered by the API. Furthermore, the complete source code of Apsose.PDF Cloud SDK for Python has been made available for download over GitHub. In case you encounter any issues while using the API or you have any further queries, please feel free to contact us via the Free product support forum.

We also recommend visiting the following links to learn more about