使用iText java创建带有水印图像的PDF文件

使用iText java创建带有水印图像的PDF文件,java,itext,itext7,Java,Itext,Itext7,将pdf文件发送到打印机时,会出现类似“此页面上存在错误。Acrobat可能无法正确显示该页面。请与创建pdf文档的人员联系以更正此问题。” 我正在创建一个PDF文件,并使用文本java向其中添加水印图像 如果从PDF文件中删除水印图像,则其工作正常 不知道水印图像的确切问题是什么?请帮忙 下面是代码片段: PdfReader pdfReader = new PdfReader(finalPath); int noOfPages = pdfReader.getNumberOfPages();

将pdf文件发送到打印机时,会出现类似“此页面上存在错误。Acrobat可能无法正确显示该页面。请与创建pdf文档的人员联系以更正此问题。”

我正在创建一个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();
PdfReader PdfReader=新的PdfReader(最终路径);
int noOfPages=pdfReader.getNumberOfPages();
PdfStamper stamp=新PdfStamper(pdfReader,新文件输出流(fileNameAfterWatermark));
int i=0;
PdfContentByte内容不足;
PDFGGS状态;
而(我
您将水印内容添加到
目录下,如下所示:

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

此外,您还可以将该内容添加到
目录下。因此,在
PdfGState
中设置的透明度只会使图像更苍白。如果可以将原始位图设置为最终所需的颜色,则根本不需要使用该
PdfGState
。在某些PDF档案中,透明度是被禁止的,因此消除它也可能是有利的…

您能展示一些代码并共享PDF吗?不正确使用iText可能会创建半个无法打开的PDF。正确使用创建正确的PDF(除非在处理PDF后显示的源PDF或图像已经存在问题)。你当然明白,如果你不提供信息,没有人能帮助你。你好,布鲁诺,谢谢你的指导。我已经提到了上面的代码片段。您可以将图像添加到文本对象的内部内容中。这是无效的,@mkl解决了问题。如果你查阅ISO32000,你会注意到你的代码创建的PDF违反了标准。谢谢,它现在可以工作了。