9 Types of Useful Data You Can Extract from a PDF Using C# | Syncfusion Blogs
Live Chat Icon For mobile
Live Chat Icon
Popular Categories.NET  (174).NET Core  (29).NET MAUI  (207)Angular  (109)ASP.NET  (51)ASP.NET Core  (82)ASP.NET MVC  (89)Azure  (41)Black Friday Deal  (1)Blazor  (219)BoldSign  (14)DocIO  (24)Essential JS 2  (107)Essential Studio  (200)File Formats  (66)Flutter  (133)JavaScript  (221)Microsoft  (119)PDF  (81)Python  (1)React  (101)Streamlit  (1)Succinctly series  (131)Syncfusion  (918)TypeScript  (33)Uno Platform  (3)UWP  (4)Vue  (45)Webinar  (51)Windows Forms  (61)WinUI  (68)WPF  (159)Xamarin  (161)XlsIO  (36)Other CategoriesBarcode  (5)BI  (29)Bold BI  (8)Bold Reports  (2)Build conference  (8)Business intelligence  (55)Button  (4)C#  (149)Chart  (131)Cloud  (15)Company  (443)Dashboard  (8)Data Science  (3)Data Validation  (8)DataGrid  (63)Development  (632)Doc  (8)DockingManager  (1)eBook  (99)Enterprise  (22)Entity Framework  (5)Essential Tools  (14)Excel  (40)Extensions  (22)File Manager  (7)Gantt  (18)Gauge  (12)Git  (5)Grid  (31)HTML  (13)Installer  (2)Knockout  (2)Language  (1)LINQPad  (1)Linux  (2)M-Commerce  (1)Metro Studio  (11)Mobile  (507)Mobile MVC  (9)OLAP server  (1)Open source  (1)Orubase  (12)Partners  (21)PDF viewer  (43)Performance  (12)PHP  (2)PivotGrid  (4)Predictive Analytics  (6)Report Server  (3)Reporting  (10)Reporting / Back Office  (11)Rich Text Editor  (12)Road Map  (12)Scheduler  (52)Security  (3)SfDataGrid  (9)Silverlight  (21)Sneak Peek  (31)Solution Services  (4)Spreadsheet  (11)SQL  (11)Stock Chart  (1)Surface  (4)Tablets  (5)Theme  (12)Tips and Tricks  (112)UI  (387)Uncategorized  (68)Unix  (2)User interface  (68)Visual State Manager  (2)Visual Studio  (31)Visual Studio Code  (19)Web  (596)What's new  (333)Windows 8  (19)Windows App  (2)Windows Phone  (15)Windows Phone 7  (9)WinRT  (26)
9 Types of Useful Data You Can Extract from a PDF Using C#

9 Types of Useful Data You Can Extract from a PDF Using C#

The Syncfusion PDF Library is a .NET library that allows users to extract various types of useful data from a PDF document, such as text, images, attachments, and form data, using C# and VB.NET.

PDF documents are mostly used for exchanging business data in the form of invoices, purchase orders, shipping notes, reports, presentations, price and product lists, HR forms, and more.

In certain scenarios, a user must read and validate the data present in a PDF document, which imposes additional costs and time requirements to do manually. To avoid this, we can use text extraction to pull all the data, or only some of it, from a PDF document, and then validate it further in an automated way.

In this blog, we are going to focus on the following nine types of data extraction from PDF documents:

Say goodbye to tedious PDF tasks and hello to effortless document processing with Syncfusion's PDF Library.

Extract all text from PDF document

Important data elements in PDF files (e.g., order number, date, total, and other fields in an invoice) have to be extracted to gain insight about the information in the files. So, huge volumes of PDF documents need manual processing to find the information, which requires a huge workforce. This can be avoided by automating with text extraction methods.

With the Syncfusion PDF library, you can extract all the text from a PDF document or from a specific page of the document.

The following code example explains the procedure to extract text from a specific page.

//Load the PDF document
PdfLoadedDocument loadedDocument = new PdfLoadedDocument("../../Data/Invoice.pdf");

// Get the first page of the loaded PDF document
PdfPageBase page = loadedDocument.Pages[0];

// Extract text from the first page with bounds
string extractedText = page.ExtractText();

loadedDocument.Close(true);

File.WriteAllText("data.txt", extractedText);

The following code example explains the procedure to extract all text from the entire PDF document.

FileStream inputStream = new FileStream("Invoice.pdf",FileMode.Open);

//Load the PDF document
PdfLoadedDocument loadedDocument = new PdfLoadedDocument(inputStream);

StringBuilder extractedText = new StringBuilder();

// Extract all the text from the PDF document pages
foreach (PdfLoadedPage loadedPage in loadedDocument.Pages)
{
    extractedText.Append(loadedPage.ExtractText());
}

//Close the document
loadedDocument.Close(true);

//Save the text to file
File.WriteAllText("data.txt", extractedText.ToString());

Extract text from predefined bounds

Sometimes we don’t need all the text extracted from a PDF document, such as when we only need to validate some specific information (e.g., the invoice number from an invoice). For this scenario, we need to pick specific text from declared bounds within a PDF document.

To do this, we need to specify bounds where the key data is present in the PDF document.

The following code example illustrates the procedure to extract text from the specified bounds.

//Load the PDF document
PdfLoadedDocument loadedDocument = new PdfLoadedDocument("Invoice.pdf");

// Get the first page of the loaded PDF document
PdfPageBase page = loadedDocument.Pages[0];

TextLines lineCollection = new TextLines();

// Extract text from the first page with bounds
page.ExtractText(out lineCollection);

RectangleF textBounds = new RectangleF(474, 161, 50, 9);

string invoiceNumer = "";

//Get the text provided in the bounds
foreach (TextLine txtLine in lineCollection)
{
    foreach (TextWord word in txtLine.WordCollection)
    {
        if (textBounds.IntersectsWith(word.Bounds))
        {
            invoiceNumer = word.Text;
            break;
        }
    }
}

loadedDocument.Close(true);

File.WriteAllText("data.txt", invoiceNumer);

By executing this code example, you will get a text document like the one in the following screenshot.

Extract text from predefined bounds

Explore the wide array of rich features in Syncfusion's PDF Library through step-by-step instructions and best practices.

Layout-based text extraction

When PDF documents don’t have a common layout structure, it is difficult to extract their text in a way that approximates the original layout of the document. Layout-based text extraction provides a clear view of the text data in a form that resembles the original document layout.

With the Syncfusion PDF library, you can extract text from the given PDF page based on its layout by using the ExtractText(bool) overload method.

The following code illustrates how to extract text based on the layout of the document.

//Load the PDF document
PdfLoadedDocument loadedDocument = new PdfLoadedDocument("Invoice.pdf");

//Get the first page of the loaded PDF document
PdfPageBase page = loadedDocument.Pages[0];

//Extract text with layout
string extractedText = page.ExtractText(true);

//Save text to file
File.WriteAllText("data.txt", extractedText);

//Close the PDF document
loadedDocument.Close(true);

By executing this code example, you will get a text document like the one in the following screenshot.

Layout-based text extraction

Extract text from a scanned PDF document

When PDF documents are created from scanned image files, we cannot use our usual text extraction methods. We must turn to OCR processing to extract the text from the PDF document.

The Syncfusion OCR Processor library has the functionalities to perform OCR processing on PDF documents. You can find more details in this documentation.

The Syncfusion OCR NuGet packages are available at the following links:

The following code example illustrates how to extract text from a PDF document generated from a scanned image.

//Initialize OCR processor
OCRProcessor processor = new OCRProcessor(@"../../TesseractBinaries/3.02/");

//Load a PDF document

PdfLoadedDocument lDoc = new PdfLoadedDocument("../../Data/Invoice.pdf");

//Set OCR language to process

processor.Settings.Language = Languages.English;

OCRLayoutResult hocrBounds;

processor.PerformOCR(lDoc, @"../../Tessdata/", out hocrBounds);

StreamWriter writer = new StreamWriter("data.txt");

foreach(Page pages in hocrBounds.Pages)
{
    foreach(Line line in pages.Lines)
    {
        writer.WriteLine(line.Text);
    }
}

writer.Close();

lDoc.Close(true);

processor.Dispose();

By executing this code example, you will get a text document like the one in the following screenshot.
Extract text from a scanned PDF document

Embark on a virtual tour of Syncfusion's PDF Library through interactive demos.

Extract images from PDF

The extract images feature allows you to extract and reuse images in any other applications or PDF documents.

The following code example illustrates the procedure to extract images from the PDF document.

// Load an existing PDF
PdfLoadedDocument loadedDocument = new PdfLoadedDocument("../../../../../../Data/Template.pdf");

foreach(PdfPageBase page in loadedDocument.Pages)
{
    //Extract images from first page
    Image[] extractedImages=page.ExtractImages();

    foreach(Image image in extractedImages)
    {
        //Save each image to file
        image.Save(Guid.NewGuid().ToString() + ".jpg",ImageFormat.Jpeg);
    }
}

//Close the document
loadedDocument.Close(true);

By executing this code example, you will get the image like in the following screenshot.

Extract images from PDF

Note: Use the following NuGet link for the package needed to extract images from PDF documents on the .NET Core platform:https://www.nuget.org/packages/Syncfusion.Pdf.Imaging.Net.Core/.

Find invoice total amount in PDF with regex

Sometimes in a PDF document, we might need only a single piece of data, such as the total amount in an invoice. In this case, we can use regex (regular expressions) to get the data with the highest value.

The following code example illustrates the procedure to extract the required text data from the PDF document using regex.

//Load the PDF document
PdfLoadedDocument loadedDocument = new PdfLoadedDocument("../../../../../../Data/Invoice.pdf");

//Search for total amount
string regexPattern = @"(TOTAL)([-+]?[0-9]*\.?[0-9]+)"; 
// See the complete regular expressions reference at https://msdn.microsoft.com/en-us/library/az24scfc(v=vs.110).aspx

// Extract all the text from existing PDF document pages
foreach (PdfLoadedPage loadedPage in loadedDocument.Pages)
{
    TextLines lines;
    loadedPage.ExtractText(out lines);
    foreach(TextLine line in lines)
    {
        Regex re = new Regex(regexPattern, RegexOptions.IgnoreCase);

        Match match = re.Match(line.Text);
        if (match.Success)
        {
            //Print the total amount
            Console.WriteLine("Found Total Amount Number:" + match.Value.Replace("Total", ""));
        }
    }
}

//Close the document
loadedDocument.Close(true);

By executing this code example, you will get the text output like in the following screenshot.

Find invoice total amount in PDF with regex

See a world of document processing possibilities in Syncfusion's PDF Library as we unveil its features in interactive demonstrations.

Extract PDF document information

Let’s see how to extract a document’s information, such as author, creator, producer, and more.

The following code example illustrates the procedure to extract a PDF document’s information.

//Load the PDF document
PdfLoadedDocument loadedDocument = new PdfLoadedDocument("../../../../../../Data/Invoice.pdf");

//Get all the document information
Console.WriteLine("Author:       " + loadedDocument.DocumentInformation.Author);
Console.WriteLine("Creator:      " + loadedDocument.DocumentInformation.Creator);
Console.WriteLine("Producer:     " + loadedDocument.DocumentInformation.Producer);
Console.WriteLine("Subject:      " + loadedDocument.DocumentInformation.Subject);
Console.WriteLine("Title:        " + loadedDocument.DocumentInformation.Title);
Console.WriteLine("CreationDate: " + loadedDocument.DocumentInformation.CreationDate);
Console.WriteLine("Keywords:     " + loadedDocument.DocumentInformation.Keywords);
Console.WriteLine("Encrypted:    " + loadedDocument.IsEncrypted);

//Close the document
loadedDocument.Close(true);

Extract attachments from PDF

PDF attachments may be different kinds of documents, such as Word documents, Excel worksheets, PowerPoint presentations, images, and more, and they might contain important data. You can extract these attachments from an existing PDF document using the following code example.

//Load the PDF document
PdfLoadedDocument loadedDocument = new PdfLoadedDocument("../../../../../../Data/Attachment.pdf");
//Get first page from document
PdfLoadedPage page = loadedDocument.Pages[0] as PdfLoadedPage;
//Get the annotation collection from pages
PdfLoadedAnnotationCollection annotations = page.Annotations;
//Iterates the annotations
foreach (PdfLoadedAnnotation annot in annotations)
{
    //Check for the attachment annotation
    if (annot is PdfLoadedAttachmentAnnotation)
    {
        PdfLoadedAttachmentAnnotation file = annot as PdfLoadedAttachmentAnnotation;
        //Extracts the attachment and saves it to the disk
        FileStream stream = new FileStream(file.FileName, FileMode.Create);
        stream.Write(file.Data, 0, file.Data.Length);
        stream.Dispose();
    }
}
//Iterates through the attachments
if (loadedDocument.Attachments.Count != 0)
{
    foreach (PdfAttachment attachment in loadedDocument.Attachments)
    {
        //Extracts the attachment and saves it to the disk
        FileStream stream = new FileStream(attachment.FileName, FileMode.Create);
        stream.Write(attachment.Data, 0, attachment.Data.Length);
        stream.Dispose();
    }
}
//Close the document
loadedDocument.Close(true);

By executing this code example, you will get the output like in the following screenshot.
Extract attachments from PDF

Extract filled PDF form data and save it as XML

A PDF document can be used as a form to acquire details from users in an interactive way. For instance, form fields in a PDF document could be used to acquire attendee data for an event registration, but correlating the provided data would be hectic.

In this situation, you can extract the filled data and save it as XML using the ExportData method.

The following code example illustrates the procedure to export filled PDF form data to an XML file.

PdfLoadedDocument loadedDocument = new PdfLoadedDocument(@"../../../../../../Data/FormFill.pdf");
//Load an existing form
PdfLoadedForm loadedForm = loadedDocument.Form;
//Export the existing PDF form data to an XML file
loadedForm.ExportData("Export.xml", DataFormat.Xml, @"AcroForm1");
//Close the document
loadedDocument.Close(true);

By executing this code example, you will get the output like in the following screenshot.

Extract filled PDF form data and save it as XML

Resources

The project samples to perform all these extraction operations are available in this GitHub repository.

Syncfusion’s high-performance PDF Library allows you to create PDF documents from scratch without Adobe dependencies.

Conclusion

In this blog post, we have learned various ways to extract data from a PDF document. Take a moment to peruse our documentation, where you’ll find other operations that can be performed over PDF documents. Every feature is accompanied by code examples.

If you have any questions about these features, please let us know in the comments section below. You can also contact us through our support forum, Support Portal, or feedback portal. We are happy to assist you!

If you like this article, we think you will also like the following articles about the Syncfusion PDF Library:

Tags:

Share this post:

Comments (1)

Hello
I want to extract a table from a pdf and turn it into a list to save it in a database table, is it possible to do this? And if so, will you have an example?

Comments are closed.

Popular Now

Be the first to get updates

Subscribe RSS feed

Be the first to get updates

Subscribe RSS feed