Extract Image from PDF Document – C#/VB.NET

Sometimes, we would like to add images in PDF document. Firstly, beautiful image can make the appearance be more wonderful. Secondly, some specified images can present a visualized effect about content, for example, chart can show data information which is provided in document. Also, we can add some websites logos in document. For example, I will add twitter logo if I add my twitter address at the end of the document.

Actually, images in one PDF documents can be used repeatedly in other documents, especially pictures in header and footer. If we don't have the original image in computer, we should extract it from PDF document. In this post, I will introduce a simple method to extract image from PDF with C#/VB.NET.

Firstly, I prepare a PDF document which is a form about employee information. In this document, we can find one logo at the top left corner. What I will do is extract this logo and save to my document folder.

PDF Document

Then, I can create my project to realize this function. Because I use Spire.PDF in this method, so I need to add its dll file as reference.

Now, let's go to steps.

  1. Create a PDF document and load the file which we want to extract image.
  2. Suppose there are many images included in this document, so declare a container IList<image> to save images.
  3. One document may have several pages, so use for loop to find images in every page and then extract them to the container.
  4. Get images from container and name them. Because there may be not one image, so name image as Extract{0}.png and use for loop to make it to be named automatically.
  5. Save image and then launch the first image we extract.

Main Coding:

C#

using System;

using System.Collections.Generic;

using System.Drawing;

using System.Drawing.Imaging;

using Spire.Pdf;

namespaceExtractImage

{

    class Extract

    {

        static void Main(string[] args)

        {

            //Load File

            PdfDocument doc = new PdfDocument();

            doc.LoadFromFile(@"D:\work\My Documents\Employee Information.pdf");

 

            IList<Image> images = new List<Image>();

 

            foreach (PdfPageBase page in doc.Pages)

            {

                foreach (Image image in page.ExtractImages())

                {

                    images.Add(image);

                }

            }

 

            doc.Close();

 

            //Save Image

            int index = 0;

            foreach (Image image in images)

            {

                String imageFileName

                    = String.Format("Extract{0}.png", index++);

                image.Save(imageFileName, ImageFormat.Png);

            }

 

            System.Diagnostics.Process.Start("Extract0.png");

        }

    }

}

VB.NET

Imports System

Imports System.Collections.Generic

Imports System.Drawing

Imports System.Drawing.Imaging

Imports Spire.Pdf

Namespace ExtractImage

       Friend Class Extract

              Shared Sub Main(ByVal args() As String)

                     'Load File

                     Dim doc As New PdfDocument()

                     doc.LoadFromFile("D:\work\My Documents\Employee Information.pdf")

 

                     Dim images As IList(Of Image) = New List(Of Image)()

 

                     For Each page As PdfPageBase In doc.Pages

                           For Each image As Image In page.ExtractImages()

                                  images.Add(image)

                           Next image

                     Next page

 

                     doc.Close()

 

                     'Save Image

                     Dim index As Integer = 0

                     For Each image As Image In images

                           Dim imageFileName As String = String.Format("Extract{0}.png", index)

                           index += 1

                           image.Save(imageFileName, ImageFormat.Png)

                     Next image

 

                     System.Diagnostics.Process.Start("Extract0.png")

              End Sub

       End Class

End Namespace

Result Shown by Following:

__________________________________________________________________________

Click Here to Learn more about Spire.PDF

Click Here to Download Spire.PDF

Advertisements

Author: janewdaisy

.NET Program Beginner. Share methods about how to use C#/VB.NET to export data, operate Word, Excel, PDF and other useful skills.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s