Code To Extract Plain Text From A PDF File - CodeProject
Code To Extract Plain Text From A PDF File - CodeProject
Source code that shows how to decompress and extract text from PDF documents.
Introduction
PDF documents are commonly used and their content is usually compressed. This article shows a simple C code that can be used to extract plain text from the PDF file.
Why?
Adobe does allows you to submit PDF files and will extract the text or HTML and mail it back to you. But there are times when you need to extract the text yourself or do it inside an
application. You may also want to apply special formatting (e.g., add tabs) so that the text can be easily imported into Excel for example (when your PDF document mostly contains tables that
you need to port to Excel, which is how this code got developed).
There are several projects on "The Code Project" that show how to create PDF documents, but none that provide free code that shows how to extract text without using a commercial library.
In the reader comments, a need was expressed for code just like what is being supplied here.
There are several libraries out there that read or create PDF file, but you have to register them for commercial use or sign various agreements. The code supplied here is very simple and
basic, but it is entirely free. It only use the ZLIB library which is also free.
Basics
You can download documents such as PDFReference15_v5.pdf from here that explains some of the inners of PDF files. In short, each PDF file contains a number of objects. Each object may
require one or more filters to decompress it and may also provide a stream of data. Text streams are usually compressed using the FlateDecode filter and may be uncompressed using code
from the ZLIB (http://www.zlib.org/) library.
The data for each object can be found between "stream" and "endstream" sections. Once inflated, the data needs to be processed to extract the text. The data usually contains one or more
text objects (starting with BT and ending with ET) with formatting instructions inside. You can learn a lot from the structure of PDF file by stepping through this application.
About Code
This single source code file contains very simple, very basic C code. It initially reads in the entire PDF file into one buffer and then repeatedly scans for "stream" and "endstream" sections. It
does not check which filter should be applied and always assumes FlateDecode. (If it gets it wrong, usually no output is generated for that section of the file, so it is not a big issue). Once the
data stream is inflated (uncompressed), it is processed. During the processing, the code searches for the BT and ET tokens that signify text objects. The contents of each is processed to
extract the text and a guess is made as to whether tabs or new line characters are needed.
The code is far from complete or being any sort of general utility class, but it does demonstrate how you can extract the text yourself. It is enough to show you how and get you going.
The code is however fully functional, so when it is applied to a PDF document, it generally does a fair job of extracting the text. It has been tested on several PDF files.
Now, step through the application and note that the input PDF and output text file names are hardwired at the start of the main method.
Future Enhancements
If there is enough interest, the author may consider uploading a release version with a Windows interface. The code is quite good for extracting data from tables in a form that can be readily
imported into Excel, with the column preserved (because of the tabs that get added).
Code Snippets
https://www.codeproject.com/Articles/7056/Code-to-extract-plain-text-from-a-PDF-file 1/5
9/28/2017 Code to extract plain text from a PDF file - CodeProject
Stream sections are located using initially:
The main work gets done in the ProcessOutput method which processes the uncompressed stream to extract text portion of any text object. It looks as follows:
https://www.codeproject.com/Articles/7056/Code-to-extract-plain-text-from-a-PDF-file 2/5
9/28/2017 Code to extract plain text from a PDF file - CodeProject
nextliteral = true;
}
else
{
nextliteral = false;
if ( ((c>=' ') && (c<='~')) || ((c>=128) && (c<255)) )
{
fputc(c, file);
}
}
}
}
//Store the recent characters for
//when we have to go back for a number:
for (j=0; j<oldchar-1; j++) oc[j]=oc[j+1];
oc[oldchar-1]=c;
if (!intextobject)
{
if (seen2("BT", oc))
{
//Start of a text object:
intextobject = true;
}
}
}
}
License
This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board
below.
Share
EMAIL TWITTER
Extract Text from PDF in C# (100% .NET) Generate and add keyword variations using AdWords API
From pdf files to plain text in a WebMatrix site Window Tabs (WndTabs) Add-In for DevStudio
https://www.codeproject.com/Articles/7056/Code-to-extract-plain-text-from-a-PDF-file 3/5
9/28/2017 Code to extract plain text from a PDF file - CodeProject
Search Comments
newbie question 2
Member 12989649 8-Feb-17 5:19
PDF extraction 1
Member 12562886 3-Oct-16 9:04
char vs byte 1
Mattias G 28-Apr-16 11:38
Need c# code 1
Ashok_kavi 15-Feb-16 0:03
Message Closed 1
15-Sep-13 16:21
Thank you 1
Member 9962494 3-Apr-13 9:12
[My vote of 1] ! 1
i30mb1 6-Feb-13 21:01
Leila 1
Rashidy_ 30-Dec-12 11:02
My vote of 5 1
manoj kumar choubey 26-Feb-12 19:59
My vote of 5 1
markjuggles 29-Jan-12 11:45
Brillaint idea 1
Member 3058755 10-Aug-11 5:38
My vote of 5 1
Geek Master 6-Oct-10 7:18
https://www.codeproject.com/Articles/7056/Code-to-extract-plain-text-from-a-PDF-file 4/5
9/28/2017 Code to extract plain text from a PDF file - CodeProject
My vote of 5
Allen Friend 2-Oct-10 12:28
THANK YOU!!! 1
marceloflu 18-Feb-10 8:17
diacritics 1
tontoncaidd 21-Sep-09 4:38
thanx 1
mrares 26-Feb-09 13:02
.Net version 2
nstuart 23-Feb-09 10:37
Bug in code... 2
nortonio 29-May-08 4:27
Link Error 1
avinash_ss 5-Mar-08 19:11
Refresh 1 2 Next
General News Suggestion Question Bug Answer Joke Praise Rant Admin
Permalink | Advertise | Privacy | Terms of Use | Mobile Layout: fixed | fluid Article Copyright 2004 by NeWi
Slectionner une langue
Web01 | 2.8.170927.1 | Last Updated 22 Jun 2004 Everything else Copyright CodeProject, 1999-2017
https://www.codeproject.com/Articles/7056/Code-to-extract-plain-text-from-a-PDF-file 5/5