Stamping Text to PDF using Java API

We are using Aspose Java API to stamp our PDF documents. When I stamp a paragraph text, I set the origin to begin at a certain point in the source pdf, but when more lines are added, the origin shifts upwards and it collides with other content in the pdf. Is there a way to handle this using the Java API? We are on Aspose 11.2.0.

Can someone from Aspose Support clarify this? We are a current customer. I could not find any information from your javadocs or documentation. Is fixed origin stamping supported from your Java API?
Not sure what this API does on TextStamp:

setTreatYIndentAsBaseLine()

Appreciate any pointers.

 

Hi Arul,


Thanks for using our API’s.

It appears that you are adding Stamp with Horizontal Alignment and Vertical Alignment as Center and new lines in Stamp object are being rendered upwards. But before we comment further, can you please share the code snippet which you are using, so that we can further look into this matter and reply accordingly. We are sorry for this delay and inconvenience.

Thank you for your response. Here is the code (attached the template pdf used in testing):

byte[] fileBytes = Files.toByteArray(new File("/Users/arul/Desktop/template.pdf"));
com.aspose.pdf.Document pdfDocument = new com.aspose.pdf.Document(new ByteArrayInputStream(fileBytes));
com.aspose.pdf.facades.FormattedText formattedText = new com.aspose.pdf.facades.FormattedText();
String text = “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. Aspose.Pdf for Java provides TextStamp class to add a text stamp in a PDF file.”;
// set the line to 40 chars width
text = addLinebreaks(text, 40);
String lines[] = text.split("\r?\n");
for (String line : lines) {
formattedText.addNewLineText(line);
}
TextStamp textStamp = new com.aspose.pdf.TextStamp(formattedText);
textStamp.setXIndent(77.22);
textStamp.setYIndent(511.08);

textStamp.setBackground(false);
textStamp.getTextState().setFontSize(9.0F);
textStamp.getTextState().setFontStyle(com.aspose.pdf.FontStyles.Italic);
textStamp.getTextState().setForegroundColor(Color.getBlack());
pdfDocument.getPages().get_Item(1).addStamp(textStamp);
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
pdfDocument.save(outputStream);
byte[] stampedPdf = outputStream.toByteArray();
FileUtils.writeByteArrayToFile(new File("/Users/arul/Desktop/stamped.pdf"), stampedPdf);

Hi Nayyer,

Let me know if you need any other information.

Thank you,
Arul

Hi Arul,


Thanks for using our API’s.

I have tested the scenario and have managed to reproduce same problem. For the sake of correction, I have logged it as PDFNEWJAVA-35864 in our issue tracking system. We will further look into the details of this problem and will keep you posted on the status of correction. Please be patient and spare us little time. We are sorry for this inconvenience.

Thanks for the update. Do you know when this will be fixed?

Hi Arul,


Thanks for your patience.

As we recently have noticed this problem, so its pending for review and unless the product team has deeply investigated this problem, we may not be able to share the possible timelines by which it will be resolved. However, as soon as we have some definite updates regarding its resolution, we will let you know. Please be patient and spare us little time. We are sorry for this delay and inconvenience.

Hi Nayyer,

Do you have any update on this issue?

Thank you,
Arul

Hi Arul,

This this not a bug as a BaseLine is the line upon which most letters “sit” and it’s not related to align of the text. YIndent is vertical stamp coordinate, starting from the bottom so it’s define a bottom position.

If you need to begin at a certain point and to shift the large text downwards, you can set the VerticalAlignment as Top and the TopMargin to certain point. Please, see the snippet bellow:

com.aspose.pdf.Document pdfDocument = new com.aspose.pdf.Document("c:/pdftest/template.pdf");

com.aspose.pdf.facades.FormattedText formattedText = new com.aspose.pdf.facades.FormattedText();
String text = "Aspose.Pdf for Java provides TextStamp class to add a text stamp in a PDF file. The TextStamp \n" +
              "class provides necessary methods to specify font size, font style, and font color \n" +
              "etc for stamp object. In order to add text stamp, first you need to create a Document object and a TextStamp object \n" +
              "using required methods. After that, you may call addStamp(…) method of the Page class to add the stamp in the PDF document. \n" +
              "Aspose.Pdf for Java provides TextStamp class to add a text stamp in a PDF file.";

// Split the text into lines
String lines[] = text.split("\r?\n");

// Add each line to the formatted text
for (String line : lines) {
    formattedText.addNewLineText(line);
}

// Create a TextStamp object with the formatted text
TextStamp textStamp = new com.aspose.pdf.TextStamp(formattedText);

textStamp.setXIndent(77.22);
textStamp.setTopMargin(270);
textStamp.setVerticalAlignment(VerticalAlignment.Top);
textStamp.setTreatYIndentAsBaseLine(true);
textStamp.setBackground(false);
textStamp.getTextState().setFontSize(9.0F);
textStamp.getTextState().setFontStyle(com.aspose.pdf.FontStyles.Italic);
textStamp.getTextState().setForegroundColor(com.aspose.pdf.Color.getBlack());

// Add the text stamp to the first page of the PDF document
pdfDocument.getPages().get_Item(1).addStamp(textStamp);

// Save the updated document
pdfDocument.save("c:/pdftest/OutputFile.pdf");

Please feel free to contact us for any further assistance.

Best Regards,

Thank you Tilal for clarifying the APIs.