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

Free Support Forum - aspose.com

Document save throws null pointer exception while applying TextStamp

Hi

We get null pointer exception for few docs while trying to apply textstamp (fontsize=10) iteratively for each page at the line where we call save() method of com.aspose.pdf.Document

Font font = FontRepository.findFont("Helvetica", true);
PageCollection pages = doc.getPages();
for (Page page : pages) {
	writeToPage(font, fontSize, doc, page, stamp);
}
//writeToPage code snippet
TextStamp textStamp = new TextStamp(text);
int margin = 15;
// Set text alignment and margin based on page rotation
textStamp.setHorizontalAlignment(HorizontalAlignment.Right);
textStamp.setVerticalAlignment(VerticalAlignment.Top);
textStamp.setTopMargin(margin);
textStamp.setRightMargin(margin);

// set text properties
textStamp.getTextState().setFont(font);
textStamp.getTextState().setFontSize(fontSize);
// add stamp to particular page
page.addStamp(textStamp);

// save output document
doc.save();

java.lang.NullPointerException
at com.aspose.pdf.internal.l8f.l0v.lv(Unknown Source)
at com.aspose.pdf.internal.l9p.ld.lI(Unknown Source)
at com.aspose.pdf.internal.l8f.l0p.lf(Unknown Source)
at com.aspose.pdf.inteyrnal.l8f.l0t.lj(Unknown Source)
at com.aspose.pdf.internal.l0k.l0if.lh(Unknown Source)
at com.aspose.pdf.ADocument.l1n(Unknown Source)
at com.aspose.pdf.ADocument.save(Unknown Source)
at com.aspose.pdf.Document.save(Unknown Source)

Here are more details about the env where Aspose is integrated in.
The Aspose related services are exposed as REST calls packaged as web application
with the rest of the other services specific to project.

aspose-pdf-19.1-jdk17.jar
Oracle JDK 1.7.0_71
Jai_Codec 1.1.3
Springframework 4.0.1.RELEASE
Tomcat 7.0.39
RHEL 6.5

Best regards,
MansoorTest-A.pdf (37.0 KB)

@dmanahmed

Thanks for contacting support.

We have tested the scenario in our environment while using your code snippet and were able to replicate the issue. However, we modified code snippet a little bit by taking doc.save() statement out of iteration. It is recommended and right approach to save document once after processing because once it is saved, it closes all initialized resources along with itself.

Furthermore, we have also observed that if we save PDF document as separate file, the issue does not occur. saved.pdf (38.2 KB) Regarding the issue, we have logged it as PDFJAVA-38445 in our issue tracking system for further investigation. We will look into details of the issue and keep you posted with the status of its correction. Please be patient and spare us little time.

We are sorry for the inconvenience.

The issues you have found earlier (filed as PDFJAVA-38445) have been fixed in Aspose.PDF for Java 19.3.