Extract Image from PDF using Syncfusion

PDF is a popular format for creating read-only documents for sharing and printing. In general, PDF documents contain images in addition to text, and in certain circumstances, you may need to extract these photos while parsing the PDFs. This post will show you how to extract photos from PDF files programmatically using C#.

Step 1:Open visual studio and create a new project

Step 2: Install Syncfusion.Imaging.Pdf.Net.Core NuGet package

Step 3: Add one dummy pdf file  to your project folder which contain images

Step 4: Create one folder inside your project solution to store images which is an extract from the pdf

Step 5: Create a view with one button to read pdf file from the folder

Copy and paste the below code into your view

@{Html.BeginForm("ExtractImages", "Home", FormMethod.Get);
    {
        <div>
            <input type="submit" value="Extract Image" style="width:150px;height:27px" class="btn btn-info" />
        </div>
    }
    Html.EndForm();
}

Step 6: Copy and paste the below code into the controller to read the pdf path from the folder

using Microsoft.AspNetCore.Hosting;
  private readonly IHostingEnvironment _hostingEnvironment;
  public HomeController(IHostingEnvironment hostingEnvironment)
    { 
            _hostingEnvironment = hostingEnvironment;
    }

Step 7: Copy and paste the below code into your controller to extract the image from the pdf

public void ExtractImages()
        {
            //Load the PDF document 
            string path = _hostingEnvironment.WebRootPath + "/PDF/sample-pdf-with-images.pdf";
            Stream stream = new FileStream(path, FileMode.Open);
            PdfLoadedDocument loadedDocument = new PdfLoadedDocument(stream);

            Image[] images = null;

            for (int i = 0; i < loadedDocument.Pages.Count; i++)
            {
                images = null;

                //Get the page 
                PdfLoadedPage page = loadedDocument.Pages[i] as PdfLoadedPage;

                //Extract images 
                images = page.ExtractImages();

                for (int j = 0; j < images.Length; j++)
                {
                    //Save the image to memory stream
                    MemoryStream imageStream = new MemoryStream();
                    images[j].Save(imageStream, ImageFormat.Png);

                    //Save the image to local file
                    imageStream.Position = 0;
                    string Imagepath = _hostingEnvironment.WebRootPath + "/ExtractImage/";
                    FileStream file = new FileStream(Imagepath + "Image" + i.ToString() + ".png", FileMode.Create, FileAccess.Write);
                    imageStream.WriteTo(file);
                    file.Close();
                    imageStream.Close();
                }
            }
            //Dispose the stream 
            stream.Dispose();
        }

OUTPUT

Submit a Comment

Your email address will not be published. Required fields are marked *

Subscribe

Select Categories