通过 Java 以 PDF 格式制作邮票

使用 Java 加盖印章 PDF 文档。使用 Aspose.PDF 以编程方式修改 PDF 文档

如何使用 Java 庫將圖章添加到 PDF

為了將文本圖章添加到PDF中,我們將使用[Aspose.PDF用於Java](https://products.aspose.com/pdf/java)API,這是一個功能豐富,功能強大且易於使用的Java平臺轉換API。您可以直接從 [Maven](https://repository.aspose.com/webapp/#/artifacts/browse/tree/General/repo/com/aspose/aspose-pdf)下載其最新版本,並通過將以下配置添加到 pom.xml 來將其安裝在基於 Maven 的專案中。

Repository

<repository>
    <id>AsposeJavaAPI</id>
    <name>Aspose Java AP</name>
    <url>https://releases.aspose.com/java/repo/</url>
</repository>

Dependency

<dependency>
<groupId>com.aspose</groupId>
<artifactId>aspose-pdf</artifactId>
<version>version of aspose-pdf API</version>
</dependency>

將圖章添加到 PDF 文件 Java


您需要 [Aspose.PDF for Java](https://releases.aspose.com/pdf/java)來嘗試環境中的代碼。

  1. 使用 Document 實例載入 PDF。
  2. 使用 Document.Info 屬性取得 DocumentInfo。
  3. 存取並顯示不同的 Document.Info 屬性。

將圖章添加到 PDF - Java


// Open document
Document pdfDocument = new Document(DATA_DIR.resolve("AddTextStamp.pdf").toString());
// Create text stamp
TextStamp textStamp = new TextStamp("Sample Stamp");
// Set whether stamp is background
textStamp.setBackground(true);
// Set origin
textStamp.setXIndent(100);
textStamp.setYIndent(100);
// Rotate stamp
textStamp.setRotate(Rotation.on90);
// Set text properties
textStamp.getTextState().setFont(FontRepository.findFont("Arial"));
textStamp.getTextState().setFontSize(14.0F);
textStamp.getTextState().setFontStyle(FontStyles.Bold | FontStyles.Italic);
textStamp.getTextState().setForegroundColor(Color.getAqua());
// Add stamp to particular page
pdfDocument.getPages().get_Item(1).addStamp(textStamp);
// Save output document
pdfDocument.save(DATA_DIR.resolve("AddTextStamp_out.pdf").toString());
pdfDocument.close();