Extract and Save an Attachment
Get All Attachments
With Aspose.PDF, it is possible to get all attachments from a PDF document. This is useful either when you want to save the documents separately from the PDF, or if you need to strip a PDF of attachments.
To get all attachments from a PDF file:
Loop through the Document object’s EmbeddedFiles collection. The EmbeddedFiles collection contains all attachments. Each element of this collection represents a FileSpecification object. Each iteration of the foreach loop through the EmbeddedFiles collection returns a FileSpecification object.
Once the object is available, retrieve either all the attached file’s properties or the file itself.
The following code snippets show how to get all the attachments from a PDF document.
The following code snippet also work with Aspose.PDF.Drawing library.
.NET Core 3.1
// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void GetAllAttachments ()
{
// The path to the documents directory
var dataDir = RunExamples . GetDataDir_AsposePdf_Attachments ();
// Open PDF document
using ( var document = new Aspose . Pdf . Document ( dataDir + "GetAlltheAttachments.pdf" ))
{
// Get embedded files collection
Aspose . Pdf . EmbeddedFileCollection embeddedFiles = document . EmbeddedFiles ;
// Get count of the embedded files
Console . WriteLine ( "Total files : {0}" , embeddedFiles . Count );
int count = 1 ;
// Loop through the collection to get all the attachments
foreach ( Aspose . Pdf . FileSpecification fileSpecification in embeddedFiles )
{
Console . WriteLine ( "Name: {0}" , fileSpecification . Name );
Console . WriteLine ( "Description: {0}" ,
fileSpecification . Description );
Console . WriteLine ( "Mime Type: {0}" , fileSpecification . MIMEType );
// Check if parameter object contains the parameters
if ( fileSpecification . Params != null )
{
Console . WriteLine ( "CheckSum: {0}" ,
fileSpecification . Params . CheckSum );
Console . WriteLine ( "Creation Date: {0}" ,
fileSpecification . Params . CreationDate );
Console . WriteLine ( "Modification Date: {0}" ,
fileSpecification . Params . ModDate );
Console . WriteLine ( "Size: {0}" , fileSpecification . Params . Size );
}
// Get the attachment and write to file or stream
var fileContent = new byte [ fileSpecification . Contents . Length ];
fileSpecification . Contents . Read ( fileContent , 0 , fileContent . Length );
using ( var fileStream = new FileStream ( dataDir + count + "_out" + ".txt" , FileMode . Create ))
{
fileStream . Write ( fileContent , 0 , fileContent . Length );
}
count += 1 ;
}
}
}
.NET 8
// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void GetAllAttachments ()
{
// The path to the documents directory
var dataDir = RunExamples . GetDataDir_AsposePdf_Attachments ();
// Open PDF document
using var document = new Aspose . Pdf . Document ( dataDir + "GetAlltheAttachments.pdf" );
// Get embedded files collection
Aspose . Pdf . EmbeddedFileCollection embeddedFiles = document . EmbeddedFiles ;
// Get count of the embedded files
Console . WriteLine ( "Total files : {0}" , embeddedFiles . Count );
int count = 1 ;
// Loop through the collection to get all the attachments
foreach ( Aspose . Pdf . FileSpecification fileSpecification in embeddedFiles )
{
Console . WriteLine ( "Name: {0}" , fileSpecification . Name );
Console . WriteLine ( "Description: {0}" ,
fileSpecification . Description );
Console . WriteLine ( "Mime Type: {0}" , fileSpecification . MIMEType );
// Check if parameter object contains the parameters
if ( fileSpecification . Params != null )
{
Console . WriteLine ( "CheckSum: {0}" ,
fileSpecification . Params . CheckSum );
Console . WriteLine ( "Creation Date: {0}" ,
fileSpecification . Params . CreationDate );
Console . WriteLine ( "Modification Date: {0}" ,
fileSpecification . Params . ModDate );
Console . WriteLine ( "Size: {0}" , fileSpecification . Params . Size );
}
// Get the attachment and write to file or stream
var fileContent = new byte [ fileSpecification . Contents . Length ];
fileSpecification . Contents . Read ( fileContent , 0 , fileContent . Length );
using var fileStream = new FileStream ( dataDir + count + "_out" + ".txt" , FileMode . Create );
fileStream . Write ( fileContent , 0 , fileContent . Length );
count += 1 ;
}
}
Get Individual Attachment
In order to get an individual attachment, we can specify the index of attachment in EmbeddedFiles
object of Document instance. Please try using following code snippet.
.NET Core 3.1
// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void GetIndividualAttachment ()
{
// The path to the documents directory
var dataDir = RunExamples . GetDataDir_AsposePdf_Attachments ();
// Open PDF document
using ( var document = new Aspose . Pdf . Document ( dataDir + "GetIndividualAttachment.pdf" ))
{
// Get particular embedded file
Aspose . Pdf . FileSpecification fileSpecification = document . EmbeddedFiles [ 1 ];
// Get the file properties
Console . WriteLine ( "Name: {0}" , fileSpecification . Name );
Console . WriteLine ( "Description: {0}" , fileSpecification . Description );
Console . WriteLine ( "Mime Type: {0}" , fileSpecification . MIMEType );
// Check if parameter object contains the parameters
if ( fileSpecification . Params != null )
{
Console . WriteLine ( "CheckSum: {0}" ,
fileSpecification . Params . CheckSum );
Console . WriteLine ( "Creation Date: {0}" ,
fileSpecification . Params . CreationDate );
Console . WriteLine ( "Modification Date: {0}" ,
fileSpecification . Params . ModDate );
Console . WriteLine ( "Size: {0}" , fileSpecification . Params . Size );
}
// Get the attachment and write to file or stream
var fileContent = new byte [ fileSpecification . Contents . Length ];
fileSpecification . Contents . Read ( fileContent , 0 , fileContent . Length );
using ( var fileStream = new FileStream ( dataDir + "test_out" + ".txt" , FileMode . Create ))
{
fileStream . Write ( fileContent , 0 , fileContent . Length );
}
}
}
.NET 8
// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void GetIndividualAttachment ()
{
// The path to the documents directory
var dataDir = RunExamples . GetDataDir_AsposePdf_Attachments ();
// Open PDF document
using var document = new Aspose . Pdf . Document ( dataDir + "GetIndividualAttachment.pdf" );
// Get particular embedded file
Aspose . Pdf . FileSpecification fileSpecification = document . EmbeddedFiles [ 1 ];
// Get the file properties
Console . WriteLine ( "Name: {0}" , fileSpecification . Name );
Console . WriteLine ( "Description: {0}" , fileSpecification . Description );
Console . WriteLine ( "Mime Type: {0}" , fileSpecification . MIMEType );
// Check if parameter object contains the parameters
if ( fileSpecification . Params != null )
{
Console . WriteLine ( "CheckSum: {0}" ,
fileSpecification . Params . CheckSum );
Console . WriteLine ( "Creation Date: {0}" ,
fileSpecification . Params . CreationDate );
Console . WriteLine ( "Modification Date: {0}" ,
fileSpecification . Params . ModDate );
Console . WriteLine ( "Size: {0}" , fileSpecification . Params . Size );
}
// Get the attachment and write to file or stream
var fileContent = new byte [ fileSpecification . Contents . Length ];
fileSpecification . Contents . Read ( fileContent , 0 , fileContent . Length );
using var fileStream = new FileStream ( dataDir + "test_out" + ".txt" , FileMode . Create );
fileStream . Write ( fileContent , 0 , fileContent . Length );
}
Get Attachments that contains in FileAttachmentAnnotation objects
In addition to the Document object’s EmbeddedFiles collection, Attachments can also be contained in FileAttachmentAnnotation objects. Below is the code for viewing the number and details of such Attachments.