用PDFBox加水印

我正在尝试使用PDFBox专门为PDF添加水印。我已经能够使图像显示在每个页面上,但是它失去了背景透明度,因为它看起来好像PDJpeg将其转换为JPG。也许有一种使用PDXObjectImage的方法。

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

public static void watermarkPDF(PDDocument pdf) throws IOException

{

// Load watermark

BufferedImage buffered = ImageIO.read(new File("C:\\PDF_Test\\watermark.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_Test\\watermarktest.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候选版本的示例

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, 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.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");

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

希望这可以帮助。!

以上是 用PDFBox加水印 的全部内容, 来源链接: utcz.com/qa/429719.html

回到顶部