How to extract images from PDF by pages in C#, VB.NET and VBScript using PDF Extractor SDK - ByteScout
Announcement
Our ByteScout SDK products are sunsetting as we focus on expanding new solutions.
Learn More Open modal
Close modal
Announcement Important Update
ByteScout SDK Sunsetting Notice
Our ByteScout SDK products are sunsetting as we focus on our new & improved solutions. Thank you for being part of our journey, and we look forward to supporting you in this next chapter!

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

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

These samples show how to extract images from PDF by pages in C#, VB.NET, and VBScript using Bytescout PDF Extractor SDK.

Also, see the following tutorial if you need to extract image coordinates from PDF.

Select your programming language:

How to extract images by pages from PDF files in C#

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

namespace ExtractImagesByPages
{
	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");

			// Get count of pages
			int pageCount = extractor.GetPageCount();

			// Extract images from each page
			for (int i = 0; i < pageCount; i++)
			{
				int j = 0;
				
				// Initialize page images enumeration
				if (extractor.GetFirstPageImage(i))
				{
					do
					{
						string outputFileName = "page" + i + "image" + j + ".png";

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

						j++;

					} while (extractor.GetNextImage()); // Advance image enumeration
				}
			}
			
			// Open first output file in default associated application
			System.Diagnostics.Process.Start("page0image0.png");
		}
	}
}

How to extract images by pages from PDF files in Visual Basic .NET

Imports Bytescout.PDFExtractor
Imports System.Drawing.Imaging

Class Program
	Friend Shared Sub Main(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")

        ' Get count of pages
		Dim pageCount As Integer = extractor.GetPageCount()

        ' Extract images from each page
        For i As Integer = 0 To pageCount - 1

            Dim j As Integer = 0

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

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

                    j = j + 1

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

        Next

		' Open first output file in default associated application
        System.Diagnostics.Process.Start("page0image0.png")
	End Sub
End Class

How to extract images by pages from PDF files 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")

' Get page count
pageCount = extractor.GetPageCount()
		
' Extract images from each page
For i = 0 To pageCount - 1

	j = 0
	
	' Initialize page images enumeration
	If extractor.GetFirstPageImage(i) Then
	    Do
	        outputFileName = "page" & i & "image" & j & ".png"

	        ' Save image to file
	        extractor.SaveCurrentImageToFile outputFileName

	        j = j + 1

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

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

Set extractor = Nothing

Tutorials:

prev
next