Free Support Forum - aspose.com

Save pdf doc to ByteArrayOutputstream throws exception


#1

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


#2

@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.


#3

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


#4

@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.


#5

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)


#6

@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.