Home
We need your feedback!
Subscribe to Our Newsletter (tips, new releases, specials):
 Subscribe in a reader
 PRODUCTS  |  DOWNLOAD  |  PURCHASE  |  SUPPORT  |  TESTIMONIALS   |  NEWS  | 

How to extract images from PDF in .NET using PDFImageExtractor (comes with source code)

Download For Free

PDF Image Extractor SDK (PDFImageExtractor.dll) provides a fast and easy way to extract images from PDF documents.

IMPORTANT: PDFImageExtractor.dll extracts images from PDF (for example, if you have PDF made from scanned images) but not able to render PDF into image!

Possible Usage Scenario:
1) Sample #1: Convert PDF with scanned document images back into .PNG or another image format file;
2) Sample #2: Open PDF with scanned document and read barcodes from extracted images using Bytescout BarCode Reader SDK;

Sample #1: extract images from PDF into .PNG image format

Visual Basic .NET :
Imports System.Collections.Generic
Imports System.Drawing.Imaging
Imports System.IO
Imports System.Text
Imports Bytescout.PDFImageExtractor

Class Program
    Friend Shared Sub Main(args As String())
        Dim extractor As New ImageExtractor()
        Dim filenum As Integer = 1

        extractor.InputPdfFile = "example.pdf"

        If extractor.GetFirstImage() Then
            Do
                Dim filename As [String] = Path.GetTempPath() & "Image" & filenum & ".png"

                If Not extractor.SaveCurrentImageToFile(filename, ImageFormat.Png) Then
                    Console.WriteLine(extractor.ErrorMessage)
                Else
                    Console.WriteLine("Saved file """ & filename & """")
                End If

                filenum += 1
            Loop While extractor.GetNextImage()
        End If

        extractor.Dispose()

        Console.WriteLine()
        Console.WriteLine("Press any key to continue.")
        Console.ReadKey()
    End Sub
End Class

Visual C# .NET :
using System;
using System.Collections.Generic;
using System.Drawing.Imaging;
using System.IO;
using System.Text;
using Bytescout.PDFImageExtractor;

namespace PdfToPng
{
    class Program
    {
        static void Main(string[] args)
        {
            ImageExtractor extractor = new ImageExtractor();
            int filenum = 1;

            extractor.InputPdfFile = "example.pdf";
            
            if (extractor.GetFirstImage())
            {
                do
                {
                    //String filename = Path.GetTempPath() + "Image" + filenum + ".png";
                    String filename = "Image" + filenum + ".png";

                    if (!extractor.SaveCurrentImageToFile(filename, ImageFormat.Png))
                    {
                        Console.WriteLine(extractor.ErrorMessage);
                    }
                    else
                    {
                        Console.WriteLine("Saved file \"" + filename + "\"");
                    }
                    
                    filenum++;
                } 
                while (extractor.GetNextImage());
            }

            extractor.Dispose();

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

Download For Free


Sample #2: Reading barcodes from images from PDF document file

This sample requires Bytescout BarCode Reader SDK installed as well.

Visual Basic .NET :
Imports System.Drawing
Imports Bytescout.BarCodeReader
Imports Bytescout.PDFImageExtractor

Class Program
    Friend Shared Sub Main(args As String())
        Dim extractor As New ImageExtractor()
        Dim barcodeReader As New Reader()
        Dim imgNum As Integer = 1

        extractor.InputPdfFile = "example.pdf"

        If extractor.GetFirstImage() Then
            Do
                Console.WriteLine("Reading barcode(s) from image #{0}", imgNum)

                Dim barcodes As FoundBarcode() = barcodeReader.ReadFrom(DirectCast(extractor.CurrentImage, Bitmap))

                For Each barcode As FoundBarcode In barcodes
                    Console.WriteLine("   Found barcode with type '{0}' and value '{1}'", barcode.Type, barcode.Value)
                Next

                imgNum += 1
            Loop While extractor.GetNextImage()
        End If

        extractor.Dispose()

        Console.WriteLine()
        Console.WriteLine("Press any key to continue.")
        Console.ReadKey()
    End Sub
End Class

Visual C# .NET :
using System;
using System.Drawing;
using Bytescout.BarCodeReader;
using Bytescout.PDFImageExtractor;

namespace BarcodesFromPDF
{
    class Program
    {
        static void Main(string[] args)
        {
            ImageExtractor extractor = new ImageExtractor();
            Reader barcodeReader = new Reader();
            int imgNum = 1;

            extractor.InputPdfFile = "example.pdf";

            if (extractor.GetFirstImage())
            {
                do
                {
                    Console.WriteLine("Reading barcode(s) from image #{0}", imgNum);

                    FoundBarcode[] barcodes = barcodeReader.ReadFrom((Bitmap) extractor.CurrentImage);

                    foreach (FoundBarcode barcode in barcodes)
                        Console.WriteLine("   Found barcode with type '{0}' and value '{1}'", barcode.Type, barcode.Value);

                    imgNum++;
                }
                while (extractor.GetNextImage());
            }

            extractor.Dispose();

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

ASP.NET (Visual Basic) :
Imports System
Imports System.Data
Imports System.Configuration
Imports System.Collections
Imports System.Web
Imports System.Web.Security
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.WebControls.WebParts
Imports System.Web.UI.HtmlControls
Imports System.Drawing
Imports System.Collections.Generic

Imports Bytescout.BarCodeReader

Partial Public Class _Default
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    End Sub

    Protected Sub UploadButton_Click(ByVal sender As Object, ByVal e As EventArgs)
        Dim savePath As String = "\uploads\"

        If FileUpload1.HasFile Then
            Dim fileName As String = FileUpload1.FileName
            savePath += fileName
            FileUpload1.SaveAs(Server.MapPath(savePath))

            Dim bitmap As Bitmap = Nothing
            Try
                bitmap = New Bitmap(FileUpload1.FileContent)
            Catch generatedExceptionName As System.Exception
            End Try

            If bitmap Is Nothing Then
                UploadStatusLabel.Visible = True
                UploadStatusLabel.Text = "Your file is not an image"
                Image1.Visible = False
                ListBox1.Visible = False
            Else
                UploadStatusLabel.Visible = False
                Image1.ImageUrl = savePath
                Image1.Visible = True
                ListBox1.Items.Clear()
                ListBox1.Visible = True
                findBarcodes(Server.MapPath(savePath))
                If ListBox1.Items.Count = 0 Then
                    ListBox1.Items.Add("No barcodes found")
                End If
            End If
        Else
            ' Notify the user that a file was not uploaded.
            UploadStatusLabel.Text = "You did not specify a file to upload."
        End If
    End Sub

    Private Sub findBarcodes(ByVal fileName As String)
        Dim reader As New Reader(fileName)
        For Each barcode As FoundBarcode In reader.FoundBarcodes
            ListBox1.Items.Add([String].Format("{0} : {1}", barcode.Type, barcode.Value))
        Next
    End Sub

End Class

ASP.NET (C#) :
using System;
using System.Data;
using System.Configuration;
using System.Collections;
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 System.Drawing;
using System.Collections.Generic;

using Bytescout.BarCodeReader;

namespace WebTestSharp
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void UploadButton_Click(object sender, EventArgs e)
        {
            String savePath = @"\uploads\";

            if (FileUpload1.HasFile)
            {
                String fileName = FileUpload1.FileName;
                savePath += fileName;
                FileUpload1.SaveAs(Server.MapPath(savePath));
                
                Bitmap bitmap = null;
                try
                {
                    bitmap = new Bitmap(FileUpload1.FileContent);
                }
                catch (System.Exception)
                {
                }

                if (bitmap == null)
                {
                    UploadStatusLabel.Visible = true;
                    UploadStatusLabel.Text = "Your file is not an image";
                    Image1.Visible = false;
                    ListBox1.Visible = false;
                }
                else
                {
                    UploadStatusLabel.Visible = false;
                    Image1.ImageUrl = savePath;
                    Image1.Visible = true;
                    ListBox1.Items.Clear();
                    ListBox1.Visible = true;
                    findBarcodes(Server.MapPath(savePath));
                    if (ListBox1.Items.Count == 0)
                        ListBox1.Items.Add("No barcodes found");
                }
            }
            else
            {
                // Notify the user that a file was not uploaded.
                UploadStatusLabel.Text = "You did not specify a file to upload.";
            }
        }

        private void findBarcodes(string fileName)
        {
            Reader reader = new Reader(fileName);
            foreach (FoundBarcode barcode in reader.FoundBarcodes)
                ListBox1.Items.Add(String.Format("{0} : {1}", barcode.Type, barcode.Value));
        }
    }
}

Download For Free


HOME  |  CONTACT US

Copyright © ByteScout, 2003-2010. Privacy Statement
Microsoft®, Windows®, Windows 2000®, Windows Server®, Windows Vista®, Internet Explorer®, .NET Framework®, ActiveX®, Visual Basic®, Visual C#®, ASP®, ASP.NET®, Excel®, PowerPoint®, are either registered trademarks or trademarks of Microsoft Corporation in the United States and/or other countries. Adobe®, Flash® and Acrobat® are registered trademarks of Adobe Systems, Incorporated. Mozilla®, Firefox® and the Mozilla and Firefox Logos are registered trademarks of the Mozilla Foundation. Other product names or brandnames used herein are for identification purposes only and might be trademarks or registered trademarks of their respective companies. We disclaim any and all rights to those marks.