通过 Java 添加文本戳记 PDF

使用 Java API 使用 Aspose.PDF 创建文本戳记。

如何使用 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。 1.使用文档对象打开 PDF 文档。 1.创建文本戳记并定义其属性。 1.使用 addStamp 方法将文本戳记添加到页面

将文本戳记添加到 PDF-Java。


    // open document
    Document pdfDocument = new Document("input.pdf");
    // 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);
    textStamp.getTextState().setFontStyle(FontStyles.Italic);
    textStamp.getTextState().setForegroundColor(Color.getGreen());
    // add stamp to particular page
    pdfDocument.getPages().get_Item(1).addStamp(textStamp);
    // save output document
    pdfDocument.save("TextStamp_output.pdf");