How to read pdf file in c# windows application.....


Thanks in advance

Dani AI

Generated

Following 's pointer and 's note about wanting "line by line" access, a few clarifications and a compact workflow are useful. PDF is a positioned document format, not a plain-text stream: text can be stored as positioned glyphs, split across objects, or rendered as images. That is why a naive "read line by line" approach often fails.

A simple, practical extraction (works for many PDFs) is to pull page text with a library and then split on newlines. Example using a common .NET PDF library:

using System.Text;
using iTextSharp.text.pdf;
using iTextSharp.text.pdf.parser;

string ExtractAllText(string path)
{
    var sb = new StringBuilder();
    using (var reader = new PdfReader(path))
    {
        for (int i = 1; i <= reader.NumberOfPages; i++)
        {
            var strategy = new SimpleTextExtractionStrategy();
            string page = PdfTextExtractor.GetTextFromPage(reader, i, strategy);
            sb.AppendLine(page);
        }
    }
    return sb.ToString();
}

// later:
var text = ExtractAllText("file.pdf");
var lines = text.Split(new[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries);

If splitting by newline produces broken or out-of-order lines, use a position-aware strategy: extract text runs with their X/Y coordinates, group runs by Y (within a tolerance), sort by X, then join runs to rebuild visual lines. Steps: 1) collect words/segments with baseline Y, 2) cluster Ys into rows (tolerance = font-size * 0.5), 3) sort within each row by X, 4) merge and fix hyphenation/columns as needed.

For scanned PDFs (empty extraction or the PDF contains images), add an OCR step (Tesseract or other OCR engines) after rendering pages to bitmaps. Licensing note: some PDF libraries are AGPL/commercial — confirm license before using in closed-source or commercial projects. Common troubleshooting: test several PDFs (fonts, subsets, embedded encodings) and inspect raw extraction to determine whether text is missing, garbled, or simply unstructured.

Recommended Answers

All 3 Replies

iText library is what you may want to try. It has support for Java and C#

Actually i tried Itextsharp lib. but not found any source code to read pdf file line by line.

Usually you do not read PDF line by line. You have to read document (in Java PdfReader), then using high-level objects such as Chunk, Phrase, Paragraph, List, and so on you can access elements of page. These objects are often referred to as iText's basic building blocks.
However this may not work properly in case that PDF document is converted bunch of images (usually the case after scanning the documents) as these would need OCR process (Optical Character Recognition)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.