Remove Attachments from PDF via C++

Delete Attachments from PDF document. Use Aspose.PDF for C++ to modify PDF files programmatically

How to remove Attachments Using C++ Library

Remove Attachments from PDF using the Aspose.PDF for C++. Files with attachments, such as images or other PDF, can increase the size of a document. Removing attachments may help reduce the file size, making it easier to share and store. Also, attachments can contain confidential information that you don’t want to share with others. So, delete attachments from PDF. Removing attachments helps protect your data. Malicious attachments may pose security risks. Removing unknown or untrusted attachments from PDF will help prevent potential security threats. Removing attachments makes the PDF easier to print and view your document. We can conclude, that removing attachments from PDF can make the document more user-friendly, efficient, and compatible. In order to remove Attachments in PDF file, we’ll use Aspose.PDF for C++ API which is a feature-rich, powerful and easy to use document manipulation API for cpp platform. Open NuGet package manager, search for Aspose.PDF and install. You may also use the following command from the Package Manager Console.

Package Manager Console

PM > Install-Package Aspose.PDF.Cpp

Remove Attachments from PDF


You need Aspose.PDF for C++ to try the code in your environment.

  1. Open existing PDF.
  2. Delete all attachments.
  3. Save the result.

This code snippet programmatically removes all attachments from a PDF document and saves the modified PDF as a separate file. It initializes a ‘pdfDocument’ object, which is associated with the PDF file located at the ‘dataDir + DeleteAllAttachments.pdf’ path. After opening the PDF document, the code instructs the software to delete all attachments contained within this PDF file. Attachments in a PDF can include embedded files, such as images or other documents. The code modifies the ‘dataDir’ variable to store the path where the updated PDF file, without any attachments, will be saved. The updated PDF will be saved with the filename ‘DeleteAllAttachments_out.pdf.’

Delete Attachment from PDF document

This sample code shows how to Remove attachment from PDF - C++


	void WorkingWithAttachments::RemovingAttachment() {

	String _dataDir("C:\\Samples\\");

	// Open document
	auto pdfDocument = new Document(_dataDir + u"DeleteAllAttachments.pdf");

	// Delete all attachments
	pdfDocument->get_EmbeddedFiles()->Delete();

	// Save updated file
	pdfDocument->Save(_dataDir + u"DeleteAllAttachments_out.pdf");
	}
    ```