Add Stamp and Save PDF to ByteArrayOutputStream in Java using Aspose.PDF for Java - objectId is registered already

Hi

We observed for a document when applying page stamp using AsposePDF library
we get an exception class com.aspose.pdf.internal.ms.System.lh: objectId is registered already at step 2 pdfDoc.save(os) method call

Code snippet:

com.aspose.pdf.Document pdfDoc = asposePDFService.loadPDF(inputStream, “”);
Font font = FontRepository.findFont(“Helvetica”, true);
Page page = pdfDoc.getPages().get(0);
TextStamp textStamp = new TextStamp(text);
textStamp.setHorizontalAlignment(HorizontalAlignment.Right);
textStamp.setVerticalAlignment(VerticalAlignment.Top);
textStamp.setTopMargin(margin);
textStamp.setRightMargin(margin);
page.addStamp(textStamp);
//step 1
try {
// save output document
pdfDoc.save();
} catch (NullPointerException npe) {}
ByteArrayOutputStream os = new ByteArrayOutputStream();
//step 2
try {
pdfDoc.save(os);
} catch (Exception e) {}
// pass this new ByteArrayInputStream(os.toByteArray()) to content manager

Please find the exception stack trace and the sample document for your reference.

Test_ExportAsposeIssue.pdf (467.0 KB)
DocSaveIssue.zip (1.8 KB)

VM Specs: 7 GB ram 2 cores
aspose-pdf-19.3-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

@dmanahmed

Thank you for contacting support.

We have modified your code snippet a little to execute it on our end. However the issue did not reproduce with Aspose.PDF for Java 19.4.

Document pdfDoc = new Document(dataDir + "Test_ExportAsposeIssue.pdf");
Font font = FontRepository.findFont("Helvetica", true);
Page page = pdfDoc.getPages().get_Item(1);
TextStamp textStamp = new TextStamp("text");
textStamp.setHorizontalAlignment(HorizontalAlignment.Right);
textStamp.setVerticalAlignment(VerticalAlignment.Top);
textStamp.setTopMargin(20);
textStamp.setRightMargin(20);
page.addStamp(textStamp);
//step 1	
try {
// save output document
pdfDoc.save();
} catch (NullPointerException npe) {}
ByteArrayOutputStream os = new ByteArrayOutputStream();
//step 2
try {
pdfDoc.save(os);
} catch (Exception e) {}

Please feel free to contact us if you still face any issue with latest version of the API.

Hi

As suggested we executed the code using the AsposePDF 19.4 Java library, but we still get the execption in
our linux envs.
Indeed we executed the code in a windows machine (developer env) and there was no error/exception.

Can you please share your feedback on this scenario.

Best regards

@dmanahmed

We have tested the scenario in Linux environment i.e. CentOS 7 64-bit and were unable to notice any exception. However, would you please make sure that all essential core fonts are installed in the system where exception is being thrown. In case issue still persists, please share a sample console application which is able to reproduce the issue in Linux atmosphere. We will again test the scenario in our environment and address it accordingly.

Hi

We would like to share the list of fonts in our Linux machine.
Please share your feedback if any of the fonts are required and are missing w.r.t to the issue document.

Best regards,
MansoorFonts_Linux_Env.zip (1.7 KB)

@dmanahmed

We are afraid that only the list of fonts may not be sufficient for investigating it. Kindly share a sample application containing SSCCE code so that we may investigate further.

Hi

We found another PDF where stamp fails. We tried the attached code using aspose-pdf-19.6-jdk17.jar and JRE 1.8.0_121 on a Windows7 Professional SP1 64 bit version

The attachment also contains the PDF having the issue.
Please share your feedback on how to fix this.
TestAsposePDF.zip (44.0 KB) Aspose_AutostampException.PNG (9.0 KB)

Note: One thing we observed is when we do not load aspose pdf license in the program
we do not get the error but neither does the pdf pages get stamped. Can you help us understand this.

Best regards,
Mansoor

@dmanahmed

Thank you for sharing the details.

We have worked with the data shared by you and have been able to reproduce the issue in our environment. A ticket with ID PDFJAVA-38678 has been logged in our issue management system for further investigations. We will let you know as soon as any significant update will be available in this regard.

Moreover, in evaluation mode, only 4 members of any collection can be processed so the error may not reproduce when license is not set.

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