When trying to create a new com. aspose. pdf. Document(contentFilePath), the stamped pdf crashes with Exception

There is in PDF version 1.2, the stamp is put ONLY if you create PdfStamper in a new version (New PdfStamper (reader, FOS ‘\0’, true) - the last parameter is responsible for the new version), but when you try to create a new com.aspose.PDF.Document (contentFilePath) in stamped PDF format crashes:

Caused by: java.lang.NullPointerException
 at com.aspose.pdf.internal.l1if.l1v.<init>(Unknown Source)
 at com.aspose.pdf.internal.l6if.lf.lf(Unknown Source)
 at com.aspose.pdf.internal.l0h.l0p.lj(Unknown Source)
 at com.aspose.pdf.PageCollection.<init>(Unknown Source)
 at com.aspose.pdf.ADocument.l1h(Unknown Source)
 at com.aspose.pdf.ADocument.lI(Unknown Source)
 at com.aspose.pdf.ADocument.<init>(Unknown Source)
 at com.aspose.pdf.Document.<init>(Unknown Source)

in the debug I see the suppressedException error UnmodifiableRandomAccesslist in the collection of pages

Unfortunately I can’t attach the pdf file due to confidential data in it. Aspose 20.10, Itext 5.0.5.

PdfStamper stamper = null;
FileOutputStream fos = null;
PdfReader reader = null;
String contentFilePathOut = "C:\\DISTR\\out.pdf";
try {
    FileInputStream fis = new FileInputStream("C:\\DISTR\\source.pdf");
    reader = new PdfReader(fis);
    fos = new FileOutputStream(contentFilePathOut, true);
    stamper = new PdfStamper(reader, fos, '\0', true);
    PdfContentByte canvas = stamper.getOverContent(1);
    ColumnText.showTextAligned(canvas, Element.ALIGN_LEFT, new Phrase("Hello people!"), 250, 750, 0);
} finally {
    try {
        if (stamper != null) {
            stamper.close();
        }
    } catch (Exception ignore) {
    }

    try {
        if (fos != null) {
            fos.close();
        }
    } catch (Exception ignore) {

    }
    try {
        if (reader != null) {
            reader.close();
        }
    } catch (Exception ignore) {
    }
}
try {
    com.aspose.pdf.Document pdf = new com.aspose.pdf.Document(contentFilePathOut);
} catch (Exception e) {
    throw new ServiceException(e);
}

20201203153425287-1 (20).pdf (1.9 MB)

@papaJones

Would you please share your sample PDF document as well with which the Document constructor is raising the exception. We will test the scenario in our environment and address it accordingly. Please note that your attachments will be secured as nobody else except you and Aspose Staff would be able to download them.

@asad.ali I attached problem file, hope this helps

@papaJones

We tried to load your shared PDF document while using Aspose.PDF for Java 20.11 and did not face any issue. The API did not throw any kind of exception. Would you kindly make sure to use the latest version of the API and if you still face any issue, please feel free to let us know.

Updated Aspose.PDF for Java up to 20.11, but it didn’t help, ecxeption(specified in bugrequest) is stably caught in the penultimate line of code

@papaJones

The document which you have shared with us could not replicate the exception at our side. We tried loading the document using the same line of code i.e.:

com.aspose.pdf.Document pdf = new com.aspose.pdf.Document("inputfile.pdf");

Furthermore, such exceptions occur when input/source document has some issues in it. It is quite possible that the document is being corrupted by the code other than Aspose.PDF. Please try catching all the exception in your above steps instead of leaving catch block empty and let us know in case you still notice that the issue is occurring due to Aspose.PDF only.

I added tracking exceptions instead of ignoring them, but it didn’t lead to anything. The error remains. However, if you get
com. aspose. pdf. Document pdf = new com.aspose.pdf.Document(“C:\DISTR\source.pdf”);
UNTIL the stamp is set, there are no errors.

I also noticed that if I do
new PdfStamper(reader, fos);,
the pdf file is broken

@papaJones

The issue seems related to iText library as Aspose.PDF is able to load the document without raising any exception. It seems like the iText is somewhere corrupting the file. Since you already have privilege to use Aspose.PDF, would you please try adding the stamp using Aspose.PDF for Java and let us know in case you still face any issue?

I redid the stamp layout using the Aspose API and it works! Thanks!

@papaJones

It is good to know that your issue has been resolved. Please keep using our API and let us know in case you need further assistance.