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 are an exciting way to access RESTFul APIs through the command line terminal. Since Aspose.PDF Cloud SDK is developed as per REST principles, so it can easily be accessed via command prompt. However, please note that in order 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 GitHub or Google account, simply Sign Up. Otherwise, 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.
The following command shows how to create a JWT access token which is going to 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 is 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}"
The sample files used in this example can be downloaded from
Adding Text Header in PDF using Java
In order to use the SDK, you need to first install it either from Maven or GitHub. Given below are the details on how to download and use Aspose.Pdf.jar in the Maven build 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>
Given below are the steps on how to accomplish this requirement.
- First of all, create an instance of PdfApi while providing ClientSecret and ClientId details
- Secondly, create a Color object to define ForeGround and BackGround color details
- Secondly, create an object of TextState class
- Use fontSize(..) method to set font size details. The font(..) method to define the font face. The foregroundColor(…) method defines text foreground details, and the backgroundColor(…) method to define background information
- Now create TextHeader object and define margin details
- Use value(..) methods to specify the content of the Text Header object
- Now call textState(…) method and pass above created TextState instance as argument
- In order to display the Header behind page content, pass the true value as an argument to the background(…) method of TextHeader class
- Now in order to render the image on the left side, pass HorizontalAlignment.CENTER value to horizontalAlignment(…) method
- The opacity(…) method indicates the stamp opacity. The default value is 1.0
- Once we have set the zoom value, create AsposeResponse object
- Finally, call PdfApi.postDocumentTextHeader(…) method and pass source PDF file name, TextHeader object, start and end pages (where stamp needs to be added) as arguments.
In order to add text in the footer area, please create an instance of TextFooter class and call postDocumentTextFooter(…) method of PdfApi class. The remaining steps and details will stay the same as mentioned above for adding the Text Header.
Image in PDF Header using the cURL
We can easily add image files using PostDocumentImageHeader API in the header section of the PDF file using the cURL command. While adding an image stamp, we get the option to specify HorizontalAlignment, Opacity, RotateAngle, Zoom factor of image, Width, Height, LeftMargin, TopMargin, RightMargin, etc details.
The name parameter defines the input/source PDF document, imageHeader specifies the image file to be stamped, the startPageNumber property defines starting page and endPageNumber ending page of PDF document to contain image stamps. In the following cURL command, the Background property is set as false so that image is visible. The HorizontalAlignment is specified as Left, Opacity value is defined as 0.8, the Zoom factor is set as 1, the Width is defined as 250, Height is defined as 80 points and RightMargin is specified as 500 points.
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}"
The sample files used in above example can be downloaded from following link
Add image in Header using Java
Aspose.PDF Cloud SDK for Java provides a remarkable feature to add an Image Header in a PDF file with fewer code lines.
Given below are steps on how to add Image Header in PDF document
- Create an object of PdfApi while providing ClientSecret and ClientId details
- Secondly, create an object of ImageHeader class
- Use width(..) and height(…) methods of ImageHeader class to set image width and height details
- Now use the fileName(…) method to set path details for the header image
- In order to display the image behind the page content, pass the false argument to background(…) method
- Now in order to render the image on the left side, pass HorizontalAlignment.LEFT value to horizontalAlignment(…) method
- The opacity(…) method indicates the stamp opacity. The default value is 1.0
- Once we have set the zoom value, create AsposeResponse object
- Finally, call PdfApi.postDocumentImageHeader(…) method while passing source PDF file name, ImageHeader object, start and end pages (where stamp needs to be added) as arguments to this method.
In order to add an image in the footer area, create an object of ImageFooter class and call the postDocumentImageFooter(…) method of the pdfApi class. The rest of the steps will remain the same as explained above for the Image in the Header section above.
Conclusion
In this article, we have discussed the steps on how to access Aspose.PDF Cloud via cURL commands to add Text and Images in Header/Footer areas of PDF documents. We have also explored the steps to install Aspose.PDF Cloud SDK for Java and use it in Java language for adding Text and Image in the header or footer section of the PDF file. For more details on other exciting features of the API, please explore product documentation.
All our Cloud SDKs are OpenSource, so a complete source code can be downloaded from GitHub.