Jacobo Aragunde created PDFBOX-6061:
---------------------------------------

             Summary: NullPointerException in COSDictionary
                 Key: PDFBOX-6061
                 URL: https://issues.apache.org/jira/browse/PDFBOX-6061
             Project: PDFBox
          Issue Type: Bug
    Affects Versions: 3.0.5 PDFBox
            Reporter: Jacobo Aragunde
         Attachments: empty-reproducer.pdf

Load the attached PDF file and try to add some text, {*}reusing the PDFont 
object{*}. This code would reproduce the problem:

 
    void convert(OutputStream outputStream) \{
        PDDocument document = Loader.loadPDF(new 
File('C:\\path\\to\\empty-reproducer.pdf'))
        PDPage page = document.getPage(0)
        new PDPageContentStream(document, page, 
PDPageContentStream.AppendMode.APPEND, false, false).withCloseable { 
contentStream ->
            String text = 'this is some text'
            PDFont myFont = new PDType1Font(Standard14Fonts.FontName.HELVETICA)

            contentStream.beginText()
            contentStream.setFont(myFont, 12)
            contentStream.newLineAtOffset(25, 0)
            contentStream.showText(text)
            contentStream.endText()
            contentStream.beginText()
            contentStream.setFont(myFont, 12) // <-- Crash
            contentStream.newLineAtOffset(25, 25)
            contentStream.showText(text)
            contentStream.endText()
        }
        document.save(outputStream)
    }
The second call to `setFont` will produce this error:


Exception in thread "main" java.lang.NullPointerException: Cannot invoke 
"Object.equals(Object)" because the return value of 
"org.apache.pdfbox.cos.COSObject.getObject()" is null
        at 
org.apache.pdfbox.cos.COSDictionary.getKeyForValue(COSDictionary.java:110)
        at org.apache.pdfbox.pdmodel.PDResources.add(PDResources.java:714)
        at org.apache.pdfbox.pdmodel.PDResources.add(PDResources.java:598)
        at 
org.apache.pdfbox.pdmodel.PDAbstractContentStream.setFont(PDAbstractContentStream.java:218)
        at 
org.apache.pdfbox.pdmodel.PDPageContentStream.setFont(PDPageContentStream.java:37)
The PDF file has been hand-crafted to produce this error, sadly I cannot share 
the original reproducer. The source of the problem is a null reference in the 
Font section of the Resources dictionary. The second time we run `setFont`, the 
font already exists in the dictionary, so it iterates all the elements trying 
to retrieve the item and crashes on the null reference.

 



--
This message was sent by Atlassian Jira
(v8.20.10#820010)

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to