奇怪的 IText 7 行为 - 错误

Strange IText 7 Behavior - Bug

我正在使用 ITextPdf 7.1.1 Java Library 为项目创建报告。问题是浏览器内部生成的 pdf 看起来不错。但是,当我将其另存为 pdf 并使用 pdf reader(预览)打开它时,它会在 div 元素中生成边框。下面是问题的图片。

也许问题出在我用来生成 Header 的 PDFCanvas Rectancle 处。

PDF 在浏览器内生成:

保存到磁盘后在预览中打开的 PDF:

生成Header的代码如下:

 @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: histologypathology@gmail.com");
        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();

    }

已将 Vestion 更改为 7.1.5,但我仍然遇到相同的行为。有什么建议么??过去没有其他人遇到过这个问题吗?

请删除这条指令:

pdfCanvas.rectangle(rectangle);

这会在内容中创建一个路径。在路径定义(以及可选的路径裁剪指令)之后,必须紧跟路径绘制指令,参见。以下 ISO 32000-1 中“图 9 – 图形对象”的副本。相反,在你的情况下你做

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

即您添加一条设置字体和大小的指令,而不是所需的路径绘制指令。

因此,生成的页面内容流是无效的。

现在 PDF 查看器通常会尝试理解损坏的内容。在你的情况下,他们必须决定:

  • 他们可以简单地忽略路径定义。 (这就是您的浏览器所执行的操作。)
  • 他们会假设您忘记了绘图说明,而只是简单地画出路径。 (这就是您的 PDF reader 预览所做的。)

不同状态下允许的指令

(ISO 32000-1 Figure 9 – Graphics Objects)