使用 pdfbox (1.7) 在包含图像的 pdf 上添加水印 [英] Add a watermark on a pdf that contains images using pdfbox (1.7)

查看:129
本文介绍了使用 pdfbox (1.7) 在包含图像的 pdf 上添加水印的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用了以下建议的代码:

I have used the code suggested in:

PDFBox 叠加失败

为现有的pdf添加水印.不幸的是,生成的 pdf 已损坏.当我打开文档时,pdf 阅读器抱怨:此页面上存在错误.Acrobat 可能无法正确显示页面.请联系创建 PDF 文档的人员以更正问题".

to add a watermark to an existing pdf. Unfortunately, the pdf produced is corrupted. The pdf reader complains when I open the document: "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".

文档已打开,但未显示图像.

The document is opened but it does not show the images.

似乎所有 pdf 都会发生这种情况.值得一提的是,它也发生在仅使用 Overlay 类的不同实现中.

It seems to happen with all the pdfs. It could be worth saying that it happens also with a different implementation that simply uses the Overlay class.

以下网址指向我用于测试的 pdf:

The following url points to a pdf that I used for my testing:

带有图片的 pdf

测试这个转换的代码是:

The code to test this transformation is:

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.pdfbox.cos.COSDictionary;
import org.apache.pdfbox.cos.COSName;
import org.apache.pdfbox.exceptions.COSVisitorException;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.common.PDStream;
import org.apache.pdfbox.pdmodel.edit.PDPageContentStream;
import org.apache.pdfbox.pdmodel.graphics.PDExtendedGraphicsState;
import org.apache.pdfbox.pdmodel.graphics.xobject.PDXObjectForm;
import org.apache.pdfbox.util.MapUtil;

/**
 * This test is about overlaying with special effect.
 * 
 * @author mkl
 */
public class OverlayWithEffect
{
    final static File RESULT_FOLDER = new File("target/test-outputs", "assembly");

    public static void overlayWithDarkenBlendMode(PDDocument document, PDDocument overlay) throws IOException
    {
        PDXObjectForm xobject = importAsXObject(document, (PDPage) overlay.getDocumentCatalog().getAllPages().get(0));
        PDExtendedGraphicsState darken = new PDExtendedGraphicsState();
        darken.getCOSDictionary().setName("BM", "Darken");

        List<PDPage> pages = document.getDocumentCatalog().getAllPages();

        for (PDPage page: pages)
        {
            if (page.getResources() == null) {
                page.setResources(page.findResources());
            }

            if (page.getResources() != null) {
                Map<String, PDExtendedGraphicsState> states = page.getResources().getGraphicsStates();
                if (states == null) {
                    states = new HashMap<String, PDExtendedGraphicsState>();
                }
                String darkenKey = MapUtil.getNextUniqueKey(states, "Dkn");
                states.put(darkenKey, darken);
                page.getResources().setGraphicsStates(states);
                PDPageContentStream stream = new PDPageContentStream(document, page, true, false, true);
                stream.appendRawCommands(String.format("/%s gs ", darkenKey));
                stream.drawXObject(xobject, 0, 0, 1, 1);
                stream.close();
            }
        }
    }

    public static PDXObjectForm importAsXObject(PDDocument target, PDPage page) throws IOException
    {
        final PDStream xobjectStream = new PDStream(target, page.getContents().createInputStream(), false);
        final PDXObjectForm xobject = new PDXObjectForm(xobjectStream);

        xobject.setResources(page.findResources());
        xobject.setBBox(page.findCropBox());

        COSDictionary group = new COSDictionary();
        group.setName("S", "Transparency");
        group.setBoolean(COSName.getPDFName("K"), true);
        xobject.getCOSStream().setItem(COSName.getPDFName("Group"), group);

        return xobject;
    }


    public static void main(String[] args) throws COSVisitorException, IOException
    {
        InputStream sourceStream = new FileInputStream("x:/pdf-test.pdf");
        InputStream overlayStream = new FileInputStream("x:/draft.pdf");
        try {
            final PDDocument document = PDDocument.load(sourceStream);
            final PDDocument overlay = PDDocument.load(overlayStream);

            overlayWithDarkenBlendMode(document, overlay);

            document.save("x:/da-draft-5.pdf");
            document.close();
        }
        finally {
            sourceStream.close();
            overlayStream.close();
        }
    }    
}

我使用的是 pdfbox 1.7 版.

I am using version 1.7 of pdfbox.

谢谢

推荐答案

按照 mkl 的建议,这可能是我使用的 pdfbox 版本的问题.

As suggested by mkl, it is probably an issue with the version of pdfbox that I am using.

这篇关于使用 pdfbox (1.7) 在包含图像的 pdf 上添加水印的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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