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.
def addTextHeader():
try:
#Client credentials
client_secret = "1c9379bb7d701c26cc87e741a29987bb"
client_id = "bbf94a2c-6d7e-4020-b4d2-b9809741374e"
#initialize PdfApi client instance using client credetials
pdf_api_client = asposepdfcloud.api_client.ApiClient(client_secret, client_id)
# create PdfApi instance while passing PdfApiClient as argument
pdf_api = PdfApi(pdf_api_client)
#source PDF file
input_file = 'awesomeTable.pdf'
# Create an object defining Text Header
textHeader = asposepdfcloud.TextHeader()
# set the value to render header in background
textHeader.background = False
# set the horizontal alignment for Text header as center
textHeader.horizontal_alignment = 'Center'
# set opacity of header as 0.5
textHeader.opacity = 0.5
# no rotate angle
textHeader.rotate = None
textHeader.rotate_angle = 0
# set X and Y indent value
textHeader.x_indent = 100
textHeader.y_indent = 100
# set the zoom factor as 1.0
textHeader.zoom = 1.0
# the Text value to be rendered inside header
textHeader.value = 'Aspose.PDF Cloud'
# create an instance of TextState defining formating of text
textState = asposepdfcloud.TextState(font_size=15, font='Arial', foreground_color=
{"A": 0,
"R": 252,
"G": 173,
"B": 3},font_style=2)
# pass TextState object to TextHeader
textHeader.text_state = textState
# call the API to add Text Header to PDF document
response = pdf_api.post_document_text_header(name = input_file, text_header = textHeader)
# print response code in console
print(response)
# print message in console (optional)
print('Text Header successfully added to PDF !')
except ApiException as e:
print("Exception while calling PdfApi: {0}".format(e))
print("Code:" + str(e.code))
print("Message:" + e.message)
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).
def addImageHeader():
try:
#Client credentials
client_secret = "1c9379bb7d701c26cc87e741a29987bb"
client_id = "bbf94a2c-6d7e-4020-b4d2-b9809741374e"
#initialize PdfApi client instance using client credetials
pdf_api_client = asposepdfcloud.api_client.ApiClient(client_secret, client_id)
# create PdfApi instance while passing PdfApiClient as argument
pdf_api = PdfApi(pdf_api_client)
#source PDF file
input_file = 'awesomeTable.pdf'
# create ImageHeader instance
imageHeader = asposepdfcloud.ImageHeader()
# set the background property as true
imageHeader.background = True
# set horizontal alignment as Right
imageHeader.horizontal_alignment = 'Right'
imageHeader.opacity = 0.7
imageHeader.rotate = None
imageHeader.rotate_angle = 0
imageHeader.x_indent = 10
imageHeader.y_indent = 10
imageHeader.zoom = .5
# name of image file available in cloud storage to be added in header
imageHeader.file_name = 'Koala.jpg'
# width of image in header
imageHeader.width = 120
# height of image in header
imageHeader.height = 120
imageHeader.left_margin = 0
imageHeader.top_margin = 10
imageHeader.right_margin = 10
# start page of document to add Header
startPage = 1
# end page of document to add Header
endPage = 2
# call the API to add Image header in PDF document and save resultant file in cloud storage
response = pdf_api.post_document_image_header(name = input_file, image_header = imageHeader, start_page_number = startPage, end_page_number=endPage)
# pritn response code in console
print(response)
# print message in console (optional)
print('Image Header successfully added to PDF !')
except ApiException as e:
print("Exception while calling PdfApi: {0}".format(e))
print("Code:" + str(e.code))
print("Message:" + e.message)
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: