Hello, I am evaluating aspose-pdf version 21.12, and I am looking to add text to both the upper right corner and bottom of all pages in a PDF file using java. TextStamp seems to be the ideal way to do this, but I can’t get the TextStamp to display on all pages of the document. I have found other posts that suggest setting TextStamp.setBackground(false); as well as others suggesting setting TextStamp.setOpacity(1); however the problem is persisting. Please see my attempts below.
Using TextStamp:
public void addHeadersAndFooters(Document pdfDoc, String enumber, String incrementedEnumber) throws Exception {
// Create header
TextStamp header = new TextStamp(enumber);
header.setTopMargin(10);
header.setHorizontalAlignment(HorizontalAlignment.Right);
header.setVerticalAlignment(VerticalAlignment.Top);
header.setBackground(false);
header.setOpacity(1);
//create footer
TextStamp footer = new TextStamp("FOR DESIGN PURPOSES ONLY NOT FOR CONSTRUCTION");
// Set properties of the stamp
footer.setBottomMargin(10);
footer.setHorizontalAlignment(HorizontalAlignment.Center);
footer.setVerticalAlignment(VerticalAlignment.Bottom);
footer.setBackground(false);
footer.setOpacity(1);
// Add header on all pages
for (Page page : pdfDoc.getPages()) {
page.getParagraphs().clear();
page.addStamp(header);
page.addStamp(footer);
}
// Save the updated document
pdfDoc.save(outpath + incrementedEnumber + ".pdf");
}
I then tried with TextFragment but the problem still persisted:
public void addHeadersAndFooters(Document pdfDoc, String enumber, String incrementedEnumber) throws Exception {
TextFragment text = new TextFragment("FOR DESIGN PURPOSES ONLY NOT FOR CONSTRUCTION");
text.getTextState().setFontSize(20);
// Add header on all pages
for (Page page : pdfDoc.getPages()) {
page.getParagraphs().add(text);
}
// Save the updated document
pdfDoc.save(outpath + incrementedEnumber + ".pdf");
}
And I then tried putting a TextFragment in a HeaderFooter but again, same issue:
public void addHeadersAndFooters(Document pdfDoc, String enumber, String incrementedEnumber) throws Exception {
HeaderFooter header = HeaderFooter();
header.getMargin().setLeft(500);
header.getMargin().setTop(10);
header.getMargin().setRight(10);
header.getMargin().setBottom(10);
header.getParagraphs().add(new TextFragment(enumber));
// Add header on all pages
for (Page page : pdfDoc.getPages()) {
page.setHeader(header);
}
// Save the updated document
pdfDoc.save(outputpath + incrementedEnumber + ".pdf");
}
Interestingly, even when the text does not display on the page I still get the evaluation water mark on the upper left of the page saying “Evaluation Only. Created with Aspose.PDF. Copyright 2002-2021 Aspose Pty Ltd”. Any suggestions would be greatly appreciated, thank you.
@alex.jones
Please use following code example to add text stamp on all pages of PDF. You need to set the position of text stamp according to your requirement using Stamp.setOrigin method.
PdfFileStamp fileStamp = new PdfFileStamp();
// Open Document
fileStamp.bindPdf(MyDir + "input.pdf");
// Create stamp
Stamp stamp = new Stamp();
stamp.bindLogo(new FormattedText("Hello World!", java.awt.Color.BLUE, java.awt.Color.GRAY, FontStyle.Helvetica,
EncodingType.Winansi, true, 14));
stamp.setOrigin(500, 770);
//stamp.setRotation(90.0F);
stamp.setBackground(true);
// Add stamp to PDF file
fileStamp.addStamp(stamp);
// Save updated PDF file
fileStamp.save(MyDir + "AddTextStamp-All_out.pdf");
// Close fileStamp
fileStamp.close();
For more detail, please read the following article.
Add Text Stamp on All Pages in a PDF File
@tahir.manzoor
This code works well if all pages in the pdf are the same size, but both the size and orientation of the pages in the pdf files I am modifying change. I have adapted your sample code below, but the text is not showing the upper right corner. If I inspect the page, I can see that it has been added to the file, but it is still not visible.
public void addHeadersAndFooters(Document pdfDoc, String enumber, String incrementedEnumber) throws Exception {
PdfFileStamp fileStamp = new PdfFileStamp();
// Open Document
fileStamp.bindPdf(pdfDoc);
//max of 4 items can be open in evaluation mode
//for (int i = 1; i < pdfDoc.getPages().size(); i++) {
for (int i = 1; i <= 3; i++) {
Page page = pdfDoc.getPages().get_Item(i);
//build and add footer
Stamp footerStamp = new Stamp ();
FormattedText footer = new FormattedText("FOR DESIGN PURPOSES ONLY NOT FOR CONSTRUCTION", java.awt.Color.BLACK, java.awt.Color.WHITE, FontStyle.Helvetica, EncodingType.Winansi, true, 10);
footerStamp.bindLogo(footer);
footerStamp.setRotation(0);
//logoStamp.setOpacity(0);
footerStamp.setBackground(false);
footerStamp.setPageNumber(i);
footerStamp.setOrigin((float) ((page.getArtBox().getWidth() - footer.getTextWidth()) / 2), 0);
fileStamp.addStamp(footerStamp);
//build and add header
Stamp headerStamp = new Stamp ();
FormattedText header = new FormattedText(enumber, java.awt.Color.BLACK, java.awt.Color.WHITE, FontStyle.Helvetica, EncodingType.Winansi, true, 10);
headerStamp.bindLogo(header);
headerStamp.setRotation(0);
headerStamp.setOpacity(1);
headerStamp.setBackground(true);
headerStamp.setPageNumber(i);
headerStamp.setOrigin((float) (page.getArtBox().getWidth() - header.getTextWidth() - 10), (float) (page.getArtBox().getHeight() - 30));
fileStamp.addStamp(headerStamp);
}
// Save updated PDF file
fileStamp.save("outputPath" + incrementedEnumber + ".pdf");
// Close fileStamp
fileStamp.close();
}
@alex.jones
You need to set the position of text stamp according to page size. Please read following article about getting page position.
Get PDF Page Size
@tahir.manzoor
This has resolved my issue, thank you.
public void addHeadersAndFooters(Document pdfDoc, String enumber, String incrementedEnumber) throws Exception {
PdfFileStamp fileStamp = new PdfFileStamp();
// Open Document
fileStamp.bindPdf(pdfDoc);
//max of 4 items can be open in evaluation mode
//for (int i = 1; i < pdfDoc.getPages().size(); i++) {
for (int i = 1; i <= 3; i++) {
Page page = pdfDoc.getPages().get_Item(i);
//build and add footer
Stamp footerStamp = new Stamp ();
FormattedText footer = new FormattedText("FOR DESIGN PURPOSES ONLY NOT FOR CONSTRUCTION", java.awt.Color.BLACK, java.awt.Color.WHITE, FontStyle.Helvetica, EncodingType.Winansi, true, 10);
footerStamp.bindLogo(footer);
footerStamp.setRotation(0);
footerStamp.setOpacity(1);
footerStamp.setBackground(false);
footerStamp.setPageNumber(i);
//set location on page
footerStamp.setOrigin((float) ((page.getPageRect(true).getWidth() - footer.getTextWidth()) / 2), 0);
fileStamp.addStamp(footerStamp);
//build and add header
Stamp headerStamp = new Stamp ();
FormattedText header = new FormattedText(enumber, java.awt.Color.BLACK, java.awt.Color.WHITE, FontStyle.Helvetica, EncodingType.Winansi, true, 10);
headerStamp.bindLogo(header);
headerStamp.setRotation(0);
headerStamp.setOpacity(1);
headerStamp.setBackground(false);
headerStamp.setPageNumber(i);
//set location on page
headerStamp.setOrigin((float) (page.getPageRect(true).getWidth() - header.getTextWidth() - 10), (float) (page.getPageRect(true).getHeight() - 30));
fileStamp.addStamp(headerStamp);
}
// Save updated PDF file
fileStamp.save(outputpath + incrementedEnumber + ".pdf");
// Close fileStamp
fileStamp.close();
}
@alex.jones
Thanks for your feedback. It is nice to hear from you that your problem has been solved.