EVO PDF Logo

PDF to HTML Component

EVO PDF Client for .NET Core

EVO PDF client library allows you to easily convert in just a few lines of code PDF documents to HTML documents. The PDF to HTML Converter object of PdfToHtmlConverter type can be initialized with the TCP/IP address of the server or with the HTTP URL address of the server, function of the EVO PDF Server type you have installed.

PDF to HTML Converter Options

The PDF to HTML Converter allows you select the page range to convert and to specify the resolution and the zoom level of the resulted HTML content. These features of the PDF to HTML converter are exemplified in the code sample below. The full Visual Studio demo project for ASP.NET Core is available in product package you can download from website.

Code Sample - Convert PDF to HTML in ASP.NET with PdfToHtmlConverter Class

C#
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 System.Text;

using EvoPdfClient;

namespace PdfToHtmlDemo.Controllers
{
    public class Getting_StartedController : Controller
    {
        private readonly IWebHostEnvironment m_hostingEnvironment;
        public Getting_StartedController(IWebHostEnvironment hostingEnvironment)
        {
            m_hostingEnvironment = hostingEnvironment;
        }

        public IActionResult Index()
        {
            ViewData["DemoFilePath"] = m_hostingEnvironment.ContentRootPath + "/wwwroot" + "/DemoFiles/Input/Demo.pdf";

            return View();
        }

        [HttpPost]
        public ActionResult ConvertPdfToHtml(IFormCollection collection)
        {
            // Get the server options
            string serverIP = collection["textBoxServerIP"];
            uint serverPort = uint.Parse(collection["textBoxServerPort"]);
            string servicePassword = collection["textBoxServicePassword"];
            bool useServicePassword = servicePassword.Length > 0;
            bool useTcpService = collection["ServerType"] == "radioButtonUseTcpService";
            string webServiceUrl = collection["textBoxWebServiceUrl"];

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

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

            // Create the PDF to HTML converter object
            PdfToHtmlConverter pdfToHtmlConverter = null;
            if (useTcpService)
                pdfToHtmlConverter = new PdfToHtmlConverter(serverIP, serverPort);
            else
                pdfToHtmlConverter = new PdfToHtmlConverter(true, webServiceUrl);

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

            pdfToHtmlConverter.LicenseKey = "1FpLW01LW0tbTVVLW0hKVUpJVUJCQkJbSw==";

            // set the resolution of HTML images
            pdfToHtmlConverter.Resolution = int.Parse(collection["resolutionTextBox"]);

            // set the zoom of HTML content
            pdfToHtmlConverter.Zoom = int.Parse(collection["zoomTextBox"]);

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

                // convert PDF pages in memory to an array of PdfPageHtml objects
                pdfPageHtmls = pdfToHtmlConverter.ConvertPdfPagesToHtml(sourcePdfBytes, startPageNumber, endPageNumber);
            }
            catch (Exception ex)
            {
                // The conversion failed
                throw new Exception(String.Format("An error occurred. {0}", ex.Message));
            }

            byte[] htmlBytes = null;
            try
            {
                // get the HTML document for the first PDF page in range
                htmlBytes = Encoding.UTF8.GetBytes(pdfPageHtmls[0].Html);
            }
            finally
            {
                // dispose the generated HTML documents
                for (int i = 0; i < pdfPageHtmls.Length; i++)
                    pdfPageHtmls[i].Dispose();
            }

            FileResult fileResult = new FileContentResult(htmlBytes, "text/html; charset=UTF-8");
            fileResult.FileDownloadName = "PdfPage.html";

            return fileResult;
        }
    }
}