In order to extract Attachments in PDF file, we’ll use Aspose.PDF for .NET API which is a feature-rich, powerful and easy to use document manipulation API for net platform. Open NuGet package manager, search for Aspose.PDF and install. You may also use the following command from the Package Manager Console.
PM > Install-Package Aspose.PDF
Extract Attachments from PDF C#
You need Aspose.PDF for .NET to try the code in your environment.
- Get embedded files collection.
- Get count of the embedded files.
- Loop through the collection to get all the attachments.
- Check if parameter object contains the parameters.
- Get the Attachment and write to file or stream.
Extract Attachment from PDF document
static void PrintFileDetails(Aspose.Pdf.FileSpecification fileSpecification)
{
Console.WriteLine($"Name: {fileSpecification.Name}");
Console.WriteLine($"Description: {fileSpecification.Description}");
Console.WriteLine($"Mime Type: {fileSpecification.MIMEType}");
if (fileSpecification.Params != null)
{
Console.WriteLine($"CheckSum: {fileSpecification.Params.CheckSum}");
Console.WriteLine($"Creation Date: {fileSpecification.Params.CreationDate}");
Console.WriteLine($"Modification Date: {fileSpecification.Params.ModDate}");
Console.WriteLine($"Size: {fileSpecification.Params.Size}");
}
}
var inputFile = Path.Combine(dataDir, "GetAlltheAttachments.pdf");
var pdfDocument = new Aspose.Pdf.Document(inputFile);
var embeddedFiles = pdfDocument.EmbeddedFiles;
Console.WriteLine($"Total files: {embeddedFiles.Count}");
foreach (var fileSpecification in embeddedFiles)
{
PrintFileDetails(fileSpecification);
var outputFilePath = Path.Combine(dataDir, fileSpecification.Name);
using var fileStream = File.OpenWrite(outputFilePath);
fileSpecification.Contents.CopyTo(fileStream);
}