A header is a text that is placed in the margin area at the top of a page, while a footer is placed at the bottom, or foot, of a page. Typically these areas are used for inserting document information, such as the name of the document, the chapter heading, page numbers, creation date, etc. This information is repeated on each page and thus enables the reader to easily navigate the document.
Furthermore, please be aware that Headers and footers are useful in providing quick information about your document in a predictable format and also help in setting out different parts of a document. In this article, we are going to learn about adding text and images to the Header and footer areas of PDF files using Aspose.PDF Cloud SDK for Java.
- Add Text to Header using cURL
- Adding Text Header in PDF using Java
- Image in PDF Header using the cURL
- Add image in Header using Java
Add Text to Header using cURL
cURL commands provide a simple way to access RESTful APIs through the command line terminal. Since Aspose.PDF Cloud SDK follows REST principles, it can be accessed easily via the command prompt. However, please note that to ensure user privacy and data integrity, all our APIs are only accessible to authorized persons. Therefore, you need to first visit Aspose.Cloud dashboard and if you have a GitHub or Google account, simply Sign Up. Otherwise, click on the Create a new Account button and provide the required information. Now log in to the dashboard, expand the Applications section, and scroll down to the Client Credentials area to see your Client ID and Client Secret.
The following command shows how to create a JWT access token which will be used for adding text in the Header area of the PDF document.
curl -v "https://api.aspose.cloud/connect/token" \
-X POST \
-d "grant_type=client_credentials&client_id=c235e685-1aab-4cda-a95b-54afd63eb87f&client_secret=b8da4ee37494f2ef8da3c727f3a0acb9" \
-H "Content-Type: application/x-www-form-urlencoded" \
-H "Accept: application/json"
Use the following cURL command to add text in the header section of the PDF document.
curl -X POST "https://api.aspose.cloud/v3.0/pdf/PdfWithAnnotations.pdf/header/text?startPageNumber=2&endPageNumber=2" \
-H "accept: application/json" \
-H "authorization: Bearer <JWT Token>" \
-H "Content-Type: application/json" \
-d "{ \"Background\": true, \"HorizontalAlignment\": \"CENTER\", \"Opacity\": 0.8, \"Rotate\": \"None\", \"RotateAngle\": 0, \"XIndent\": 0, \"YIndent\": 0, \"Zoom\": 1, \"TextAlignment\": \"CENTER\", \"Value\": \"Aspose.PDF Cloud SDK for Java via cURL\", \"TextState\": { \"FontSize\": 16, \"Font\": \"Arial\", \"ForegroundColor\": { \"A\": 0, \"R\": 0xDA, \"G\": 0xA5, \"B\": 0x20 }, \"BackgroundColor\": { \"A\": 0, \"R\": 0xFA, \"G\": 0xEB, \"B\": 0xD7 }, }, \"LeftMargin\": 1, \"TopMargin\": 20, \"RightMargin\": 200}"
Image 1:- Text Header added via cURL command.
The sample files used in this example can be downloaded from:
Adding Text Header in PDF using Java
To use the SDK, install it from Maven or GitHub. Below are the details on how to add Aspose.Pdf.jar to a Maven project.
Add the following dependencies in your pom.xml file.
<repositories>
<repository>
<id>aspose-cloud</id>
<name>artifact.aspose-cloud-releases</name>
<url>https://artifact.aspose.cloud/repo</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>com.aspose</groupId>
<artifactId>aspose-pdf-cloud</artifactId>
<version>21.1.0</version>
<scope>compile</scope>
</dependency>
</dependencies>
Follow these steps to add a text header:
- Create an instance of PdfApi with ClientSecret and ClientId.
- Create a Color object to define foreground and background colors.
- Create a TextState object and set font size, font face, foreground and background colors.
- Build a TextHeader object, define margins, and set the header value.
- Call
textState(...)to attach the TextState instance. - Set
background(true)if the header should appear behind page content. - Use
horizontalAlignment(HorizontalAlignment.CENTER)to center the header. - Adjust opacity with
opacity(...)(default is 1.0). - Create an AsposeResponse after setting the zoom level.
- Finally, invoke
PdfApi.postDocumentTextHeader(...)with the source PDF name, TextHeader, and the start/end page numbers.
Image 2:- Preview of Text Header added with Java.
To add text in the footer area, create an instance of TextFooter and call
postDocumentTextFooter(...). The remaining steps are identical to those for the text header.
Image in PDF Header using the cURL
You can add image stamps with the PostDocumentImageHeader API in the header section of a PDF file using cURL. The command lets you specify horizontal alignment, opacity, rotation, zoom, width, height, and margins.
The name parameter defines the source PDF, imageHeader specifies the image file, and startPageNumber / endPageNumber define the page range. In the example below, Background is false so the image is visible, HorizontalAlignment is set to Left, opacity to 0.8, zoom to 1, width to 250 pt, height to 80 pt, and right margin to 500 pt.
curl -X POST "https://api.aspose.cloud/v3.0/pdf/PdfWithAnnotations.pdf/header/image?startPageNumber=2&endPageNumber=2" \
-H "accept: application/json" \
-H "authorization: Bearer <JWT Token>" \
-H "Content-Type: application/json" \
-d "{"Background":false,"HorizontalAlignment":'Left',"Opacity": .8,"Rotate":'None',"RotateAngle": 0,"XIndent": 2,"YIndent": 5,"Zoom": 1,"FileName": 'asposetoolsnew.png',"Width": 250,"Height": 80,"LeftMargin": 1,"TopMargin": 10,"RightMargin": 500}"
Image 3:- Image stamp in Header section of PDF.
The sample files used in the example can be downloaded from:
Add image in Header using Java
Aspose.PDF Cloud SDK for Java makes it easy to add an image header to a PDF file with minimal code.
Steps to add an image header:
- Create a PdfApi instance with ClientSecret and ClientId.
- Instantiate an ImageHeader object.
- Set image dimensions with
width(..)andheight(...). - Use
fileName(...)to specify the path to the header image. - Call
background(false)if the image should appear behind page content. - Set
horizontalAlignment(HorizontalAlignment.LEFT)to align the image left. - Adjust opacity with
opacity(...)(default 1.0). - Create an AsposeResponse after configuring zoom.
- Finally, call
PdfApi.postDocumentImageHeader(...)with the source PDF name, ImageHeader, and the start/end page numbers.
To add an image in the footer area, create an ImageFooter object and call
postDocumentImageFooter(...). The remaining steps are the same as for the header.
Conclusion
In this article, we covered how to use Aspose.PDF Cloud via cURL commands to add text and images to PDF header and footer areas. We also demonstrated how to install the Aspose.PDF Cloud SDK for Java and use it in Java to achieve the same results. For more details on other powerful API features, explore the product documentation.
All our Cloud SDKs are open source, and the full source code is available on GitHub.