We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Add page watermark and border to PDF file in Java using Aspose.PDF


I have the license for Aspose 11 and using com.aspose.pdf classes.

1 - I want to add page border and watermark to the page while creating the PDF and not stamping it later.

The watermark can be either Image or some text. The watermark is to be inserted in the center of page always, irrespective of the width of the image or the length of text.

2 - The PDF document that I am creating has some margin, Now i want to write some text at a absolute position in the margin while creating the PDF document and not stamp it later.

How do I do this ?

Hi Saurabh,

Thanks for your inquiry. Please check following code snippet to add text/image stamp in center of a PDF page while creating a new PDF document and setting page margins(border) as well. Hopefully it will help you to accomplish the task.

Please note as PDF document is created dynamically by API and in order to get real PDF document pages we need to render its programming/dynamic model. So we can render PDF document model by calling save() method or processParagraph() and later can iterate through the pages as following to add stamps.

Furthermore to add stamp on some specific position you need to set stamp XIndent and YIndent property accordingly.

Document pdf = new Document(); <o:p></o:p>

Page page = pdf.getPages().add();





TextFragment text = new TextFragment("Aspose.Pdf for Java");

for (int i = 0; i < 400; i++)




//render PDF document structure


//Image stamp

ImageStamp imageStamp = new ImageStamp(myDir + "aspose_pdf-for-java.jpg");




TextStamp theStamp = new TextStamp("Hello World");





// define the stamp's location

// center



for (int i=1;i<=pdf.getPages().size();i++)





pdf.save(myDir + "test_stampout.pdf");

Please feel free to contact us for any further assistance.

Best Regards,

Hello ,

Thanks for the coded example. But I am a bit confused.

The code that you provided to set page border I think will just set page margin. Right ?
I would like to set page border with black or any color with some border width.
How or where do i specify this ?

Also can I provide border style like dashed or dotted ?

Hi Saurabh,

Thanks for your feedback. I am sorry for misunderstanding your requirement, I am looking into it and will update you soon.

Best Regards,


Can you please provide me a update on the page border query.
I need to implement it in my application and I need this feature.

Hi Saurabh,

We are sorry for the inconvenience. After initial investigation I have logged a ticket PDFNEWJAVA-35566 in our issue tracking system for further investigation and resolution. We will keep you updated about the issue resolution progress.

Best Regards,