当尝试打开使用iText库创建的pdf时,Adobe阅读器在mac上显示错误

时间:2022-07-03 21:10:08

I have created PDF using iText library with dynamic document size . Document size is depends on content. It is opening fine in windows but when trying to open in MAC its show error "message error exist on this . page acrobat may not display the page correctly. Please contact the person who created the PDF document."

我使用具有动态文档大小的iText库创建了PDF。文档大小取决于内容。它在Windows中打开正常,但是当尝试在MAC中打开时,显示错误“此消息错误。页面acrobat可能无法正确显示页面。请联系创建PDF文档的人员。”

I have added image in header using absolute position. When I remove this image then it working fine. I have check every pixel there is no any pixel is overlapping each others. I don't know exactly what wrong in this code.

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

Find my code below

在下面找到我的代码

    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();

当尝试打开使用iText库创建的pdf时,Adobe阅读器在mac上显示错误

My pdf looks like this. I have added above code in onEndPage() event this code is for generate header and footer part.

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

当尝试打开使用iText库创建的pdf时,Adobe阅读器在mac上显示错误

1 个解决方案

#1


2  

You are adding content using PdfContentByte which means that you consider yourself as proficient in PDF. However, I see that you have the following line:

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

cb.beginText();

This opens a text object. Inside a text object, there are some strict rules that you need to follow. For instance: the first thing you add to the direct content after beginning a text object is an image. That's not correct, is it?

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

Also, you cannot have a beginText() without an endText().

此外,您不能没有endText()的beginText()。

This is the most blatant error in your code. You may have other errors. While some PDF viewers may be tolerant towards people who violate the PDF specification, others are more strict.

这是代码中最明显的错误。您可能有其他错误。虽然一些PDF查看者可能会容忍违反PDF规范的人,但其他人则更为严格。

#1


2  

You are adding content using PdfContentByte which means that you consider yourself as proficient in PDF. However, I see that you have the following line:

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

cb.beginText();

This opens a text object. Inside a text object, there are some strict rules that you need to follow. For instance: the first thing you add to the direct content after beginning a text object is an image. That's not correct, is it?

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

Also, you cannot have a beginText() without an endText().

此外,您不能没有endText()的beginText()。

This is the most blatant error in your code. You may have other errors. While some PDF viewers may be tolerant towards people who violate the PDF specification, others are more strict.

这是代码中最明显的错误。您可能有其他错误。虽然一些PDF查看者可能会容忍违反PDF规范的人,但其他人则更为严格。