更新された回答(以下のコメンテーターと彼の回答を入力してくれた@okokのおかげで、透かしを入れる簡単な方法を備えたより良いバージョン)
PDFBox 1.8.10以降を使用している場合は、透かしを入れる必要のあるページをより適切に制御して、PDFドキュメントに透かしを簡単に追加できます。透かし画像を含む1ページの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.
PDFBox2.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");
注意:両方のドキュメントのページ数が一致していることを確認する必要があります。そうしないと、ページ数が最も少ないドキュメントと一致するページ数のドキュメントになってしまいます。透かしドキュメントを操作して、ドキュメントに一致するようにページを複製できます。
お役に立てれば。!