Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NullPointerException using PdfCopyFields (broken since v1.1.0) #500

Closed
StefanOltmann opened this issue Mar 8, 2021 · 6 comments · Fixed by #516
Closed

NullPointerException using PdfCopyFields (broken since v1.1.0) #500

StefanOltmann opened this issue Mar 8, 2021 · 6 comments · Fixed by #516

Comments

@StefanOltmann
Copy link

Describe the bug

Here is a sample document with a table of contents on the first page: openpdf_bug_test.pdf

I want to copy only the first page into a new file using code like this...

            InputStream stream = Main.class.getResourceAsStream("openpdf_bug_test.pdf");

            PdfReader reader = new PdfReader(stream);

            byte[] bytes;

            try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) {

                PdfCopyFields pdfCopyFields = new PdfCopyFields(baos);

                pdfCopyFields.addDocument(reader, "1"); // <-- just the table of contents

                pdfCopyFields.close(); // <-- bang

                baos.flush();

                bytes = baos.toByteArray();
            }

            try (OutputStream os = new FileOutputStream(new File("output.pdf"))) {
                os.write(bytes);
            }

... and get this exception:

java.lang.NullPointerException
	at com.lowagie.text.pdf.PdfWriter$PdfBody.add(PdfWriter.java:394)
	at com.lowagie.text.pdf.PdfWriter$PdfBody.add(PdfWriter.java:390)
	at com.lowagie.text.pdf.PdfWriter.addToBody(PdfWriter.java:829)
	at com.lowagie.text.pdf.PdfCopyFieldsImp.closeIt(PdfCopyFieldsImp.java:408)
	at com.lowagie.text.pdf.PdfCopyFieldsImp.close(PdfCopyFieldsImp.java:368)
	at com.lowagie.text.pdf.PdfCopyFields.close(PdfCopyFields.java:162)
	at Main.main(Main.java:30)

Since OpenPDF v1.1.0 it only works if I specify "1-2" as a range because otherwise a NullPointerException occurs due to the missing page refs in the table of contents.

Up to v1.0.5 it was possible to just copy range "1" and the missing page refs will be ignored.

To Reproduce

Full reproducer project:
OpenPdfBugReproducer.zip

Expected behavior

If a page has a reference to another page that's not part of the exported range this reference should just be ignored or stripped out. This seems to be what happens in version 1.0.5 and the old iText library.

Maybe you won't want to allow this per default, but then there should be a way better exception than a NullPointerException. But I would still need an option to force that copy even if page references get lost.

System (please complete the following information):

  • OS: Windows
  • Used Font: Not relevant
@asturio
Copy link
Member

asturio commented Mar 20, 2021

Pull requests are welcome!

@Wugengxian
Copy link

Pull requests are welcome!
I have fixed it solution

@Wugengxian
Copy link

@StefanOltmann I have fixed it, you can find the pull request

@StefanOltmann
Copy link
Author

@StefanOltmann I have fixed it, you can find the pull request

Thank you! 😀

@asturio asturio mentioned this issue Apr 22, 2021
@asturio asturio linked a pull request Apr 22, 2021 that will close this issue
@asturio asturio reopened this Apr 22, 2021
@StefanOltmann
Copy link
Author

@asturio Nice.

When will the next release to maven central happen?

@asturio
Copy link
Member

asturio commented Apr 25, 2021

Let's see if I can go through some PR this weekend, so I can release in the next week. :-). It's great to have so much contribution.

@asturio asturio added this to the 1.3.26 milestone May 2, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants