I need to extract data from .PDF files and load it in to SQL 2008. Can any one tell me how to proceed??
-
What *data* contain those PDF files that you want to extract? – Darin Dimitrov Jan 24 '11 at 16:31
-
Info related to books(basically a list of best seller books in the market) – S.. Jan 24 '11 at 16:39
-
1I think you mean metadata, which is the title and author and such – Edo Mangelaars Jan 24 '11 at 17:09
-
1@Edootjuh I don't think he means metadata about the file - it sounds more like he wants to extract the contents of the file, which consists of a table of data, right S? – Benubird Mar 02 '11 at 17:06
-
the file contains book info like title author price etc....i want to extract that info from the PDF file – S.. Mar 10 '11 at 16:34
4 Answers
Here is an example of how to use iTextSharp to extract text data from a PDF. You'll have to fiddle with it some to make it do exactly what you want, I think it's a good outline. You can see how the StringBuilder is being used to store the text, but you could easily change that to use SQL.
static void Main(string[] args)
{
PdfReader reader = new PdfReader(@"c:\test.pdf");
StringBuilder builder = new StringBuilder();
for (int x = 1; x <= reader.NumberOfPages; x++)
{
PdfDictionary page = reader.GetPageN(x);
IRenderListener listener = new SBTextRenderer(builder);
PdfContentStreamProcessor processor = new PdfContentStreamProcessor(listener);
PdfDictionary pageDic = reader.GetPageN(x);
PdfDictionary resourcesDic = pageDic.GetAsDict(PdfName.RESOURCES);
processor.ProcessContent(ContentByteUtils.GetContentBytesForPage(reader, x), resourcesDic);
}
}
public class SBTextRenderer : IRenderListener
{
private StringBuilder _builder;
public SBTextRenderer(StringBuilder builder)
{
_builder = builder;
}
#region IRenderListener Members
public void BeginTextBlock()
{
}
public void EndTextBlock()
{
}
public void RenderImage(ImageRenderInfo renderInfo)
{
}
public void RenderText(TextRenderInfo renderInfo)
{
_builder.Append(renderInfo.GetText());
}
#endregion
}

- 1,067
- 8
- 15
Imagine if you asked this question. How can I load data from arbitrary text files into a SQL table. The challenge isn't opening the text file and reading it, its getting meaningful data out of the files automatically.
So you can use either iText or pdfSharp to read the PDF files, but its the getting meaningful data out that's going to be the challenge.

- 51,984
- 12
- 96
- 155
-
1More specifically, the C# implementation of iText iTextSharp :--> http://sourceforge.net/projects/itextsharp/ – Pandincus Jan 24 '11 at 16:57
-
I tried ItextSharp but it did not work, basically almost all the functions in it are to create and edit a PDF doc but not to read the data.....could u provide me a sample example? – S.. Feb 09 '11 at 16:19
what you need to do is to use a tool to extract the text from PDF first and then read the file into a binary reader .. then store it into your database .. for extracting the text there are several tools to use. the first to mention are:
these are the most well known and well documented ones! check the following examples: try the following examples on code project:
These do the job and they ain't hard to understand. Hope they help you :-)
A final note: as for me, i would iTextSharp as it's the most well documented library with most available examples.

- 1,358
- 3
- 15
- 29
If you mean metadata, try this question (first answer)
Read/Modify PDF Metadata using iTextSharp
You'll have to do the database stuff yourself though.

- 1
- 1

- 352
- 2
- 10