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

Hi,


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();

page.getPageInfo().getMargin().setLeft(10);

page.getPageInfo().getMargin().setRight(10);

page.getPageInfo().getMargin().setTop(10);

page.getPageInfo().getMargin().setBottom(10);

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

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

{

page.getParagraphs().add(text);

}

//render PDF document structure

pdf.processParagraphs();

//Image stamp

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

imageStamp.setHorizontalAlignment(HorizontalAlignment.Center);

imageStamp.setVerticalAlignment(VerticalAlignment.Center);

imageStamp.setBackground(true);

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

theStamp.setTextAlignment(HorizontalAlignment.Center);

theStamp.setBackground(false);

theStamp.setOpacity(.5);

theStamp.setRotateAngle(0);

// define the stamp's location

// center

theStamp.setHorizontalAlignment(HorizontalAlignment.Center);

theStamp.setVerticalAlignment(VerticalAlignment.Center);

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

{

pdf.getPages().get_Item(i).addStamp(theStamp);

pdf.getPages().get_Item(i).addStamp(imageStamp);

}

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,

Hi,


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,