SelectPdf for .NET - Pdf To Text Converter. Extract Text from PDF - C# / ASP.NET Sample

This sample shows how to use SelectPdf Pdf Library for .NET to extract text from a PDF document.

The sample uses the following (existing) test PDF:
Test PDF document

Text Layout:


Start Page:


End Page:

(Leave empty to extract until the last page)

Note: The free trial version of SelectPdf will always extract text from the first 3 pages of the PDF document, no matter the page settings received.


Sample Code C#



using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace SelectPdf.Samples
{
    public partial class pdf_to_text_converter : System.Web.UI.Page
    {
        protected void BtnSubmit_Click(object sender, EventArgs e)
        {
            // the test file
            string filePdf = Server.MapPath("~/files/selectpdf.pdf");

            // settings
            string text_layout = DdlTextLayout.SelectedValue;
            TextLayout textLayout = (TextLayout)Enum.Parse(typeof(TextLayout),
                text_layout, true);

            int startPage = 1;
            try
            {
                startPage = Convert.ToInt32(TxtStartPage.Text);
            }
            catch { }

            int endPage = 0;
            try
            {
                endPage = Convert.ToInt32(TxtEndPage.Text);
            }
            catch { }

            // instantiate a pdf to text converter object
            PdfToText pdfToText = new PdfToText();

            // load PDF file
            pdfToText.Load(filePdf);

            // set the properties
            pdfToText.Layout = textLayout;
            pdfToText.StartPageNumber = startPage;
            pdfToText.EndPageNumber = endPage;

            // extract the text
            string text = pdfToText.GetText();

            // convert text to UTF-8 bytes
            byte[] utf8 = System.Text.Encoding.UTF8.GetBytes(text);

            // send text to browser
            Response.Clear();
            Response.ClearHeaders();

            Response.AddHeader("Content-Type", "text/plain; charset=UTF-8");
            Response.AddHeader("Content-Length", utf8.Length.ToString());
            Response.AppendHeader("content-disposition", 
                "attachment;filename=\"output.txt\"");

            Response.BinaryWrite(utf8);
            Response.End();
        }
    }
}