How to perform OCR for a PDF document in Azure App Service?
The Syncfusion Essential® PDF is a .NET Core PDF library that supports OCR by using the Tesseract open-source engine. Using this library, you can Perform OCR for a PDF document in Azure using .NET Core.
Steps to Perform OCR for a PDF document in Azure programmatically:
- Create a new ASP.NET Core MVC application.
- Install the Syncfusion.PDF.OCR.Net.Core NuGet package as a reference to your .NET Core application from NuGet.org
- Copy the tessdata and Tesseractbinaries folder from the installed OCR NuGet package and paste it into the folder that contains the project file.
- Then, set Copy to output directory to copy all the tessdata and Tesseract binaries (All files including inner folders and files) assemblies.
- Add a Perform OCR button in index.cshtml.
@{Html.BeginForm("PerformOCR", "Home", FormMethod.Get); { <br /> <div> <input type="submit" value="Perform OCR" style="width:150px;height:27px" /> </div> } Html.EndForm(); }
- Include the following namespaces and code samples for performing OCR for a PDF document in Azure.
C#
using Syncfusion.OCRProcessor; using Syncfusion.Pdf.Parsing; using System.Net; using System.Net.Http.Headers; using Microsoft.Azure.WebJobs.Host;
//To get content root path of the project private readonly IHostingEnvironment _hostingEnvironment; public HomeController(IHostingEnvironment hostingEnvironment) { _hostingEnvironment = hostingEnvironment; } public IActionResult PerformOCR() { //Initialize the OCR processor with tesseract binaries folder path string binaries = Path.Combine(_hostingEnvironment.ContentRootPath, "Tesseractbinaries", "Windows"); OCRProcessor processor = new OCRProcessor(binaries); //Set custom temp file path location processor.Settings.TempFolder = Path.Combine(_hostingEnvironment.ContentRootPath, "Data"); //Load a PDF document FileStream stream1 = new FileStream(Path.Combine(_hostingEnvironment.ContentRootPath, "Data", "Input.pdf"), FileMode.Open); PdfLoadedDocument lDoc = new PdfLoadedDocument(stream1); //Set OCR language to process processor.Settings.Language = Languages.English; //Perform OCR with input document and tessdata (Language packs) string tessdataPath = Path.Combine(_hostingEnvironment.ContentRootPath, "tessdata"); string ocr = processor.PerformOCR(lDoc, tessdataPath); //Save the document. MemoryStream stream = new MemoryStream(); lDoc.Save(stream); return File(stream.ToArray(), System.Net.Mime.MediaTypeNames.Application.Pdf, "OCR_Azure.pdf"); }
- Now, check the OCR creation in the local machine.
- Right-click the project and select Publish.
- Create a new profile in the publish window.
- Create App Service using the Azure subscription and select a hosting plan.
- Configure the Hosting plan.
- After creating a profile, click the Publish button.
- Now, the published website will open in the browser, then you can perform OCR for a PDF document.
A complete working sample can be downloaded from PerformOCR.zip.
Take a moment to peruse the documentation. You will find other options like performing OCR on the image, region of the document, and large PDF documents with code examples.
Refer here to explore a rich set of Syncfusion Essential® PDF features.
Starting with v16.2.0.x, if you reference Syncfusion® assemblies from 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 trail message.
I hope you enjoyed learning about how to perform OCR for a PDF document in Azure App Service.
You can refer to our ASP.NET Core PDF's feature tour page to know about its other groundbreaking feature representations and documentation, and how to quickly get started for configuration specifications. You can also explore our ASP.NET PDF example to understand how to create and manipulate data.
For current customers, you can check out our Document Processing Libraries from the License and Downloads page. If you are new to Syncfusion®, you can try our 30-day free trial to check out our 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, Direct-Trac, or feedback portal. We are always happy to assist you!