Click here to Skip to main content
14,699,570 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hiiii friend...
i have to read pdf file using itextsharp so plzg give some code of that....

in my application many file in one page so i will generate pdf page so i was get values in that files to store in my sql database....

any one know about that so give me some code or hint...
Posted

What do you mean by read the PDF file? I'm not kidding asking this question because it's important to understand that a PDF file isn't a structured file. In other words, you can't say that you can retrieve a paragraph, for instance, just by reading some strings. Plus, do you want to consider image data in this as well? About the best that you can do is something like this:
public string ParsePdf(string fileName)
{
  if (!File.Exists(fileName))
    throw new FileNotFoundException("fileName");
  using (PdfReader reader = new PdfReader(fileName))
  {
    StringBuilder sb = new StringBuilder();

    ITextExtractionStrategy strategy = new SimpleTextExtractionStrategy();
    for (int page = 0; page < reader.NumberOfPages; page++)
    {
      string text = PdfTextExtractor.GetTextFromPage(reader, page + 1, strategy);
      if (!string.IsNullOrWhitespace(text))
      {
        sb.Append(Encoding.UTF8.GetString(ASCIIEncoding.Convert(Encoding.Default, Encoding.UTF8, Encoding.Default.GetBytes(text))));
      }
    }

    return sb.ToString();
  } 
 }
}
This uses a simple reader provided by ITextSharp to read the text out. There's no attempt to create anything like paragraphs out of this.
   
Comments
Sagar Tajpara 6-Mar-12 7:38am
   
first of all thanks....

i have create table ilke this

username : textbox1
pwd: textbox2
city: textbox3
ph no: textbox4


so this pdf i was display in client side and client user edit that record so i need to get that record to enter by user....


thanks again
static void Main(string[] args)
        {
            string[] strings = { pdffile1 };
            ReadPdf("D:\\", strings);
        }
        private static void ReadPdf(string outputFilePath, string[] pdfFiles)
        {
            if (pdfFiles.Length > 0)
            {
                Console.WriteLine("Reading Pdf file.....");
                PdfDocument outputPDFDocument = new PdfDocument();

                foreach (string pdfFile in pdfFiles)
                {
                    PdfDocument inputPDFDocument = PdfReader.Open(pdfFile, PdfDocumentOpenMode.Import);
                    outputPDFDocument.Version = inputPDFDocument.Version;
                    foreach (PdfPage page in inputPDFDocument.Pages)
                    {
                        outputPDFDocument.AddPage(page);
                    }
                }
                outputPDFDocument.Save(outputFilePath);
                Console.WriteLine("Successfully Completed the pdf documents");
                Console.WriteLine("File path is: {0}", MergedFileresultspath);
                Console.ReadLine();
            }
            else
                Console.WriteLine("Please Give the pdf file path before read & save");

        }
   
Comments
Sagar Tajpara 6-Mar-12 7:50am
   
thanks
shefeekcm 29-Aug-13 6:39am
   
which API you are using for this purpose
Bojjaiah 19-Jun-12 0:47am
   
Welcome....
   
Comments
Sagar Tajpara 6-Mar-12 7:50am
   
thanks
Sagar Tajpara 6-Mar-12 7:52am
   
i just store that record in my sqldatabase so what will you do?
plz if u have any idea or code then reply me
Anuja Pawar Indore 6-Mar-12 8:23am
   
Refer this also
http://www.eggheadcafe.com/community/asp-net/17/10425399/export-grid-view-ittem-template-values-into-pdf-in-c.aspx
Sagar Tajpara 6-Mar-12 22:54pm
   
thanks...
You can try this

using System.IO;
using iTextSharp;
using iTextSharp.text;
using iTextSharp.text.pdf;


using (MemoryStream myMemoryStream = new MemoryStream())
            {
                Document myDocument = new Document();
                PdfWriter myPDFWriter = PdfWriter.GetInstance(myDocument, myMemoryStream);
 
                myDocument.Open();
 
                // Add to content to your PDF here... 
                PdfPTable table = new PdfPTable(2);
                PdfPCell header = new PdfPCell(new Phrase("Your Heading"));
                header.Colspan = 2;
                header.HorizontalAlignment = 1; //0=Left, 1=Centre, 2=Right
                table.AddCell(header);
                table.AddCell("ID:" + Textbox1.Text);  //Your textboxes
                myDocument.Add(table);
                myDocument.Close();
 
                byte[] content = myMemoryStream.ToArray();
 
          // Write out PDF from memory stream. 
          using (FileStream fs = File.Create("C:\\Test.pdf")) //Your pdf file
                {
                    fs.Write(content, 0, (int)content.Length);
                }
            } 


On the form you want to view the PDf, you can write the following on the Load event of the Form

private string strFilename = "";

strFilename = @"C:\aTestFile.pdf";
axAcroPDF1.src = strFilename;
//I used a Adobe PDF Reader component from the toolbox that you need to add. You can get it from the COM components-Adobe PDF Reader
   
v4
Comments
Sagar Tajpara 6-Mar-12 22:54pm
   
thanks...
first of all thanks....

i have create table ilke this

username : textbox1
pwd: textbox2
city: textbox3
ph no: textbox4


so this pdf i was display in client side and client user edit that record so i need to get that record to enter by user....


thanks again
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900