PDF处理控件Aspose.PDF功能演示:使用Java在PDF中添加或删除附件

PDF格式允许将文件作为附件嵌入PDF文件中。为了自动执行PDF附件操作,本文提供了一些使用Java在PDF文件中添加和删??除附件的简单方法。

PDF是数字文档世界中的主要文件格式之一。PDF格式允许将文件作为附件嵌入PDF文件中。这些附件类似于您添加到电子邮件中的附件。为了自动执行PDF附件操作,本文提供了一些使用Java在PDF文件中添加和删除附件的简单方法。

  • 使用Java获取PDF附件信息
  • 在Java中将附件添加到PDF
  • 在Java中从PDF删除附件

目前,Java版Aspose.PDF升级到v20.11版,感兴趣的朋友可点击下方按钮下载最新版。(安装包仅提供部分功能,并设置限制,如需试用完整功能请申请免费授权

17一齐周年庆!整合所有格式的Aspose.Total永久授权正在火热促销中,乐享85折起!联系客服立马1分钟了解全部咨询!

使用Java获取PDF附件信息

首先,让我们检查一下如何检索PDF文件中的附件信息。该信息包含附件的名称,描述,MIME类型和其他参数,例如校验和,修改日期等。以下是在PDF文件中获取有关附件信息的步骤。

  • 使用Document类加载PDF文件。
  • 使用FileSpecification类通过Document.getEmbeddedFiles()。get_Item(int)方法检索PDF附件。
  • 使用FileSpecification对象检索附件的信息。

下面的代码示例演示如何使用Java获取PDF附件的信息。

// Open documentDocument pdfDocument = new Document("input.pdf");// Get particular embedded fileFileSpecification fileSpecification = pdfDocument.getEmbeddedFiles().get_Item(1);// Get the file propertiesSystem.out.println("Name:-" + fileSpecification.getName());System.out.println("Description:- " + fileSpecification.getDescription());System.out.println("Mime Type:-" + fileSpecification.getMIMEType());// Check if parameter object contains the parametersif (fileSpecification.getParams() != null) {System.out.println("CheckSum:- " + fileSpecification.getParams().getCheckSum());System.out.println("Creation Date:- " + fileSpecification.getParams().getCreationDate());System.out.println("Modification Date:- " + fileSpecification.getParams().getModDate());System.out.println("Size:- " + fileSpecification.getParams().getSize());}

在Java中将附件添加到PDF

以下是将附件添加到PDF文档的步骤。

  • 使用Document类加载PDF文件。
  • 创建FileSpecification类的对象以加载附件文件。
  • 使用Document.getEmbeddedFiles()。add(FileSpecificatio)方法添加附件。
  • 使用Document.save(String)方法保存PDF文件。

以下代码示例显示了如何使用Java将附件添加到PDF。

// Open a documentDocument pdfDocument = new Document("input.pdf");// Set up a new file to be added as attachmentFileSpecification fileSpecification = new FileSpecification("sample.txt", "Sample text file");// Add an attachment to document's attachment collectionpdfDocument.getEmbeddedFiles().add(fileSpecification);// Save the updated documentpdfDocument.save("output.pdf");

在Java中从PDF删除附件

可以使用名称删除特定附件,也可以一次删除所有附件。以下是从PDF文档中删除附件的步骤。

  • 使用Document类加载PDF文件。
  • 使用Document.getEmbeddedFiles()。delete()和Document.getEmbeddedFiles()。delete(String)分别删除所有或特定附件。
  • 使用Document.save(String)方法保存PDF文件。

以下代码示例显示了如何使用Java删除PDF附件。

// Open a documentDocument pdfDocument = new Document("input.pdf");// Delete all attachmentspdfDocument.getEmbeddedFiles().delete();// Save the updated filepdfDocument.save("output.pdf");


还想要更多吗可以点击阅读【2020 · Aspose最新资源整合】查找需要的教程资源。如果您有任何疑问或需求,请随时加入Aspose技术交流群(),我们很高兴为您提供查询和咨询
标签:

来源:慧都

声明:本站部分文章及图片转载于互联网,内容版权归原作者所有,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!

上一篇 2020年10月21日
下一篇 2020年10月22日

相关推荐

发表回复

登录后才能评论