PDF processing control Aspose.PDF function demonstration: add or delete attachments in PDF using Java

PDF is one of the major file formats in the world of digital documents. The PDF format allows files to be embedded as attachments into PDF files. These attachments are similar to attachments you add to emails. To automate PDF attachment operations, this article provides some simple methods to add and remove attachments from PDF files using Java.

  • Get PDF attachment information using Java
  • Add attachment to PDF in Java
  • Remove attachments from PDF in Java

下载Aspose.PDF for Java(qun:761297826)icon-default.png?t=N7T8https://www.evget.com/product/4202/download

Get PDF attachment information using Java

First, let's check how to retrieve attachment information from PDF files. This information contains the attachment's name, description, MIME type and other parameters such as checksum, modification date, etc. Following are the steps to get information about attachments in PDF files.

  • Use the Document class to load PDF files.
  • Use the FileSpecification class via Document.getEmbeddedFiles(). The get_Item(int) method retrieves PDF attachments.
  • Use the FileSpecification object to retrieve the attachment's information.

The following code example demonstrates how to obtain information about a PDF attachment using Java.

// Open document
Document pdfDocument = new Document("input.pdf");

// Get particular embedded file
FileSpecification fileSpecification = pdfDocument.getEmbeddedFiles().get_Item(1);

// Get the file properties
System.out.println("Name:-" + fileSpecification.getName());
System.out.println("Description:- " + fileSpecification.getDescription());
System.out.println("Mime Type:-" + fileSpecification.getMIMEType());

// Check if parameter object contains the parameters
if (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());
}

Add attachment to PDF in Java

Here are the steps to add attachments to PDF documents.

  • Use the Document class to load PDF files.
  • Create an object of FileSpecification class to load attachment files.
  • Use Document.getEmbeddedFiles(). The add(FileSpecificatio) method adds attachments.
  • Save PDF files using Document.save(String) method.

The following code example shows how to add attachments to a PDF using Java.

// Open a document
Document pdfDocument = new Document("input.pdf");

// Set up a new file to be added as attachment
FileSpecification fileSpecification = new FileSpecification("sample.txt", "Sample text file");

// Add an attachment to document's attachment collection
pdfDocument.getEmbeddedFiles().add(fileSpecification);

// Save the updated document
pdfDocument.save("output.pdf");

Remove attachments from PDF in Java

You can delete specific attachments by name or delete them all at once. Here are the steps to remove attachments from PDF documents.

  • Use the Document class to load PDF files.
  • Use Document.getEmbeddedFiles(). delete() and Document.getEmbeddedFiles(). delete(String) Delete all or specific attachments respectively.
  • Save PDF files using Document.save(String) method.

The following code example shows how to delete PDF attachments using Java.

// Open a document
Document pdfDocument = new Document("input.pdf");

// Delete all attachments
pdfDocument.getEmbeddedFiles().delete();

// Save the updated file
pdfDocument.save("output.pdf");

Guess you like

Origin blog.csdn.net/m0_67129275/article/details/132733723