Aspose.PDF FOSS for Java 是一个 MIT 许可证的 Java 库,用于创建和处理 PDF 文档。它提供了一个全面的 Document 类作为中心入口点,并完整覆盖注释、交互式表单字段、页面操作、元数据以及 PDF/A 合规性验证。
该库公开了 527 个类,覆盖完整的 PDF 规范:通过 Document、Page 和 PageCollection 实现文档结构;通过 Annotation 层次结构(包括 WidgetAnnotation、FreeTextAnnotation 和 HighlightAnnotation)实现注释;使用 Form、ButtonField、CheckboxField 和 RadioButtonField 实现交互式表单;通过 BmpDevice 进行页面渲染;以及通过 ActionRules 和 ActionFixes 检查 PDF/A 合规性。加密通过 AESCipher 的 CBC 模式提供。
在 Maven 项目中使用 groupId org.aspose 和 artifactId aspose-pdf 添加 Aspose.PDF FOSS。该库需要 Java 11 或更高版本,并完全采用 MIT 许可证。有关企业功能和支持,请参阅 Aspose.PDF for Java — Enterprise Product。
Document 类来创建新的 PDF 文档。可以通过 Document(filePath) 或 Document(stream) 构造函数访问现有文档。PageCollection 添加、访问和修改页面。每个 Page 都公开 MediaBox、CropBox、旋转、资源和内容操作符。DocumentInfo 和 XmpMetadata 读取和写入文档元数据。getParser()、getCatalog() 和 getTrailer() 访问底层 PDF 对象。getEmbeddedFiles() 管理嵌入文件集合。Document 并填写页面来编程文件.PdfFileEditor和 Page API来调整,旋转或重新排列页.Annotation 类是所有注册的基础;具体类型包括 WidgetAnnotation,FreeTextAnnotation, HighlightAnnotation 和 UnderlineAnnotation.Document.getForm()访问文档表单,以列出或创建ButtonField,CheckboxField和 RadioButtonField 的实例.AppearanceCharacteristics 边界颜色,背景,旋转和标题.FormEditor和 FormElement用于更高层次的表格操作.ActionRules和 ActionFixes来验证和修复长期存储的文档.AESCipher 通过 CBC 模式(AESCipher.encrypt(key, data) 和 AESCipher.decrypt(key, data))加密和解密数据字节数组。Artifact 和 ArtifactCollection API 识别并操作页眉、页脚、水印和背景图像。PdfFileEditor.resizeContents() 和 ContentsResizeParameters 调整页面内容布局。AnnotationRules 和 ActionRules 验证注释和操作规则是否符合 PDF/A 标准。AESCipher 进行文件流中嵌入数据部分的加码.Artifact 表示水和页面装饰的对象.ActionFixes和 AnnotationFixes,使现有文件符合PDF/A-1b的存储标准.创建一个新文档,添加页面并附上有风格的小工具注释.
try (Document doc = new Document()) ```java
try (Document doc = new Document()) {
Page page = doc.getPages().add();
WidgetAnnotation w = new WidgetAnnotation(page, new Rectangle(0, 0, 100, 50));
w.getCharacteristics().setBorder(Color.fromRgb(1, 0, 0));
w.getCharacteristics().setCaption("Submit");
page.getAnnotations().add(w);
doc.save("output.pdf");
}
将无线电按组添加到文档表格中,并通过程序选择一个值.
try (Document doc = new Document()) {
Page page = doc.getPages().add();
RadioButtonField radio = new RadioButtonField(page);
radio.setPartialName("choice");
radio.addOption("Option1", new Rectangle(50, 50, 70, 70));
radio.addOption("Option2", new Rectangle(50, 80, 70, 100));
doc.getForm().add(radio, 1);
radio.setValue("Option2");
doc.save("form.pdf");
}
访问现有文档并阅读第一页的媒体框尺寸.
try (Document doc = new Document("input.pdf")) {
double width = doc.getPages().get(1).getMediaBox().getWidth();
double height = doc.getPages().get(1).getMediaBox().getHeight();
System.out.println("Page size: " + width + " x " + height);
}