Add Text and Image to PDF Header

The headers and footers are small pieces of information available at the top margin of each page. They support the purpose of labeling and providing descriptive information. More commonly, people add page numbers and file names on each page to label it and avoid misplaced pages. Furthermore, 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. By adding this information to your multiple-page document, you remind your readers about your company/author or the readers will keep in mind that it’s a different revision of the document. The addition of such details to bulk documents may get cumbersome through a manual approach but a viable solution would be to employ a programmatic solution. In this article, we are going to discuss the steps and related details on how to add Text to PDF Header using Python.

PDF Manipulation SDK

Aspose.PDF Cloud is our award-winning API offering the capabilities to create, edit as well as manipulate existing PDF files. As the API is built as per REST architecture, so it can be accessed on any platform i.e. Desktop, Mobile, Web, Hybrid, etc. Furthermore, you can use any operating system of your choice to access the API and accomplish your requirements. Now in order to further enhance the experience of our users, we have created programming SDKs around the Cloud API. Therefore, in order to take benefit of PDF processing in Python applications, please try using Aspose.PDF Cloud SDK for Python.

The first step in usage is its installation and it’s available for free download over PIP and GitHub repository. Simply execute the following command on the terminal/command prompt to install the latest version of SDK on the system.

pip install asposepdfcloud

Free Cloud Dashboard Account

We have created a Cloud dashboard to manage customer credentials as well as the files stored in their specific storage. Therefore, 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.

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 formating 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. However, we need to generate a JSON Web Token (JWT) based on your client credential details specified over Aspose.Cloud dashboard. This step 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=88d1cda8-b12c-4a80-b1ad-c85ac483c5c5&client_secret=406b404b2df649611e508bbcfcd2a77f" \
-H "Content-Type: application/x-www-form-urlencoded" \
-H "Accept: application/json"

Now that JWT Token has been generated, 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 this article, we have discussed the capabilities of Aspose.PDF Cloud SDK for Python for adding Text and Image objects to PDF header sections. We have also learned about the flexibility the API provides for adjusting formatting and layout details for these objects. Furthermore, in the above section, we have also explored the options of adding Text and Image objects through cURL commands. Furthermore, please note that our Cloud SDKs are developed under MIT license, so their complete code snippet is available for free download over GitHub. Should you have any related queries or you encounter any issues while using our APIs, please feel free to contact us via the free customer support forum.

We recommend visiting the following articles to learn about: