How to extract images from ASP.NET Core PDF?
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:
- Create a new C# ASP.NET Core web application project.
- Select the web application pattern (Model-View-Controller) for the project.
- Install the Syncfusion.Pdf.Imaging.Net.Core NuGet package as a reference to your .NET Standard application from nuget.org.
- 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
- 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.
- 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(); }
- 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.
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.
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
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!