PDFStamper 是 iText 庫中的一個類,用于對現有的 PDF 文檔進行修改和處理。iText 是一個用于處理 PDF 文檔的 Java 庫,可以用于創建、編輯、簽名、合并等多種操作。
在 Java 中,PDFStamper 的實際應用場景包括:
PdfReader reader = new PdfReader("input.pdf");
PdfStamper stamper = new PdfStamper(reader, new FileOutputStream("output.pdf"));
PdfContentByte canvas = stamper.getOverContent(1);
canvas.beginText();
canvas.setFontAndSize(BaseFont.createFont(), 36);
canvas.showTextAligned(Element.ALIGN_CENTER, "Watermark", 297, 421, 45);
canvas.endText();
stamper.close();
PdfReader reader = new PdfReader("input.pdf");
PdfStamper stamper = new PdfStamper(reader, new FileOutputStream("output.pdf"));
PdfContentByte canvas = stamper.getOverContent(1);
canvas.beginText();
canvas.setFontAndSize(BaseFont.createFont(), 12);
canvas.showTextAligned(Element.ALIGN_LEFT, "Header", 36, 800, 0);
canvas.showTextAligned(Element.ALIGN_RIGHT, "Footer", 559, 36, 0);
canvas.endText();
stamper.close();
PdfReader reader = new PdfReader("input.pdf");
PdfStamper stamper = new PdfStamper(reader, new FileOutputStream("output.pdf"));
PdfAnnotation annotation = PdfAnnotation.createText(stamper.getWriter(), new Rectangle(100, 100, 200, 200), "Note", "This is a note.", true, "Comment");
stamper.addAnnotation(annotation, 1);
stamper.close();
PdfReader reader = new PdfReader("input.pdf");
FileOutputStream os = new FileOutputStream("output.pdf");
PdfStamper stamper = PdfStamper.createSignature(reader, os, '\0');
PdfSignatureAppearance appearance = stamper.getSignatureAppearance();
appearance.setReason("reason");
appearance.setLocation("location");
appearance.setVisibleSignature(new Rectangle(100, 100, 200, 200), 1, "sig");
ExternalSignature pks = new PrivateKeySignature(pk, DigestAlgorithms.SHA256, provider.getName());
ExternalDigest digest = new BouncyCastleDigest();
MakeSignature.signDetached(appearance, digest, pks, chain, null, null, null, 0, CryptoStandard.CMS);
這些只是 PDFStamper 在 Java 中的一些實際應用場景,還有更多其他功能可以根據需求進行使用。