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

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

Extract images from PDF – source code samples below will help you to extract images from PDF files in ASP.NET, C#, VB.NET and VBScript using PDF Extractor SDK.

Also, check this tutorial: How to extract images from PDF by pages.

Select your programming language:

How to extract images from PDF in ASP.NET

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Drawing.Imaging;
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 ExtractAllImages
{
	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("sample1.pdf");

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

			Response.Clear();

			int i = 0;

			// Initialize image enumeration
			if (extractor.GetFirstImage())
			{
				do
				{
					if (i == 0) // Write the fist image to the Response stream
					{
						string imageFileName = "image" + i + ".png";

						Response.Write("<b>" + imageFileName + "</b>");

						Response.ContentType = "image/png";
						Response.AddHeader("Content-Disposition", "inline;filename=" + imageFileName);

						// Write the image bytes into the Response output stream 
						Response.BinaryWrite(extractor.GetCurrentImageAsArrayOfBytes());
					}

					i++;

				} while (extractor.GetNextImage()); // Advance image enumeration
			}

			Response.End();
		}
	}
}

How to extract images from PDF in C#

using System;
using System.Drawing.Imaging;
using Bytescout.PDFExtractor;

namespace ExtractAllImages
{
	class Program
	{
		static void Main(string[] args)
		{
			// Create Bytescout.PDFExtractor.ImageExtractor instance
			ImageExtractor extractor = new ImageExtractor();
			extractor.RegistrationName = "demo";
			extractor.RegistrationKey = "demo";
			
			// Load sample PDF document
			extractor.LoadDocumentFromFile("sample1.pdf");

			int i = 0;

			// Initialize image enumeration
			if (extractor.GetFirstImage())
			{
				do
				{
					string outputFileName = "image" + i + ".png";

					// Save image to file
					extractor.SaveCurrentImageToFile(outputFileName, ImageFormat.Png);

					i++;

				} while (extractor.GetNextImage()); // Advance image enumeration
			}

			// Open first output file in default associated application
			System.Diagnostics.Process.Start("image0.png");
		}
	}
}

How to extract images from PDF in Visual Basic .NET

Imports Bytescout.PDFExtractor
Imports System.Drawing.Imaging

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

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

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

        Dim i As Integer = 0

        ' Initialize image enumeration
        If extractor.GetFirstImage() Then
            Do
                Dim outputFileName As String = "image" & i & ".png"

                ' Save image to file
                extractor.SaveCurrentImageToFile(outputFileName, ImageFormat.Png)

                i = i + 1

            Loop While extractor.GetNextImage() ' Advance image enumeration
        End If

        ' Open first output file in default associated application
        System.Diagnostics.Process.Start("image0.png")

    End Sub
End Class

How to extract images from PDF in VBScript (Visual Basic 6)

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

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

i = 0

' Initialize image enumeration
If extractor.GetFirstImage() Then
    Do
        outputFileName = "image" & i & ".png"

        ' Save image to file
        extractor.SaveCurrentImageToFile outputFileName

        i = i + 1

    Loop While extractor.GetNextImage() ' Advance image enumeration
End If

' Open first output image in default associated application
Set shell = CreateObject("WScript.Shell")
shell.Run "image0.png", 1, false
Set shell = Nothing

Set extractor = Nothing


prev
next