Products Technologies Demo Docs Blog Support Company

Searching Strings in PDF Documents

TX Text Control is not only able to load and modify MS Word documents such as DOC, RTF and DOCX files. TX Text Control is also able to import "born digital" PDF documents, so that you can view, edit or convert these files. An overview of the features and the possibilities can be read here: PDF Reflow - Load, view, edit and convert Adobe PDF files The combination of ServerTextControl, LINQ and regular expressions provides a powerful tool to search strings in PDF documents. The method…

Searching Strings in PDF Documents
Searching strings in PDF documents

TX Text Control is not only able to load and modify MS Word documents such as DOC, RTF and DOCX files. TX Text Control is also able to import "born digital" PDF documents, so that you can view, edit or convert these files.

An overview of the features and the possibilities can be read here:

PDF Reflow - Load, view, edit and convert Adobe PDF files

The combination of ServerTextControl, LINQ and regular expressions provides a powerful tool to search strings in PDF documents. The method FindInPDF listed below accepts a PDF document as a file path and a value to seek. ServerTextControl opens the PDF in order to provide the plain text to a regular expression.

The resulting MatchCollection of the Matches method is used by LINQ to return the index of each individual result that are stored in an IEnumerable<int> object.

private IEnumerable<int> FindInPDF(string path, string value)
{
    string sSourceString = "";

    // create a temporary ServerTextControl that imports the PDF file
    using (TXTextControl.ServerTextControl tx =
        new TXTextControl.ServerTextControl())
    {
        TXTextControl.LoadSettings ls= new TXTextControl.LoadSettings();
        ls.PDFImportSettings = TXTextControl.PDFImportSettings.GenerateLines;

        tx.Create();
        tx.Load(path, TXTextControl.StreamType.AdobePDF, ls);

        // prepare the string to match the numbers of
        // control chartacters
        sSourceString = tx.Text.Replace("
","
"); 
    }

    // use RegEx and LINQ to match the strings and to return the ID
    return
        Regex.Matches(sSourceString, value).Cast<Match>().Select(m => m.Index);
}

The following code returns all index positions of the string "text" in a PDF document:

IEnumerable<int> index = FindInPDF("test.pdf", "text");

Stay in the loop!

Subscribe to the newsletter to receive the latest updates.

Related Posts

PDFTutorial

Create Password Protected and Signed Adobe PDF and PDF/A Documents

The document format Adobe PDF is probably the most popular format when it comes to invoices, quotes and other business documents. It has several advantages: It is a "read only" document that…


ASP.NETAIASP.NET Core

Automating PDF/UA Accessibility with AI: Describing DOCX Documents Using TX…

This article shows how to use TX Text Control together with the OpenAI API to automatically add descriptive texts (alt text and labels) to images, links, and tables in a DOCX. The resulting…


ASP.NETASP.NET CoreJava

Converting Office Open XML (DOCX) to PDF in Java

Learn how to convert Office Open XML (DOCX) documents to PDF in Java using the powerful ServerTextControl library. This guide provides step-by-step instructions and code examples to help you…


ASP.NETComparisonDocument Processing SDK

Document SDK Comparison: Complete Document Processing vs. PDF SDK

This blog outlines why complete document processing SDKs offer greater value for your investment compared to PDF SDKs. It also specifies the business factors and technical advantages that matter…


ASP.NETASP.NET CoreDS Server

Extending DS Server with Custom Digital Signature APIs

In this article, we will explore how to extend the functionality of DS Server by integrating custom digital signature APIs. We will cover the necessary steps to create a plugin that allows DS…