With HiQPdf library you can extract the images from PDF documents using the HiQPdfClientPdfImagesExtract class. You can extract the images to memory HiQPdfClientExtractedPdfImage objects using the PdfImagesExtractExtractToImageObjects(String, Int32, Int32) method or to image files using the PdfImagesExtractExtractToImageFiles(String, Int32, Int32, String, String) method. There is also a special method PdfImagesExtractRaiseImageExtractedEvent(String, Int32, Int32) which can be used to extract the images one by one in memory to avoid high memory usage for large PDF documents. The HiQPdfClientExtractedPdfImage objects can be disposed when they are no longer necessary.
In this demo you can learn how to extract the images from a PDF document. You can choose the range of PDF pages from where to extract the images. Images will be extracted in memory in .NET Image objects.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Hosting;
using HiQPdfClient;
namespace HiQPdf_Demo.Controllers
{
public class ExtractImagesFromPdfController : Controller
{
IWebHostEnvironment m_hostingEnvironment;
public ExtractImagesFromPdfController(IWebHostEnvironment hostingEnvironment)
{
m_hostingEnvironment = hostingEnvironment;
}
// GET: ExtractImagesFromPdf
public ActionResult Index()
{
SetCrtPageUri();
return View();
}
[HttpPost]
public ActionResult ExtractImages(IFormCollection collection)
{
string serverIP = collection["textBoxServerIP"];
uint serverPort = uint.Parse(collection["textBoxServerPort"]);
string serverPassword = collection["textBoxServerPassword"];
// get the PDF file
string pdfFile = m_hostingEnvironment.WebRootPath + "/DemoFiles/Pdf/InputPdf.pdf";
// create the PDF images extractor
PdfImagesExtract pdfImagesExtract = new PdfImagesExtract(serverIP, serverPort);
// use server password if necessary
if (serverPassword.Length > 0)
pdfImagesExtract.ServerPassword = serverPassword;
// set a demo serial number
pdfImagesExtract.SerialNumber = "YCgJMTAE-BiwJAhIB-EhlWTlBA-UEBRQFBA-U1FOUVJO-WVlZWQ==";
int fromPdfPageNumber = int.Parse(collection["textBoxFromPage"]);
int toPdfPageNumber = collection["textBoxToPage"][0].Length > 0 ? int.Parse(collection["textBoxToPage"]) : 0;
// extract the images from PDF document to memory in .NET Image objects
// the images can also be extracted to a folder using the ExtractToImageFiles method
// or they can be extracted one by one using the RaiseImageExtractedEvent method
ExtractedPdfImage[] extractedImages = pdfImagesExtract.ExtractToImageObjects(pdfFile, fromPdfPageNumber, toPdfPageNumber);
// return if no image was extracted
if (extractedImages.Length == 0)
return Redirect("/ExtractImagesFromPdf");
// get the largest image bytes in a buffer
byte[] imageBuffer = null;
try
{
// select the largest image
ExtractedPdfImage largestImage = null;
for (int i = 0; i < extractedImages.Length; i++)
{
if (largestImage == null || extractedImages[i].ImageData.Length > largestImage.ImageData.Length)
{
largestImage = extractedImages[i];
}
}
// get the image data in a buffer
imageBuffer = GetImageBuffer(largestImage);
}
finally
{
// dispose the extracted images
for (int i = 0; i < extractedImages.Length; i++)
extractedImages[i].Dispose();
}
FileResult fileResult = new FileContentResult(imageBuffer, "image/png");
fileResult.FileDownloadName = "ExtractedImage.png";
return fileResult;
}
private byte[] GetImageBuffer(ExtractedPdfImage extractedImage)
{
byte[] imageBuffer = new byte[extractedImage.ImageData.Length];
Array.Copy(extractedImage.ImageData, imageBuffer, imageBuffer.Length);
return imageBuffer;
}
private void SetCrtPageUri()
{
HttpRequest httpRequest = this.ControllerContext.HttpContext.Request;
UriBuilder uriBuilder = new UriBuilder();
uriBuilder.Scheme = httpRequest.Scheme;
uriBuilder.Host = httpRequest.Host.Host;
if (httpRequest.Host.Port != null)
uriBuilder.Port = (int)httpRequest.Host.Port;
uriBuilder.Path = httpRequest.PathBase.ToString() + httpRequest.Path.ToString();
uriBuilder.Query = httpRequest.QueryString.ToString();
ViewData["CrtPageUri"] = uriBuilder.Uri.AbsoluteUri;
}
}
}