sudharsan s

sudharsan s

  • NA
  • 204
  • 85.4k

pdf reader in C#

Jul 2 2012 1:27 AM
Hi,

while uploading the pdf file i want to read the content of that particular pdf document. and i want to show the document in grid.



my problem is:

if the pdf document contains 5 pages means it display in grid 5 times.
and 5 th time the whole content of pdf is saved. 1 time it saved 1 page content. 2 time it save 1 and 2 page content like upto 4 pages.

BUt i want to show only one time in grid and i want to save the 5 pages content data at once.


here i am using the code to read pdf document:





 else if (extn.Equals(".pdf"))
  {
  string filePath = Server.MapPath("uploads/" + filename);
  string pdfText = string.Empty;

  PdfReader pdfreader = new PdfReader(filePath);
 
 
 
  for (int j = 1; j <= pdfreader.NumberOfPages; j++)
  {

  ITextExtractionStrategy itextextStrat = new iTextSharp.text.pdf.parser.SimpleTextExtractionStrategy();

  // ITextExtractionStrategy strategy = new SimpleTextExtractionStrategy();

  String extractText = PdfTextExtractor.GetTextFromPage(pdfreader, j, itextextStrat);

  extractText = Encoding.UTF8.GetString(ASCIIEncoding.Convert(Encoding.Default, Encoding.UTF8, Encoding.Default.GetBytes(extractText)));

  pdfText = pdfText + extractText;


  // byte[e] bytes = Encoding.UTF8.GetBytes("?"); //bytes now holds 0xDB8C
  // byte[] converted = Encoding.Convert(Encoding.Default, Encoding.UTF8, bytes);//converted now holds 0xC39BC592
  //// string final = Encoding.UTF8.GetString(converted);
  // string pdftext = Encoding.UTF8.GetString(converted);

  pdfreader.Close();

  // query = "INSERT INTO Fileupload([category],[subcategory],[uid],[oid],[up_name],[demo1],[keyword],[fdata],[aflag],sizebyte) VALUES('" + dropcategory.SelectedValue + "','" + DropDownList1.SelectedValue + "','" + Session["user_id"].ToString() + "','" + txtOwnerId.SelectedItem + "','" + fname + "','" + demo1.Text + "','" + key + "','" + pdfText + "',1,'" + size + "')";

  query = @"INSERT INTO Fileupload([category],[subcategory],[uid],[oid],[up_name],[demo1],[keyword],[fdata],[aflag],sizebyte)
VALUES(@category,@subcategory,@uid,@oid,@up_name,@demo1,@keyword,@fdata,1,@sizebyte)  ";
  SqlCommand command = new SqlCommand(query, con);
  //command.CommandText = command.CommandText.ToString();
  command.Parameters.AddWithValue("@category", dropcategory.SelectedValue.ToString());
  command.Parameters.AddWithValue("@subcategory", DropDownList1.SelectedValue.ToString());
  command.Parameters.AddWithValue("@uid", Session["user_id"].ToString());
  command.Parameters.AddWithValue("@oid", txtOwnerId.SelectedItem.ToString());
  command.Parameters.AddWithValue("@up_name", fname);
  command.Parameters.AddWithValue("@demo1", demo1.Text);
  command.Parameters.AddWithValue("@keyword", key);
  command.Parameters.AddWithValue("@fdata", pdfText);
  command.Parameters.AddWithValue("@sizebyte", size);
  command.ExecuteNonQuery();
  }
  }



PLs Help me for this.




Answers (3)