通过 Java 以 PDF 格式制作邮票

使用 Java 加盖印章 PDF 文档。使用 Aspose.PDF 以编程方式修改 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。
  2. 使用 Document.Info 属性获取文档信息。
  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();