Adding Attachment to PDF document

Contents
[ ]

Attachments can contain a wide variety of information and can be of a variety of file types. This article explains how to add an attachment to a PDF file.

  1. Create a new C++ project.
  2. Add a reference to Aspose.PDF DLL.
  3. Create a Document object.
  4. Create a FileSpecification object with the file you are adding, and file description.
  5. Add the FileSpecification object to the Document object’s EmbeddedFiles collection, with the collection’s Add method.

The EmbeddedFiles collection contains all the attachments in the PDF file. The following code snippet shows you how to add an attachment in a PDF document.

using namespace System;
using namespace Aspose::Pdf;
using namespace Aspose::Pdf::Text;

void WorkingWithAttachments::AddingAttachment()
{
	String _dataDir("C:\\Samples\\");

	// Open document
	auto pdfDocument = MakeObject<Document>(_dataDir + u"AddAttachment.pdf");

	// Setup new file to be added as attachment
	auto fileSpecification = MakeObject<FileSpecification>(_dataDir + u"test.txt", u"Sample text file");

	// Add attachment to document's attachment collection
	pdfDocument->get_EmbeddedFiles()->Add(fileSpecification);

	// Save new output
	pdfDocument->Save(_dataDir + u"AddAttachment_out.pdf");
}