Adobe reader 在尝试打开使用 iText 库创建的 pdf 时在 mac 上显示错误

Adobe reader shows error on mac when trying to open pdf which is created using iText library

我使用 iText 具有动态文档大小的库创建了 PDF。 文档大小取决于内容。 它在 windows 中正常打开,但是当试图在 MAC 中打开时显示错误 "message error exist on this . page acrobat may not display the page correctly. Please contact the person who created the PDF document."

我使用绝对位置在 header 中添加了图像。 当我删除此图像时,它工作正常。 我检查了每个像素,没有任何像素相互重叠。 我不知道这段代码到底有什么问题。

在下面找到我的代码

    PdfContentByte cb = writer.getDirectContent();
    cb.saveState();
    try {
        //Header and Footer Setting
        float textBase = document.bottom();
        float textTop = document.top();
        String ramboAccounturl = docRoot + RamboConstants.DEFAULT_LOGO_IMAGE_PATH;
        Image ramboImage = Image.getInstance(ramboAccounturl);
        String reviewName = displayName;//"Review: " + review.getReviewName();

        //Header Horizontal Line
        cb.setLineWidth(1);
        cb.closePath();
        //Footer Horizontal Line
        float footerY = document.bottom();
        footerY += 10;
        cb.setColorStroke(BaseColor.BLACK);;
        cb.moveTo(document.left(), footerY);
        cb.lineTo(document.right(), footerY);
        cb.stroke();
        cb.setColorFill(BaseColor.BLACK);
        Image bkgImage = Image.getInstance(docRoot + RamboConstants.BACKGROUNG_IMAGE);
        cb.addImage(bkgImage, document.right() - document.rightMargin() + 35f, 0, 0, RamboConstants.PDF_LOGO_MAX_HEIGHT + 8f,
                document.left() - 20, textTop - 18f);
        cb.beginText();
        //account logo image
        account.setDirectS3Download(RamboConstants.TRUE);
        Image image = null;
        String accountLogoUrl = ramboContext.getFileManager().buildFileUrl(account, RamboConstants.DOCTYPE_LOGO_HEADER_ACCOUNT, null);
        if (accountLogoUrl != null) {
            try {
                image = Image.getInstance(new URL(accountLogoUrl));
            } catch(Exception e) {
                ramboAccounturl = docRoot + RamboConstants.DEFAULT_LOGO_WHITE_IMAGE_PATH;
                image = Image.getInstance(ramboAccounturl);
            }
        } else {
            ramboAccounturl = docRoot + RamboConstants.DEFAULT_LOGO_WHITE_IMAGE_PATH;
            image = Image.getInstance(ramboAccounturl);
        }
        image.setAbsolutePosition(document.left() - 4f, textTop - 12f);
        float width = image.getWidth() * RamboConstants.PDF_REVIEW_PIXEL_TO_USER_POINT_CONVERSION_RATE;
        float height = image.getHeight() * RamboConstants.PDF_REVIEW_PIXEL_TO_USER_POINT_CONVERSION_RATE;
        if (width > RamboConstants.PDF_LOGO_MAX_WIDTH){ // source is wider than target in proportion
            float ratio = RamboConstants.PDF_LOGO_MAX_WIDTH / width;
            width = width * ratio;
            height = height * ratio;      
        }
        if (height > RamboConstants.PDF_LOGO_MAX_HEIGHT){ 
            float ratio = RamboConstants.PDF_LOGO_MAX_HEIGHT / height;
            width = width * ratio;
            height = height * ratio;      
        } 
        image.scaleAbsoluteWidth(width);
        image.scaleAbsoluteHeight(height);
        cb.addImage(image);

        //review name
        cb.setColorFill(BaseColor.WHITE);
        cb.setFontAndSize(helv , RamboConstants.PDF_REVIEW_NAME_FONT_SIZE);
        cb.setTextMatrix(document.right() - helv.getWidthPoint(reviewName, RamboConstants.PDF_REVIEW_NAME_FONT_SIZE) - 
                40, textTop + 5);
        cb.showText(reviewName);

        cb.setColorFill(BaseColor.BLACK);
        //rambo account logo
        ramboImage.setAbsolutePosition(document.left(), textBase - 25);
        width = ramboImage.getWidth();
        height = ramboImage.getHeight();
        if (width > RamboConstants.PDF_ROBOHEAD_LOGO_MAX_WIDTH){ // source is wider than target in proportion
            float ratio = RamboConstants.PDF_ROBOHEAD_LOGO_MAX_WIDTH / width;
            width = width * ratio;
            height = height * ratio;
        }
        if (height > RamboConstants.PDF_ROBOHEAD_LOGO_MAX_HEIGHT){ 
            float ratio = RamboConstants.PDF_ROBOHEAD_LOGO_MAX_HEIGHT / height;
            width = width * ratio;
            height = height * ratio;      
        } 
        ramboImage.scaleAbsoluteWidth(width);
        ramboImage.scaleAbsoluteHeight(height);
        cb.addImage(ramboImage);
        //powered by text
        String poweredByText = ramboContext.getMessageSource().getMessage("msg_footer_powered_by", null, Locale.getDefault());
        cb.setFontAndSize(BaseFont.createFont(BaseFont.HELVETICA_BOLD, BaseFont.WINANSI, BaseFont.NOT_EMBEDDED), 
                10);
        cb.setTextMatrix(document.left() + 70, 20);
        cb.showText(poweredByText);

        //Page number
        String text = "Page " + writer.getPageNumber() + " of ";
        cb.setFontAndSize(helv , RamboConstants.PDF_FOOTER_FONT_SIZE);
        cb.setTextMatrix(document.right() - helv.getWidthPoint(text, RamboConstants.PDF_FOOTER_FONT_SIZE) - 
                RamboConstants.PDF_FOOTER_FONT_SIZE, 20);
        cb.showText(text);
        cb.endText();
        cb.addTemplate(total, document.right() - RamboConstants.PDF_FOOTER_FONT_SIZE, 20);  

    } catch (Exception e) {
        throw new RuntimeException("Failed to add header footer to PDF page ");
    }

    cb.restoreState();

我的 pdf 看起来像这样。我在 onEndPage() 事件中添加了上面的代码,此代码用于生成 header 和页脚部分。

您正在使用 PdfContentByte 添加内容,这意味着您认为自己精通 PDF。但是,我看到您有以下行:

cb.beginText();

这将打开一个文本对象。在文本对象内部,您需要遵循一些严格的规则。例如:在开始文本对象后添加到直接内容的第一件事是图像。这不正确,是吗?

此外,没有 endText() 就不能有 beginText()

这是您代码中最明显的错误。您可能还有其他错误。虽然一些 PDF 查看器可能对违反 PDF 规范的人持宽容态度,但其他人则更为严格。