某些pdf文件水印未使用iText显示 [英] Some pdf file watermark does not show using iText

查看:167
本文介绍了某些pdf文件水印未使用iText显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们公司使用iText在某些pdf表单上标记一些水印文本(而非图像)。我注意到95%的表格正确显示水印,约5%没有。我测试了,复制了2个原始pdf文件,一个标记为ok,另一个没有问题,然后通过一个小程序测试,结果相同:一个得到标记,另一个没有。然后我尝试了最新版本的iText jar文件(版本5.0.6),同样的事情。我检查了pdf文件属性,安全设置等,似乎没有显示任何提示。结果文件确实改变了大小并在执行程序后标记为由iText版本改变....。

Our company using iText to stamp some watermark text (not image) on some pdf forms. I noticed 95% forms shows watermark correctly, about 5% does not. 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. I then tried the latest version of iText jar file (version 5.0.6), same thing. I checked pdf file properties, security settings etc, seems nothing shows any hint. The result file does changed size and markd "changed by iText version...." after executed program.

这是示例水印代码(使用itext jar版本2.1。 7),注意传入的topText,mainText,bottonText参数,使3行水印在pdf中显示为水印。

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.

任何帮助赞赏!!

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


推荐答案

我们通过更改Adobe LifecycleSave文件选项解决问题。文件 - >保存 - >属性 - >另存为,然后查看另存为类型,默认为Acrobat 7.0.5动态PDF表单文件,我们更改为使用7.0.5静态PDF表单文件(实际上任何静态的都可以工作)。保存在静态文件中的文件没有此水印消失问题。感谢Mark指出了正确的方向。

We solved problem by change Adobe LifecycleSave file option. 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). File saved in static one do not have this watermark disappear problem. Thanks Mark for pointing to the right direction.

这篇关于某些pdf文件水印未使用iText显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆