简体   繁体   English

尝试打开使用iText库创建的PDF时,Adobe Reader在Mac上显示错误

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

I have created PDF using iText library with dynamic document size . 我已经使用具有动态文档大小的iText库创建了PDF。 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." 它在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();

在此处输入图片说明

My pdf looks like this. 我的pdf看起来像这样。 I have added above code in onEndPage() event this code is for generate header and footer part. 我在onEndPage()事件中添加了以上代码,该代码用于生成页眉和页脚部分。

在此处输入图片说明

You are adding content using PdfContentByte which means that you consider yourself as proficient in PDF. 您正在使用PdfContentByte添加内容,这意味着您认为自己精通PDF。 However, I see that you have the following line: 但是,我看到您有以下一行:

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() . 另外,没有beginText() ,就无法拥有endText()

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规范的人,但其他人则更为严格。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM