Adding Stamp in a PDF file

Add Page Number Stamp in a PDF file

You can use PageNumberStamp class to add a page number stamp in a PDF document. The PageNumberStamp class provides methods to create a page number based stamp like format, margins, alignments, starting number etc. In order to add page number stamp, you need to create a Document object and a PageNumberStamp object with required properties. After that, you can call addStamp(..) method of the Page class to add the stamp in PDF file. You can also set the font attributes of the page number stamp.

The following code snippet shows you how to add page numbers in a PDF file.

Add Text Stamp in a PDF file

Aspose.PDF for Java provides TextStamp class to add a text stamp in a PDF file. The TextStamp class provides necessary methods to specify font size, font style, and font color etc for stamp object. In order to add text stamp, first you need to create a Document object and a TextStamp object using required methods. After that, you may call addStamp(..) method of the Page class to add the stamp in the PDF document.

The following code snippet shows you how to add text stamp in the PDF file.

In order to add Text stamp on all pages, please iterate through all pages and add call [addStamp](textStamp) method.

Define alignment for TextStamp object

Adding watermarks to PDF documents is one of the frequent demanded features and Aspose.PDF for Java is fully capable of adding Image as well as Text watermarks. The TextStamp class provides the feature to add text stamps over the PDF file. Recently there has been a requirement to support the feature to specify the alignment of text when using TextStamp object. So in order to fulfill this requirement, we have introduced setTextAlignment(..) method in TextStamp class. By using this method, you can specify the Horizontal text alignment.

The following code snippets shows an example on how to load an existing PDF document and add TextStamp over it.

todo:image_alt_text
Figure: Output PDF file displaying stamp alignment feature

Add Image Stamp in a PDF file

You can use ImageStamp class to add an image as a stamp in PDF document. The ImageStamp class provides methods to specify height, width, and opacity etc. In order to add image stamp, first you need to create a Document object and an ImageStamp object with required properties. After that, you need to call addStamp(..) method of the Page class to add the stamp in the PDF.

The following code snippet shows you how to add image stamp in the PDF file.

Control Image Quality when Adding Image Stamp

The ImageStamp class lets you add an image as a stamp in a PDF document. It also allows you to control the image quality when adding an image as a watermark in a PDF file. To allow this, a method named setQuality(…) has been added to the ImageStamp class. A similar method can also be found in the Stamp class of the com.aspose.pdf.facades package. Please note that possible values for this method are (0..100).

The following code snippet shows you how to control image quality when adding as stamp in the PDF file.

DOM approach

Facades approach

Add PDF Page Stamp in a PDF file

You can use PdfPageStamp class to add a PDF page as stamp in a PDF file. PdfPageStamp class provides properties necessary to create a PDF page based stamp. You can pass a particular page of any PDF file to the constructor of PdfPageStamp class. In order to add page based stamp, you need to create a Document object and a PdfPageStamp object using required properties. After that, you can call addStamp method of the Page to add the stamp in the PDF. The following code snippet shows you how to add PDF page stamp in a PDF file.

Add Image Stamp as Background in Floating Box

Aspose.PDF API lets you add image stamp as background in a floating box. The BackgroundImage property of FloatingBox class can be used to set the background image stamp for a floating box as shown in following code sample.