Add Text and Image to PDF Header

When it comes to creating professional and visually appealing PDF documents, having the ability to add images and text to the header is a must-have feature. The header section is one of the most important areas of a document, as it provides readers with essential information such as the title, author, and date. While adding text to the header is a relatively straightforward process, adding images can be more challenging, particularly for Python developers who are new to PDF document generation. In this article, we will explore how to add image and text to the header of a PDF document using Python Cloud SDK.

You can add images, logos, the date and time you created the document, the title of the document, version or revision number, draft, author name, and chapter names in PDF header.

REST API for PDF Document Assembly

Aspose.PDF Cloud SDK for Python provides a variety of functions and methods that allow developers to manipulate PDF files programmatically. It also provides a powerful and flexible set of tools for adding images and text to PDF document headers, and can be easily integrated into Python applications for automated PDF document processing. Now concerning the process of adding an image to the header, it involves specifying the location and dimensions of the image, as well as the page number on which the image should appear. Similarly, adding text to the header requires specifying the text content, font size, and other formatting options. The SDK also provides options for controlling the alignment and positioning of the header content, as well as for specifying the background color of the header.

Now, first we need to install the SDK which is freely available for download over PIP and GitHub repository. Simply execute the following command to install the latest version of the SDK.

pip install asposepdfcloud

In order to access the REST API, you need to register and get your personal credentials. Please take a look over Quick Start guide for related information.

Add Text to PDF Header using Python

The SDK enables you to add text strings to the header section of the document and this requirement can be accomplished using the following steps.

  • Firstly, create an instance of ApiClient class while providing Client ID Client Secret as arguments.
  • Secondly, create an object of PdfApi class which takes ApiClient object as input argument.
  • Thirdly, create an object of TextHeader defining properties of TextHeader instance.
  • In order to set text formatting for TextHeader, create an object of TextState where we define foreground color, font size, font name etc.
  • Finally, call the post_document_text_header(..) method to add Text Header to PDF document and save resultant file to Cloud storage.
Text Header in PDF

Image 2:- Preview of Text Header in PDF document.

The input and resultant PDF files can be downloaded from awesomeTable.pdf and awecomeTable-TextHeader.pdf.

Add Image to PDF Header using Python

Please follow the instructions below to add an Image object to the PDF header section. The resultant file uploads to cloud storage.

  • First of all, create an instance of ApiClient class while providing Client ID Client Secret as arguments.
  • Secondly, create an object of PdfApi class which takes ApiClient object as input argument.
  • Now create an object of ImageHeader defining properties of ImageHeader.
  • In order to control the rendering of header on certain pages, values are assigned to start_page_number and end_page_number optional parameters.
  • Finally, call the post_document_image_header(..) method to add Image Header to PDF document and save resultant file to Cloud storage. Notice that header is only added to first two pages of the document (as specified in code).
Image 3:- Preview of Image Header added in PDF.

Image 3:- Preview of Image Header added in PDF.

The source files and resultant file generated in the above example can be downloaded from

Adding Text Header to PDF using cURL Command

The cURL commands provide a convenient mechanism for accessing REST APIs through command line terminals. Since Aspose.PDF Cloud is also developed as per REST architecture, so we can also access the API using cURL commands. This approach can be useful for developers who prefer to work with command-line tools or who need to integrate PDF header editing into a larger automation workflow.

Now in order to use the REST API, we need to generate a JSON Web accessToken (JWT) based on your client credential by executing the following command.

curl -v "https://api.aspose.cloud/connect/token" \
-X POST \
-d "grant_type=client_credentials&client_id=88d1cda8-b12c-4a80-b1ad-c85ac483c5c5&client_secret=406b404b2df649611e508bbcfcd2a77f" \
-H "Content-Type: application/x-www-form-urlencoded" \
-H "Accept: application/json"

Once we have the JWT Token, please try using the following command to add text in the header section of the document on the 1st and 2nd pages.

curl -X POST "https://api.aspose.cloud/v3.0/pdf/awesomeTable.pdf/header/text?startPageNumber=1&endPageNumber=2" \
-H  "accept: application/json" \
-H  "authorization: Bearer <JWT Token>" \
-H  "Content-Type: application/json" \
-d "{  \"Background\": false,  \"HorizontalAlignment\": \"Center\",  \"Opacity\": 0.5,  \"Rotate\": \"None\",  \"RotateAngle\": 0,  \"XIndent\": 100,  \"YIndent\": 10,  \"Zoom\": 1.0,  \"TextAlignment\": \"None\",  \"Value\": \"Aspose.PDF Cloud\",  \"TextState\": {    \"FontSize\": 15,    \"Font\": \"Arial\",    \"ForegroundColor\": {      \"A\": 0,      \"R\": 252,      \"G\": 173,      \"B\": 3    },    \"BackgroundColor\": {      \"A\": 10,      \"R\": 0,      \"G\": 0,      \"B\": 0    }  },  \"LeftMargin\": 0,  \"TopMargin\": 0,  \"RightMargin\": 0}"

Adding Image Header to PDF using cURL Command

Please try using the following cURL command to add an Image Header to a PDF document and save the resultant file in cloud storage. In the following command, the image header to the 1st and 2nd page of the document where opacity is defined as 0.7, zoom factor is 0.5, and image height and width properties are defined as 120 points.

curl -X POST "https://api.aspose.cloud/v3.0/pdf/awesomeTable.pdf/header/image?startPageNumber=1&endPageNumber=2" \
-H  "accept: application/json" \
-H  "authorization: Bearer <JWT Token>" \
-H  "Content-Type: application/json" \
-d "{  \"Background\": true,  \"HorizontalAlignment\": \"Right\",  \"Opacity\": 0.7,  \"Rotate\": \"None\",  \"RotateAngle\": 0,  \"XIndent\": 10,  \"YIndent\": 10,  \"Zoom\": .5,  \"FileName\": \"Koala.jpg\",  \"Width\": 120,  \"Height\": 120,  \"LeftMargin\": 0,  \"TopMargin\": 10,  \"RightMargin\": 10}"

Conclusion

In conclusion, adding an image and text to the header of a PDF document is a useful feature that can enhance the visual appeal and branding of your documents. With the Aspose.PDF Cloud SDK for Python, it is a simple task to accomplish with just a few lines of code. Whether you are creating invoices, reports, or any other document that requires a header, this feature can save you time and effort while improving the quality of your output. In addition to using Aspose.PDF Cloud SDK for Python, we have also learnt that it is also possible to add images and text to PDF headers using cURL commands.

We hope that this article has provided you with all the necessary guidance and inspiration to add this functionality to your Python application.

We recommend visiting the following articles to learn about: