Add watermark to PDF

A watermark is text or an image that appears either in front of or behind existing document content, like a stamp. For example, you could apply a “Confidential” watermark to pages with sensitive information. The Watermarks can be used to protect confidential information and to indicate the validity of a legal document. You may also watermark any paper banknote, where they help to prevent counterfeiting. In this article, we are going to discuss the features related to watermark in PDF document.

PDF manipulation REST API

Aspose.PDF Cloud is our award-winning REST API offering the capabilities to create, manipulate as well as render PDF files to other supported formats including DOCX, PPTX, XLSX, XPS, SVG, JPEG, etc. Now let’s talk about Aspose.PDF Cloud SDK for Java which is a Java wrapper around Aspose.PDF Cloud API. It provides all the features of Cloud REST API including the capabilities to add watermark to PDF using Java programming language.

In order to use Java Cloud SDK, the first step is to install Aspose.PDF Cloud SDK for Java. The Cloud SDK is available for download over Maven | 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>http://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>

Add Text watermark in PDF using REST API

In order to use Cloud APIs, you need to first register an account on Aspose.Cloud dashboard where you will see your personalized Client ID and Client Secret details. These essential while accessing the APIs. Given below are the details on how to add Text watermark to existing PDF document using Java

  • First, you need to specify your Client ID and Client Secret details against two variables
  • Secondly, create an object of PdfApi class by providing Client ID and Client Secret as arguments
  • Thirdly, create an object of TextState class where we specify font name as Arial and font size as 14
  • Now we need to create an instance of TextStamp class. While using this object, we will specify HorizontalAlignment as a center, Text stamp value, the font size, and font type details are specified using textState(..) method where TextState object created in the above step is passed as an argument. The margin details and VerticalAlignment are also specified as Center
  • In order to display the TextStamp in the background, the background(..) method is used with an argument as true
  • Furthermore, the opacity, rotateAngle, Zoom factor, and XY indent for the stamp are also specified
  • Now create an object of the List class with type TextStamp and add the above-created stamp object to this list
  • Finally, call the method postPageTextStamps(..) of PdfAPi and pass input PDF, page number, and stamps List as an argument to initiate the stamp process
TextStamp added to PDF

Image 1:- Text Stamp added to PDF document.

The sample files used in the above example can be downloaded from the following links

How to add Image watermark in PDF using Java

With fewer code lines, the Image watermark can easily be added to a PDF document. Given below are the steps on how you can add a JPEG image to a PDF file

  • The first step is to create a PdfApi object while passing ClientSecret and ClientID as an argument
  • The next step is to create ImageStamp object where we define the image file to be stamped, margin detalis and VerticalAlignment. In our case, we have selected Center value from VerticalAlignment enumeration
  • Set ImageStamp.background(True) so that the image appears behind the content over page. Else, it will appear on top of page content
  • Create a List object of type ImageStamp and add ImageStamp object created in second step to this collection
  • Finally, call the postPageImageStamps(…) method of PdfApi class where we pass input PDF, page number for watermark and ImageStamp list created above as argument
  • The PDF file with image watermark is saved on same cloud storage. The preview of resultant file is displayed below
Image stamp added to PDF

Image 2:- Image stamp added to PDF document.

The sample files used in above example can be downloaded from following link.

Add PDF page as watermark stamp

Other than Text and Image, the PDF page can also be added as a watermark to an existing PDF document. Given below are the steps on how to accomplish these requirements

  • First of all, create an object of PdfApi class while passing Client ID and Client Secret as arguments
  • Secondly, create an instance of PdfPageStamp class. This class is used to specify the path of PDF document while will be used for watermark
  • PdfPageStamp.pageIndex(..) method is used to specify the page number of the PDF file which will be used for watermark
  • Among other methods, background(..) is important as it’s used to set watermark behind PDF content or keep it over the content
  • Then create a List of type PdfPageStamp and add the above created PdfPageStamp object to the List collection
  • Finally, call the method postPagePdfPageStamps(…) of PdfApi class where we pass source PDF file, page index to be watermarked, and List of PdfPageStamp as arguement. The resultant output is saved on cloud storage
PDF file as watermark

Image 3:- Preview of PDF file added as watermark.

Sample files used in the above example can be downloaded from the following links

Add Page Number as watermark

The page number details are very important from a usability perspective as they give a clear indication to the reader regarding the length of the document as well as the page the reader is going through.

  • Once we have created the PdfApi instance, create an object of PageNumberStamp class
  • use method value(…) of PageNumberStamp class to specify the pattern for PageNumber stamp
  • Another important method is setStartingNumber(..) which indicates the count from which Page Number watermark shall start
  • The other bit of code including margin details, opacity, rotate angle, oom factor, etc are the same as the above code snippets
  • We need to specify the startPageNumber and endPageNumber where the Page Number watermark shall be placed
  • Finally, call the postDocumentPageNumberStamps(…) which takes input PDF file, PageNumberStamp object, Start and Ending page information as arguments, and appends the Page Number watermark in the source file
Page Number stamp in PDF

Image 4:- Preview of Page Number as watermark in PDF.

The sample files used in the above example can be downloaded from the following link

Quick Tip

Aspose.PDF Cloud SDK for Java is so powerful that with a couple of code lines, you can convert, JPEG, EPUB, LaTeX, HTML, PS, XSLFo, XPS, SVG, DOCX, PCL formats to PDF format. For more information, please visit the product page.

Conclusion

In this blog post, we have learned about the steps on how we can easily add Text, Image, PDF, and PageNumber information to existing PDF documents using Aspose.PDF Cloud SDK for Java. Please note that our Cloud SDKs are open source and you can also download their complete source code and modify it as per your requirements. The complete source code can be downloaded from the GitHub repository. Should you have any related query, please feel free to contact via free support forums.