Strange IText 7 Behavior - Bug

mixtou :

I am using ITextPdf 7.1.1 Java Library to create a report for a project. The problem is that the pdf generated inside the browser looks fine. However, when I save it as pdf and open it with a pdf reader (Preview) it generates borders in div element. Bellow are images of the problem.

Maybe the problem is at PDFCanvas Rectancle I use to generate the Headers.

PDF Generated Inside Browser: PDF Generated Inside Browser

PDF Opened In Preview After Saved To Disk:

PDF Opened In Preview After Saved To Disk

The Code that Generates The Header is below:

 @Override
    public void handleEvent(Event event) {
        System.out.println("THIS IS ME: HEADER EVENT HANDLER STARTED.....");


        PdfDocumentEvent documentEvent = (PdfDocumentEvent) event;
        PdfDocument pdfDoc = documentEvent.getDocument();
        PdfPage page = documentEvent.getPage();
        Rectangle pageSize = page.getPageSize();
        int pageNumber = pdfDoc.getPageNumber(page);

        String logoImagePath = null;

        ClassLoader classLoader = getClass().getClassLoader();
        logoImagePath = classLoader.getResource("/Images/logo.png").getPath();


        System.out.println("Page size: " + pageSize.getHeight());

        Rectangle rectangle = new Rectangle(pageSize.getLeft() + 30, pageSize.getHeight() - 114, pageSize.getWidth() - 60, 80);
        PdfCanvas pdfCanvas = new PdfCanvas(page.newContentStreamBefore(), page.getResources(), pdfDoc);
        pdfCanvas.rectangle(rectangle);
        pdfCanvas.setFontAndSize(FontsAndStyles.getRegularFont(), 10);
        Canvas canvas = new Canvas(pdfCanvas, pdfDoc, rectangle);

        Div header = new Div();
        header.setBorder(Border.NO_BORDER);

        try {
            Image logoImage = new Image(ImageDataFactory.create(logoImagePath));
            logoImage.setFixedPosition(15, 740);
            logoImage.scale(.15f, .15f);

            header.add(logoImage);
        } catch (MalformedURLException ex) {
            ex.printStackTrace();
            System.err.println(ex.getMessage());
        }

        Paragraph paragraph = new Paragraph();
        Text text = new Text("\n\n\n");
        paragraph.add(text);
        paragraph.setFont(FontsAndStyles.getRegularFont());
        paragraph.setFontSize(8);
        text = new Text("http://www.histopath.gr\n");
        paragraph.setTextAlignment(TextAlignment.RIGHT);
        paragraph.add(text);
        header.add(paragraph);
        paragraph = new Paragraph();
        text = new Text("Αποκορώνου 66, ΧΑΝΙΑ, Τηλ: 2821002827, Κιν: 6948571893, 6976800330, email: [email protected]");
        text.setFont(FontsAndStyles.getRegularFont());
        text.setFontSize(8);
        paragraph.add(text);
        paragraph.setTextAlignment(TextAlignment.JUSTIFIED_ALL);
        header.add(paragraph);


        header.setTextAlignment(TextAlignment.CENTER);
        canvas.add(header);
        canvas.setBorder(Border.NO_BORDER);
        canvas.close();

    }

Changed Vestion to 7.1.5 but i still get the same behavior. Any Suggestions?? Hasn't anybody else faced this issue in the past??

mkl :

Please remove this instruction:

pdfCanvas.rectangle(rectangle);

This creates a path in the content. After a path definition (and optionally a path clipping instruction) there must immediately follow a path painting instruction, cf. the copy of "Figure 9 – Graphics Objects" from ISO 32000-1 below. Instead in your case you do

pdfCanvas.setFontAndSize(FontsAndStyles.getRegularFont(), 10);

I.e. instead of the required path painting instruction, you add an instruction to set font and size.

Thus, the resulting page content stream is invalid.

Now PDF viewers usually try to make sense out of broken content. In your case they have to decide:

  • They can simply ignore the path definition. (This is what your browser appears to do.)
  • They can assume you forgot the drawing instruction and simply draw the path. (This is what your PDF reader Preview appears to do.)

Allowed instructions in different states

Figure 9 – Graphics Objects

(ISO 32000-1 Figure 9 – Graphics Objects)

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=160033&siteId=1