简体   繁体   English

某些pdf文件水印不会显示使用iText

[英]Some pdf file watermark does not show using iText

Our company using iText to stamp some watermark text (not image) on some pdf forms. 我们公司使用iText在一些pdf表格上标记一些水印文本(不是图像)。 I noticed 95% forms shows watermark correctly, about 5% does not. 我注意到95%的表格正确显示水印,约5%没有。 I tested, copy 2 original pdf files, one was marked ok, other one does not ok, then tested in via a small program, same result: one got marked, the other does not. 我测试了,复制了2个原始pdf文件,一个标记为ok,另一个没有问题,然后通过一个小程序测试,结果相同:一个得到标记,另一个没有。 I then tried the latest version of iText jar file (version 5.0.6), same thing. 然后我尝试了最新版本的iText jar文件(版本5.0.6),同样的事情。 I checked pdf file properties, security settings etc, seems nothing shows any hint. 我检查了pdf文件属性,安全设置等,似乎没有显示任何提示。 The result file does changed size and markd "changed by iText version...." after executed program. 结果文件确实改变了大小,并在执行程序后标记为“由iText版本改变....”。

Here is the sample watermark code (using itext jar version 2.1.7), note topText, mainText, bottonText parameters passed in, make 3 lines of watermarks show in the pdf as watermark. 这是样本水印代码(使用itext jar版本2.1.7),注意传入的topText,mainText,bottonText参数,使3行水印在pdf中显示为水印。

Any help appreciated !! 任何帮助赞赏!!

public class WatermarkGenerator {

    private static int TEXT_TILT_ANGLE = 25;
    private static Color MEDIUM_GRAY = new Color(160, 160, 160);
    private static int SUPPORT_FONT_SIZE = 42;
    private static int PRIMARY_FONT_SIZE = 54;

    public static void addWaterMark(InputStream pdfInputStream,
        OutputStream outputStream, String topText, 
        String mainText, String bottomText) throws Exception {
        PdfReader reader = new PdfReader(pdfInputStream);
        int numPages = reader.getNumberOfPages();

        // Create a stamper that will copy the document to the output
        // stream.
        PdfStamper stamp = new PdfStamper(reader, outputStream);
        int page=1;

        BaseFont baseFont = 
            BaseFont.createFont(BaseFont.HELVETICA_BOLDOBLIQUE,
                BaseFont.WINANSI, BaseFont.EMBEDDED);

        float width;
        float height;

        while (page <= numPages) {
            PdfContentByte cb = stamp.getOverContent(page);
            height = reader.getPageSizeWithRotation(page).getHeight() / 2;
            width = reader.getPageSizeWithRotation(page).getWidth() / 2;

            cb = stamp.getUnderContent(page);
            cb.saveState();
            cb.setColorFill(MEDIUM_GRAY);

            // Top Text
            cb.beginText();
            cb.setFontAndSize(baseFont, SUPPORT_FONT_SIZE);
            cb.showTextAligned(Element.ALIGN_CENTER, topText, width,
                    height+PRIMARY_FONT_SIZE+16, TEXT_TILT_ANGLE);
            cb.endText();

            // Primary Text
            cb.beginText();
            cb.setFontAndSize(baseFont, PRIMARY_FONT_SIZE);
            cb.showTextAligned(Element.ALIGN_CENTER, mainText, width,
                    height, TEXT_TILT_ANGLE);
            cb.endText();

            // Bottom Text
            cb.beginText();
            cb.setFontAndSize(baseFont, SUPPORT_FONT_SIZE);
            cb.showTextAligned(Element.ALIGN_CENTER, bottomText, width,
                    height-PRIMARY_FONT_SIZE-6, TEXT_TILT_ANGLE);
            cb.endText();
            cb.restoreState();

            page++;
        }

        stamp.close();
    }
}

We solved problem by change Adobe LifecycleSave file option. 我们通过更改Adobe LifecycleSave文件选项解决了问题。 File->Save->properties->Save as, then look at Save as type, default is Acrobat 7.0.5 Dynamic PDF Form File, we changed to use 7.0.5 Static PDF Form File (actually any static one will work). 文件 - >保存 - >属性 - >另存为,然后查看另存为类型,默认为Acrobat 7.0.5动态PDF表单文件,我们更改为使用7.0.5静态PDF表单文件(实际上任何静态的都可以工作)。 File saved in static one do not have this watermark disappear problem. 保存在静态文件中的文件没有此水印消失问题。 Thanks Mark for pointing to the right direction. 感谢Mark指出了正确的方向。

You're using the underContent rather than the overContent. 你正在使用underContent而不是overContent。 Don't do that. 不要那样做。 It leaves you at the mercy of big, white-filled rectangles that some folks insist on drawing first thing. 它让你受到大白色矩形的摆布,一些人坚持要先画一些东西。 It's a hold over from less-than-good PostScript interpreters and hasn't been necessary for Many Years. 这是一个不太好的PostScript解释器,并且多年来一直没有必要。


Okay, having viewed your PDF, I can see the problem is that this is an XFA-based form (from LiveCycle Designer). 好的,看过你的PDF后,我发现问题在于这是一个基于XFA的表格(来自LiveCycle Designer)。 Acrobat can (and often does) rebuild the entire file based on the XFA (a type of xml) it contains. Acrobat可以(并且经常)根据它包含的XFA(一种xml)重建整个文件。 That's how your changes are lost. 这就是你的改变丢失的方式。 When Acrobat rebuilds the PDF from the XFA, all the existing PDF information is pitched, including your watermark. 当Acrobat从XFA重建PDF时,所有现有的PDF信息都会被调整,包括您的水印。

The only way to get this to work would be to define the watermark as part of the XFA file contained in the PDF. 实现此功能的唯一方法是将水印定义为PDF中包含的XFA文件的一部分。

Detecting these forms isn't all that hard: 检测这些形式并不是那么难:

PdfReader reader = new PdfReader(...);
AcroFields acFields = reader.getAcroFields();
XfaForm xfaForm = acFields.getXfaForm();
if (xfaForm != null && xfaForm.isXfaPresent()) {
  // Ohs nose.
  throw new ItsATrapException("We can't repel XML of that magnitude!");
}

Modifying them on the other hand could be Quite Challenging, but here's the specs . 另一方面,修改它们可能是相当具有挑战性的,但这是规格

Once you've figured out what needs to be changed, it's a simple matter of XML manipulation... but that "figure it out" part could be interesting . 一旦你弄清楚需要改变什么,这是一个简单的XML操作问题......但是“弄清楚”部分可能很有趣

Good hunting. 好狩猎。

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

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