Articles in this section
Category / Section

How to extract images from ASP.NET Core PDF?

7 mins read

The Syncfusion Essential® PDF is a .NET PDF library used to create, read, and edit the PDF documents. Using this library, you can extract the images from a PDF document in ASP.NET Core.

Steps to extract images from PDF document in ASP.NET Core:

  1. Create a new C# ASP.NET Core web application project.

Create a new ASP.NET Core web application

  1. Select the web application pattern (Model-View-Controller) for the project.

Select a web application pattern

  1. Install the Syncfusion.Pdf.Imaging.Net.Core NuGet package as a reference to your .NET Standard application from nuget.org.

Nuget_Image

  1. A default controller with name HomeController.cs gets added on creation of ASP.NET MVC project. Include the following namespaces in the HomeController.cs file.

C#

using Syncfusion.Pdf;
using Syncfusion.Pdf.Parsing;
using Syncfusion.Pdf.Exporting;

 VB.NET

Imports Syncfusion.Pdf
Imports Syncfusion.Pdf.Parsing
Imports Syncfusion.Pdf.Exporting

 

  1. A default action method named Index will be present in HomeController.cs. Right click the Index method and select ‘Go To View’ where you will be directed to its associated view page index.cshtml. 
  2. Add a new button in Index.cshtml as follows.

CSHTML

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

 

  1. Add the new method ExtractImages in HomeController.cs and include the following code snippet to extract images from the PDF document in ASP.NET Core.

C#

//Load the PDF document 
string path = _hostingEnvironment.WebRootPath + "/Data/Essential_Pdf.pdf";
Stream stream = new FileStream(path, FileMode.Open);
PdfLoadedDocument loadedDocument = new PdfLoadedDocument(stream); 
 
for (int i = 0; i < loadedDocument.Pages.Count; i++)
{    
    //Get the page 
    PdfLoadedPage page = loadedDocument.Pages[i] as PdfLoadedPage;
 
    //Extract images 
    Stream[] images = page.ExtractImages();
 
    for(int j=0;j<images.Length;j++)
    {     
        //Save the image to local file
        FileStream outStream = System.IO.File.OpenWrite("Test"+i.ToString()+".png");
        images[0].CopyTo(outStream);
        outStream.Flush();   
    }        
}
 
//Dispose the stream 
stream.Dispose();

 

VB.NET

'Load the PDF document 
 Dim path As String = _hostingEnvironment.WebRootPath & "/Data/Essential_Pdf.pdf"
 Dim stream As Stream = New FileStream(path, FileMode.Open)
 Dim loadedDocument As PdfLoadedDocument = New PdfLoadedDocument(stream)
 
 For i As Integer = 0 To loadedDocument.Pages.Count - 1
    
     'Get the page 
     Dim page As PdfLoadedPage = TryCast(loadedDocument.Pages(i), PdfLoadedPage)
     'Extract images 
     Dim images As Stream() = page.ExtractImages()
 
     For j As Integer = 0 To images.Length - 1
         
         'Save the image to local file      
         Dim outStream As FileStream = System.IO.File.OpenWrite("Test" & i.ToString() & ".png")
         images(0).CopyTo(outStream)
         outStream.Flush()
         
     Next
 Next
 'Dispose the stream 
 stream.Dispose()

 

A complete working sample to extract the images from the PDF document in ASP.NET Core platform can be downloaded from ExtractImages.zip.

By executing the program, you will get the image as follows.  

Output document screenshot

 

Take a moment to peruse the documentation, where you will find options such as extracting image from PDF page, getting image information, and converting PDF to image.

Click here to explore the rich set of Syncfusion Essential® PDF features.

An online sample link to extract images from PDF document in ASP.NET Core.

 

Note:

Starting with v16.2.0.x, if you reference Syncfusion® assemblies from the trial setup or the NuGet feed, include a license key in your projects. Refer to the link to learn about generating and registering the Syncfusion® license key in your application to use the components without a trial message.


See Also:

Extract images from PDF file in Windows Forms

Extract a part of image from a PDF in Windows Forms

Convert PDF to image in Windows Forms

Convert PDF to image in WPF


Conclusion:

I hope you enjoyed learning about how to extract images from ASP.NET Core PDF.

 

You can refer to our Flutter PDF feature tour page to learn about its other groundbreaking feature representations and documentation, and how to quickly get started with configuration specifications. You can also explore our Flutter PDF Flutter PDF examples to understand how to create and manipulate data.

For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion®, you can try our 30-day free trial to check out our other controls.

If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forums or feedback portal. We are always happy to assist you!

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please  to leave a comment
Access denied
Access denied