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."
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. I have added above code in onEndPage()
event this code is for generate header and footer part.
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:
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()
.
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.