PDF Images Extractor Component

Winnovative client library allows you to extract the images from PDF documents. The PDF Images Extractor object of PdfImagesExtractor type can be initialized with the TCP/IP address of the server or with the HTTP URL address of the server, function of the Winnovative Server type you have installed.

  • TCP/IP Server. If you installed the Winnovative Server in an Azure Cloud Service Worker Role, in an in an Azure Service Fabric Application or in a Windows Service on a remote Windows machine you can use the PdfImagesExtractorPdfImagesExtractor(String, UInt32) constructor which takes as parameters the server IP address and the TCP port.

  • HTTP Server. If you installed the Winnovative Server in an Azure Cloud Service Web Role or in an IIS ASP.NET Web Application you can use the PdfImagesExtractorPdfImagesExtractor(Boolean, String) constructor which takes as parameters a flag to be set on true to indicate the usage of a HTTP service and the HTTP Server URL string as the second parameter.

PDF Images Extractor Options

The PDF Images Extractor allows you select the page range from where to extract the images. The features of the PDF Images Extractor are exemplified in the code sample below. The full Visual Studio demo project for ASP.NET Web Forms or MVC is available in product package you can download from website.

Code Sample - Extract Images from PDF in ASP.NET with PdfImagesExtractor Class

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using WinnovativeClient;

namespace PdfImagesExtractorClientDemo
{
    public partial class Default : System.Web.UI.Page
    {
        protected void extractImagesButton_Click(object sender, EventArgs e)
        {
            // Get the server options
            string serverIP = textBoxServerIP.Text;
            uint serverPort = uint.Parse(textBoxServerPort.Text);
            string servicePassword = textBoxServicePassword.Text;
            bool useServicePassword = servicePassword.Length > 0;
            bool useTcpService = radioButtonUseTcpService.Checked;
            string webServiceUrl = textBoxWebServiceUrl.Text;

            // the pdf file to convert
            string pdfFilePath = filePathTextBox.Text.Trim();
            if (pdfFilePath.Equals(String.Empty))
                throw new Exception("Please choose a PDF file");

            // start page number
            int startPageNumber = int.Parse(startPageTextBox.Text.Trim());
            // end page number
            // when it is 0 the extraction will continue up to the end of document
            int endPageNumber = 0;
            if (endPageTextBox.Text.Trim() != String.Empty)
                endPageNumber = int.Parse(endPageTextBox.Text.Trim());

            // Create the the PDF images extractor object
            PdfImagesExtractor pdfImagesExtractor = null;
            if (useTcpService)
                pdfImagesExtractor = new PdfImagesExtractor(serverIP, serverPort);
            else
                pdfImagesExtractor = new PdfImagesExtractor(true, webServiceUrl);

            // Set optional service password
            if (useServicePassword)
                pdfImagesExtractor.ServicePassword = servicePassword;

            pdfImagesExtractor.LicenseKey = "mRcGFgMAFgYWABgGFgUHGAcEGA8PDw8WBg==";

            ExtractedImage[] pdfPageImages = null;
            try
            {
                // read the PDF file in a memory buffer
                byte[] sourcePdfBytes = System.IO.File.ReadAllBytes(pdfFilePath);

                // extract the images to an array of ExtractedImage objects
                pdfPageImages = pdfImagesExtractor.ExtractImages(sourcePdfBytes, startPageNumber, endPageNumber);
            }
            catch (Exception ex)
            {
                // The extraction failed
                throw new Exception(String.Format("An error occurred. {0}", ex.Message));
            }

            // return the image extracted from PDF with the largest data
            ExtractedImage largeImage = null;
            for (int i = 0; i < pdfPageImages.Length; i++)
            {
                if (largeImage == null || pdfPageImages[i].ImageData.Length > largeImage.ImageData.Length)
                    largeImage = pdfPageImages[i];
            }

            byte[] imageBytes = largeImage.ImageData;

            Response.AddHeader("Content-Type", "image/png");
            Response.AddHeader("Content-Disposition", String.Format("attachment; filename=ExtractedImage.png; size={0}", imageBytes.Length.ToString()));
            Response.BinaryWrite(imageBytes);

            // End the HTTP response and stop the current page processing
            Response.End();
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                filePathTextBox.Text = Server.MapPath("~/DemoFiles/Input/Demo.pdf");

                Master.CollapseAll();
                Master.ExpandNode("PDF_Images_Extract");
                Master.SelectNode("Getting_Started");
            }
        }
    }
}