使用 iText 创建的带有水印图像的 PDF 文件 java
PDF file with watermark image created using iText java
在向打印机发送 pdf 文件时出现 "An error exists on this page. Acrobat may not display the page correctly. Please contact the person who created the PDF document to correct the problem."
之类的错误
我正在创建一个 PDF 文件并使用文本 java 向其添加水印图像。
如果从 PDF 文件中删除水印图像,则它工作正常。
不知道水印图片的具体问题是什么?请帮忙
下面是代码片段:
PdfReader pdfReader = new PdfReader(finalPath);
int noOfPages = pdfReader.getNumberOfPages();
PdfStamper stamp = new PdfStamper(pdfReader, new FileOutputStream(fileNameAfterWatermark));
int i = 0;
PdfContentByte underContent;
PdfGState gs;
while (i < noOfPages) {
i++;
underContent = stamp.getUnderContent(i);
gs = new PdfGState();
gs.setFillOpacity(0.3f);
gs.setStrokeOpacity(0.3f);
Rectangle pagesize = pdfReader.getPageSize(i);
int pageRotation = pdfReader.getPageRotation(i);
float x = (pagesize.getLeft() + pagesize.getRight()) / 2 ;
float y = (pagesize.getTop() + pagesize.getBottom()) / 2 ;
if(pageRotation != 0){
x = (pagesize.getHeight()) / 2;
y = (pagesize.getWidth()) / 2;
y = y - 80;
}
float w = image.getScaledWidth();
float h = image.getScaledHeight();
float scaleMultiplicationFactor = 1.25f;
float image_width = (w * (scaleMultiplicationFactor));
float image_height = (h * (scaleMultiplicationFactor));
float x_co_ordinate = x - (image_width / 2 );
float y_co_ordinate = y - (image_height / 2);
int fontSize = 180;
underContent.saveState();
underContent.setGState(gs);
underContent.beginText();
underContent.setFontAndSize(bf, fontSize);
underContent.setColorFill(Color.LIGHT_GRAY);
underContent.addImage(image, image_width, 0, 0, image_height, x_co_ordinate , y_co_ordinate );
underContent.endText();
underContent.restoreState();
}
stamp.close();
pdfReader.close();
您将水印内容添加到 UnderContent
中,如下所示:
underContent.saveState();
underContent.setGState(gs);
underContent.beginText();
underContent.setFontAndSize(bf, fontSize);
underContent.setColorFill(Color.LIGHT_GRAY);
underContent.addImage(image, image_width, 0, 0, image_height, x_co_ordinate , y_co_ordinate );
underContent.endText();
underContent.restoreState();
即您将您的(位图?)图像添加到 文本对象内 。这是无效的,文本对象可能不包含外部对象或内联图像对象。在文本对象外添加图像:
underContent.saveState();
underContent.setGState(gs);
underContent.beginText();
underContent.setFontAndSize(bf, fontSize);
underContent.setColorFill(Color.LIGHT_GRAY);
underContent.endText();
underContent.addImage(image, image_width, 0, 0, image_height, x_co_ordinate , y_co_ordinate );
underContent.restoreState();
也就是说,您没有在该文本对象中添加任何内容。因此,您可以将代码简化为:
underContent.saveState();
underContent.setGState(gs);
underContent.addImage(image, image_width, 0, 0, image_height, x_co_ordinate , y_co_ordinate );
underContent.restoreState();
此外,您将该内容添加到 UnderContent
。因此,您在 PdfGState
中设置的透明度只会使图像变浅。如果您可以使原始位图像最终需要的那样苍白,则根本不需要使用 PdfGState
。在 PDF 的某些配置文件中,透明度是被禁止的,因此摆脱它也可能是有利的...
在向打印机发送 pdf 文件时出现 "An error exists on this page. Acrobat may not display the page correctly. Please contact the person who created the PDF document to correct the problem."
之类的错误我正在创建一个 PDF 文件并使用文本 java 向其添加水印图像。
如果从 PDF 文件中删除水印图像,则它工作正常。
不知道水印图片的具体问题是什么?请帮忙
下面是代码片段:
PdfReader pdfReader = new PdfReader(finalPath);
int noOfPages = pdfReader.getNumberOfPages();
PdfStamper stamp = new PdfStamper(pdfReader, new FileOutputStream(fileNameAfterWatermark));
int i = 0;
PdfContentByte underContent;
PdfGState gs;
while (i < noOfPages) {
i++;
underContent = stamp.getUnderContent(i);
gs = new PdfGState();
gs.setFillOpacity(0.3f);
gs.setStrokeOpacity(0.3f);
Rectangle pagesize = pdfReader.getPageSize(i);
int pageRotation = pdfReader.getPageRotation(i);
float x = (pagesize.getLeft() + pagesize.getRight()) / 2 ;
float y = (pagesize.getTop() + pagesize.getBottom()) / 2 ;
if(pageRotation != 0){
x = (pagesize.getHeight()) / 2;
y = (pagesize.getWidth()) / 2;
y = y - 80;
}
float w = image.getScaledWidth();
float h = image.getScaledHeight();
float scaleMultiplicationFactor = 1.25f;
float image_width = (w * (scaleMultiplicationFactor));
float image_height = (h * (scaleMultiplicationFactor));
float x_co_ordinate = x - (image_width / 2 );
float y_co_ordinate = y - (image_height / 2);
int fontSize = 180;
underContent.saveState();
underContent.setGState(gs);
underContent.beginText();
underContent.setFontAndSize(bf, fontSize);
underContent.setColorFill(Color.LIGHT_GRAY);
underContent.addImage(image, image_width, 0, 0, image_height, x_co_ordinate , y_co_ordinate );
underContent.endText();
underContent.restoreState();
}
stamp.close();
pdfReader.close();
您将水印内容添加到 UnderContent
中,如下所示:
underContent.saveState();
underContent.setGState(gs);
underContent.beginText();
underContent.setFontAndSize(bf, fontSize);
underContent.setColorFill(Color.LIGHT_GRAY);
underContent.addImage(image, image_width, 0, 0, image_height, x_co_ordinate , y_co_ordinate );
underContent.endText();
underContent.restoreState();
即您将您的(位图?)图像添加到 文本对象内 。这是无效的,文本对象可能不包含外部对象或内联图像对象。在文本对象外添加图像:
underContent.saveState();
underContent.setGState(gs);
underContent.beginText();
underContent.setFontAndSize(bf, fontSize);
underContent.setColorFill(Color.LIGHT_GRAY);
underContent.endText();
underContent.addImage(image, image_width, 0, 0, image_height, x_co_ordinate , y_co_ordinate );
underContent.restoreState();
也就是说,您没有在该文本对象中添加任何内容。因此,您可以将代码简化为:
underContent.saveState();
underContent.setGState(gs);
underContent.addImage(image, image_width, 0, 0, image_height, x_co_ordinate , y_co_ordinate );
underContent.restoreState();
此外,您将该内容添加到 UnderContent
。因此,您在 PdfGState
中设置的透明度只会使图像变浅。如果您可以使原始位图像最终需要的那样苍白,则根本不需要使用 PdfGState
。在 PDF 的某些配置文件中,透明度是被禁止的,因此摆脱它也可能是有利的...