加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

java – 使用PDFBox加水印

发布时间:2020-12-15 07:38:38 所属栏目:Java 来源:网络整理
导读:我正在尝试使用PDFBox专门为PDF添加水印.我已经能够让图像显示在每个页面上,但它会失去背景透明度,因为它看起来好像PDJpeg将其转换为JPG.也许有一种方法可以使用PDXObject Image来完成它. 这是我到目前为止所写的内容: public static void watermarkPDF(PDD
我正在尝试使用PDFBox专门为PDF添加水印.我已经能够让图像显示在每个页面上,但它会失去背景透明度,因为它看起来好像PDJpeg将其转换为JPG.也许有一种方法可以使用PDXObject Image来完成它.

这是我到目前为止所写的内容:

public static void watermarkPDF(PDDocument pdf) throws IOException
{
    // Load watermark
    BufferedImage buffered = ImageIO.read(new File("C:PDF_Testwatermark.png"));
    PDJpeg watermark = new PDJpeg(pdf,buffered);

    // Loop through pages in PDF
    List pages = pdf.getDocumentCatalog().getAllPages();
    Iterator iter = pages.iterator();
    while(iter.hasNext())
    {
        PDPage page = (PDPage)iter.next();

        // Add watermark to individual page
        PDPageContentStream stream = new PDPageContentStream(pdf,page,true,false);
        stream.drawImage(watermark,100,0);
        stream.close();
    }

    try 
    {
        pdf.save("C:PDF_Testwatermarktest.pdf");
    } 
    catch (COSVisitorException e) 
    {
        e.printStackTrace();
    }
}

解决方法

更新的答案(更好的版本,简单的水印方式,感谢下面的评论员和@okok提供输入他的答案)

如果您使用的是PDFBox 1.8.10或更高版本,则可以轻松地为PDF文档添加水印,从而更好地控制需要加水印的页面.假设您有一个带有水印图像的单页PDF文档,您可以将其叠加在要添加水印的文档上,如下所示.

示例代码使用1.8.10

import java.util.HashMap;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.util.Overlay;

public class TestPDF {
    public static void main(String[] args) throws Exception{
            PDDocument realDoc = PDDocument.load("originaldocument.pdf"); 
            //the above is the document you want to watermark                   

            //for all the pages,you can add overlay guide,indicating watermark the original pages with the watermark document.
            HashMap<Integer,String> overlayGuide = new HashMap<Integer,String>();
            for(int i=0; i<realDoc.getPageCount(); i++){
                overlayGuide.put(i+1,"watermark.pdf");
                //watermark.pdf is the document which is a one page PDF with your watermark image in it. Notice here that you can skip pages from being watermarked.
            }
            Overlay overlay = new Overlay();
            overlay.setInputPDF(realDoc);
            overlay.setOutputFile("final.pdf");
            overlay.setOverlayPosition(Overlay.Position.BACKGROUND);
            overlay.overlay(overlayGuide,false);
           //final.pdf will have the original PDF with watermarks.

使用PDFBox 2.0.0 Release candidate的示例

import java.io.File;
import java.util.HashMap;
import org.apache.pdfbox.multipdf.Overlay;
import org.apache.pdfbox.pdmodel.PDDocument;

public class TestPDF {

    public static void main(String[] args) throws Exception{        
        PDDocument realDoc = PDDocument.load(new File("originaldocument.pdf"));
        //the above is the document you want to watermark
        //for all the pages,indicating watermark the original pages with the watermark document.

        HashMap<Integer,String>();
        for(int i=0; i<realDoc.getNumberOfPages(); i++){
            overlayGuide.put(i+1,"watermark.pdf");
            //watermark.pdf is the document which is a one page PDF with your watermark image in it. 
            //Notice here,you can skip pages from being watermarked.
        }
        Overlay overlay = new Overlay();
        overlay.setInputPDF(realDoc);
        overlay.setOutputFile("final.pdf");
        overlay.setOverlayPosition(Overlay.Position.BACKGROUND);
        overlay.overlay(overlayGuide);      
    }
}

如果要使用新包org.apache.pdfbox.tools.OverlayPDF进行叠加,可以这样做. (感谢下面的海报)

String[] overlayArgs = {"C:/Examples/foreground.pdf","C:/Examples/background.pdf","C:/Examples/resulting.pdf"};
OverlayPDF.main(overlayArgs);
System.out.println("Overlay finished.");

老答案效率不高,不推荐.

好吧,OP询问如何在PDFBox中执行此操作,第一个答案看起来像是使用iText的示例.在PDFBox中创建水印非常简单.诀窍是,你应该有一个带有水印图像的空PDF文档.然后,您所要做的就是在要添加水印的文档上覆盖此水印文档.

PDDocument watermarkDoc = PDDocument.load("watermark.pdf");
//Assuming your empty document with watermark image in it.

PDDocument realDoc = PDDocument.load("document-to-be-watermarked.pdf");
//Let's say this is your document that you want to watermark. For example sake,I am opening a new one,you would already have a reference to PDDocument if you are creating one

Overlay overlay = new Overlay();
overlay.overlay(realDoc,watermarkDoc);
watermarkDoc.save("document-now-watermarked.pdf");

警告:您应确保匹配两个文档中的页数.否则,您最终会得到一个文档,其页数与页数最少的页面相匹配.您可以操作水印文档并复制页面以匹配您的文档.

希望这可以帮助.!

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读