通过 Java 向 PDF 添加文本图章

使用 Aspose.PDF 为 Java 库以编程方式创建文本图章

如何使用 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. 載入包含文件實例的 PDF。
  2. 使用文件對象打開 PDF 文件。
  3. 建立文字戳並定義其屬性。
  4. 使用添加時間戳方法將文字戳添加到頁面

使用 Python 向 PDF 添加文本图章

String inputFileName = "AddTextStamp.pdf";
String outputFileName = "AddTextStamp_out.pdf";

// Open document
Document pdfDocument = new Document(inputFileName);
// 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.getGreen());
// Add stamp to particular page
pdfDocument.getPages().get_Item(1).addStamp(textStamp);
// Save output document
pdfDocument.save(outputFileName);
pdfDocument.close();