Extract embedded files from PDF

Content extraction
4/15/2011

Downloads

This code sample illustrates how to loop through embedded files in an existing PDF document and extract those attachments one by one and place them in a folder.

C# code sample

1 using (FileStream fileIn = new FileStream(@"..\..\inputAttachments.pdf", FileMode.Open, 2 FileAccess.Read)) 3 { 4 Document docIn = new Document(fileIn); 5 6 foreach (EmbeddedFile embeddedFile in docIn.EmbeddedFiles) 7 { 8 using (FileStream fileOut = 9 new FileStream(String.Format(@"..\..\Output\" + embeddedFile.FileName), 10 FileMode.Create, FileAccess.Write)) 11 { 12 embeddedFile.Write(fileOut); 13 } 14 } 15 }

VB.NET code sample

1 Using fileIn As New FileStream("..\..\inputAttachments.pdf", FileMode.Open, FileAccess.Read) 2 Dim docIn As New Document(fileIn) 3 4 For Each embeddedFile As EmbeddedFile In docIn.EmbeddedFiles 5 Using fileOut As New FileStream([String].Format("..\..\Output\" + embeddedFile.FileName), 6 FileMode.Create, FileAccess.Write) 7 embeddedFile.Write(fileOut) 8 End Using 9 Next 10 End Using