X
    Categories: Document ImagingOCRPDF

Solution to 4 Common First-world Problems – Convert and Merge to PDF

This past weekend, I was very fortunate compared to so many people that recently faced and are still having to deal with Hurricane Florence. At home, the power is out, but there is no major damage. I am fortunate. Since there is nothing I can do at the moment about the power and everyone around me is safe, I decided to forget my first-world problems, go to the office, which does have power, and write a small and simple app that solves some common first-world problems. I am hoping that you will find this tool useful, too!

Problems to Solve

Scanned Files and Photos are not Usually Text-Searchable

Most scanners do not automatically OCR while scanning, even though it sends a PDF. It merely captures the image and wraps it in the PDF format making it possible to open in Acrobat, but not possible search for a word or phrase.

Also, there are times I do not have a scanner around and just have my phone. Those are pictures and not in the PDF format nor text-searchable.

Some Scanned Documents are too Large to Email

Some clients have inbound limitations on the size of emails they can receive. This means that if you need to scan a 90-page document and try to send it to them, it will probably bounce due to the size restriction.

Multi-page Documents are Scanned as Separate Files

It never fails. I go to the network scanner to scan a document just after someone else and do not pay attention to the settings. It just so happens that they needed to scan a large batch of single-page documents and changed the settings to scan each page as a separate file. I do not notice until I get back to my desk and see 12 different files for my 12-page document. When that happens, I quietly say a few choice words and go back to the scanner to try again.

Combine Documents from Different Sources into One File

I like to digitally keep track of bills and receipts. For example, I receive a property tax bill in the mail and make a payment online. The confirmation is either a PDF or a screenshot (image). Now, I must keep two files from different sources and possibly in different formats together. It would be better if I could merge the files and only have to keep track of one file. Another example is keeping related purchase requests, approvals, and receipts in one file, even if they were originally received in different formats and at different times.

Requirements

For me, the only requirement is that the tool be easy to use and save time. Ideally, I want to be able to right-click a file or files and do as little as possible to solve all of the problems I listed above. (I feel like I have heard that before.)

The Solution

I created a .NET Core 2 console application and added a shortcut to the shell:sendto folder.

To create the application, I used Visual Studio 2017 to create a .NET Core 2 self-contained application and added the Leadtools.Document.Sdk NuGet package. I then added a few lines of code and published it to a folder.

The results are exactly what I wanted. I can now right-click a single file or multiple files to solve all of the problems I listed above.

See it in Action

Click the thumbnails to see at full size.

Convert Scanned PDF to Searchable PDF and Reduce the Size

Convert a Photo Into a Searchable PDF

Merge Multiple Scanned PDFs into One Searchable PDF

Merge Multiple Formats Into One Searchable PDF

The Code

Below is an example of the code that uses the LEADTOOLS Document Converter to merge multiple files into one text-searchable PDF file.

private static void MergeAndConvertToPdf(IReadOnlyCollection<string> inputFiles)
{
    Console.WriteLine();
    ConsoleHelper.WriteStatus("Converting Files... ");
    using (var documentConverter = new DocumentConverter())
    {
        var firstFile = GetFirst(inputFiles);
        var outputFile = GetNewFilePath(firstFile);

        var loadOptions = new LoadDocumentOptions() {UseCache = false};
        var createOptions = new CreateDocumentOptions() {UseCache = false};
        documentConverter.SetDocumentWriterInstance(GetDocumentWriter());

        using (var ocrEngine = OcrEngineManager.CreateEngine(OcrEngineType.LEAD, false))
        {
            ocrEngine.Startup(null, null, null, null);
            documentConverter.SetOcrEngineInstance(ocrEngine, false);

            using (var masterDocument = DocumentFactory.Create(createOptions))
            {
                masterDocument.AutoDisposeDocuments = true;
                foreach (var inputFile in inputFiles)
                    masterDocument.Pages.AddRange(DocumentFactory.LoadFromFile(inputFile, loadOptions).Pages);

                var jobData =
                    DocumentConverterJobs.CreateJobData(masterDocument, outputFile, DocumentFormat.Pdf);
                jobData.JobName = "merge and convert job";
                var job = documentConverter.Jobs.CreateJob(jobData);
                documentConverter.Jobs.JobOperation += JobOperationEventHandler;
                documentConverter.Jobs.RunJob(job);
                documentConverter.Jobs.JobOperation -= JobOperationEventHandler;

                DisplayResultMessage(job, outputFile);
            }

            ocrEngine.Shutdown();
        }
    }
}

Click here to download the entire project. Once downloaded, unzip the project into a folder and adjust the path to the license file in the SetLicense() function. You can request a license file or download the entire evaluation from the Leadtools.Document.Sdk NuGet package page.

UPDATE: Check out my next post, “Correct Photos of Documents – Ambient Lighting,” where I update this project to improve the visual quality of photos of documents.

It is hard to believe that the solution to several common first-world problems can be solved so easily when you have the right tools on hand.

About 

Developer Advocate

    Find more about me on:
Gabriel Smith: Developer Advocate

View Comments

  • Does this ocr pdfs similar to other solutions? This is another solution I am looking at right now: https://www.bisok.com/grooper-data-capture-method-features/multi-pass-ocr/

    • LEADTOOLS is a product for developers to create solutions exactly like that.