How to extract attachments from PDF in ASP.NET, C#, VB.NET, VBScript using PDF Extractor SDK - ByteScout

How to extract attachments from PDF in ASP.NET, C#, VB.NET, VBScript using PDF Extractor SDK

  • Home
  • /
  • Articles
  • /
  • How to extract attachments from PDF in ASP.NET, C#, VB.NET, VBScript using PDF Extractor SDK

Use the sample source code below to extract attachments (embedded files) from PDF with Bytescout PDF Extractor SDK.

Select your programming language:

ASP.NET

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.IO;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Bytescout.PDFExtractor;

namespace ExtractInfo
{
	public partial class _Default : System.Web.UI.Page
	{
		protected void Page_Load(object sender, EventArgs e)
		{
			// This test file will be copied to the project directory on the pre-build event (see the project properties).
			String inputFile = Server.MapPath("attachments.pdf");

			// Create Bytescout.PDFExtractor.AttachmentExtractor instance
			AttachmentExtractor extractor = new AttachmentExtractor();
			extractor.RegistrationName = "demo";
			extractor.RegistrationKey = "demo";
			
			// Load sample PDF document
			extractor.LoadDocumentFromFile(inputFile);

			Response.Clear();
			Response.ContentType = "text/html";

			for (int i = 0; i < extractor.Count; i++)
			{
				Response.WriteLine("Saving attachment:       " + extractor.GetFileName(i) + "
");
				// extractor.Save (i, extractor.GetFileName(i)); // you can save into temp folder or save to Stream object to avoid temp files
				Response.WriteLine("File size: " + extractor.GetSize(i).ToString() + "
");
			}

			Response.End();
		}
	}
}

C#

using System;
using Bytescout.PDFExtractor;

namespace ExtractInfo
{
	class Program
	{
		static void Main(string[] args)
		{
			// Create Bytescout.PDFExtractor.AttachmentExtractor instance
			AttachmentExtractor extractor = new AttachmentExtractor();
			extractor.RegistrationName = "demo";
			extractor.RegistrationKey = "demo";

			// Load sample PDF document
			extractor.LoadDocumentFromFile("attachments.pdf");

			for (int i = 0; i < extractor.Count; i++)
			{
				Console.WriteLine("Saving attachment:t" + extractor.GetFileName(i));
				
				// save file into the current folder
				extractor.Save(i, extractor.GetFileName(i));

				Console.WriteLine("Done.");
			}

			
			Console.WriteLine();
			Console.WriteLine("Press any key to continue...");
			Console.ReadLine();
		}
	}
}

VB.NET

Imports Bytescout.PDFExtractor

Class Program
	Friend Shared Sub Main(args As String())

        ' Create Bytescout.PDFExtractor.AttachmentExtractor instance
        Dim extractor As New AttachmentExtractor()
		extractor.RegistrationName = "demo"
		extractor.RegistrationKey = "demo"

		' Load sample PDF document
        extractor.LoadDocumentFromFile("attachments.pdf")

	For i = 0 to extractor.Count-1 
	        Console.WriteLine("Save attachment:       " + extractor.GetFileName(i))
		Extractor.Save i, extractor.GetFileName (i) ' save into the current folder
	        Console.WriteLine("Done")
	Next


        Console.WriteLine("Press any key to continue...")
        Console.ReadLine()

	End Sub
End Class

VBScript

' Create Bytescout.PDFExtractor.AttachmentExtractor object
Set AttachmentExtractor = CreateObject("Bytescout.PDFExtractor.AttachmentExtractor")
AttachmentExtractor.RegistrationName = "demo"
AttachmentExtractor.RegistrationKey = "demo"


' Load sample PDF document with embedded attachments
AttachmentExtractor.LoadDocumentFromFile("....attachments.pdf")


' walk through attachments and save them
For I = 0 To AttachmentExtractor.Count-1

 AttachmentExtractor.Save i, AttachmentExtractor.GetFileName (I) ' save in the current folder with original filename

Next

MsgBox "Done: " & CStr(AttachmentExtractor.Count) & " attachments extracted"

Set extractor = Nothing

Tutorials:

prev
next