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 the following code snippet to add a text/image stamp in the 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 that, as the PDF document is created dynamically by API and in order to obtain real PDF document pages, we need to render its programming/dynamic model. Thus we can render the PDF document model by calling the save() method or processParagraphs() and later can iterate through the pages to add stamps.

Furthermore, to add a stamp on some specific position, you need to set the stamp XIndent and YIndent properties accordingly.

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

// Set page margins
MarginInfo margin = page.getPageInfo().getMargin();
margin.setLeft(10);
margin.setRight(10);
margin.setTop(10);
margin.setBottom(10);

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

// Add text fragments to the page
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);

// Text stamp
TextStamp textStamp = new TextStamp("Hello World");
textStamp.setTextAlignment(HorizontalAlignment.Center);
textStamp.setBackground(false);
textStamp.setOpacity(0.5);
textStamp.setRotateAngle(0);
textStamp.setHorizontalAlignment(HorizontalAlignment.Center);
textStamp.setVerticalAlignment(VerticalAlignment.Center);

// Add stamps to all pages
for (int i = 1; i <= pdf.getPages().size(); i++) {
    pdf.getPages().get_Item(i).addStamp(textStamp);
    pdf.getPages().get_Item(i).addStamp(imageStamp);
}

// Save the output PDF
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,