通过 Java 向 PDF 添加文本图章

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

如何使用 Java 库将文本图章添加到 PDF

为了在 PDF 中添加文本图章,我们将使用 Aspose.PDF for 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 才能在你的环境中试用这些代码。

  1. 加载带有文档实例的 PDF。
  2. 使用文档对象打开 PDF 文档。
  3. 创建文本戳记并定义其属性。
  4. 使用 addStamp 方法将文本戳记添加到页面

使用 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();