Uploaded image for project: 'PDFBox'
  1. PDFBox
  2. PDFBOX-4742

Incorrect handling of float Infinity and NaN

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Major
    • Resolution: Fixed
    • 2.0.8, 2.0.18
    • 2.0.19, 3.0.0 PDFBox
    • PDModel, Writing
    • None

    Description

      When creating document, there is an issue if the operand for a command is floating number positive or negative Infinity, or NaN.

      The problem is in method org.apache.pdfbox.pdmodel.PDPageContentStream#write. In case the method org.apache.pdfbox.pdmodel.PDPageContentStream#writeOperand(float) receives as operand a floating number with value Float.POSITIVE_INFINITY or Float.NEGATIVE_INFINITY the result of formating is symbol for infinity: ∞ (unicode 0x221E). The NaN produce unicode symbol 0xFFFD (replacement character). This is then later converted to bytes using the US_ASCII encoding which produces symbol ? (0x3F) this symbol is then put into the content stream and the resulting PDF is invalid.

       

      Mimimal working example to reproduce this issue.

       

      public class InfiniteError {
          public static void main(String[] args) throws IOException {
              PDDocument document = new PDDocument();
              PDPage page = new PDPage();
              document.addPage(page);        
              PDFont font = PDType1Font.HELVETICA_BOLD;
              PDPageContentStream contentStream = new PDPageContentStream(document, page, PDPageContentStream.AppendMode.OVERWRITE, false);
              contentStream.beginText();
              contentStream.setFont(font, 12);
              contentStream.newLineAtOffset(100, Float.POSITIVE_INFINITY); // incorrect value 
              contentStream.showText("Hello World");
              contentStream.endText();
              contentStream.close();
              document.save("Hello World.pdf");
              document.close();
          }
      }
      

       

      Attachments

        1. Hello World.pdf
          0.8 kB
          Jose Novacho

        Activity

          People

            tilman Tilman Hausherr
            jnovacho Jose Novacho
            Votes:
            1 Vote for this issue
            Watchers:
            3 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: