[英]Adobe reader shows error on mac when trying to open pdf which is created using iText library
我已经使用具有动态文档大小的iText
库创建了PDF。 文档大小取决于内容。 它在windows
可以很好地打开,但是当尝试在MAC
打开时,其显示错误“此页面上存在消息错误。acrobat可能无法正确显示该页面。请与创建PDF文档的人联系。”
我已经使用绝对位置在标题中添加了图像。 当我删除该图像时,它工作正常。 我检查了每个像素,没有任何像素相互重叠。 我不知道这段代码到底出了什么问题。
在下面找到我的代码
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()
事件中添加了以上代码,该代码用于生成页眉和页脚部分。
您正在使用PdfContentByte
添加内容,这意味着您认为自己精通PDF。 但是,我看到您有以下一行:
cb.beginText();
这将打开一个文本对象。 在文本对象内部,需要遵循一些严格的规则。 例如:在开始一个文本对象之后,添加到直接内容中的第一件事就是图像。 那是不对的?
另外,没有beginText()
,就无法拥有endText()
。
这是代码中最明显的错误。 您可能还有其他错误。 尽管某些PDF查看器可以容忍违反PDF规范的人,但其他人则更为严格。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.