2011-05-11 3 views

답변

0
using iTextSharp.text; 
using iTextSharp.text.pdf; 
using iTextSharp.text.pdf.parser; 

public DataTable ImportPDF(string Filename) 
    { 
     string strText = string.Empty; 
     List<string[]> list = new List<string[]>(); 
     string[] PdfData = null; 
     try 
     { 
      PdfReader reader = new PdfReader((string)Filename); 
      for (int page = 1; page <= reader.NumberOfPages; page++) 
      { 
       ITextExtractionStrategy its = new iTextSharp.text.pdf.parser.LocationTextExtractionStrategy(); 
       String cipherText = PdfTextExtractor.GetTextFromPage(reader, page, its); 
       cipherText = Encoding.UTF8.GetString(ASCIIEncoding.Convert(Encoding.Default, Encoding.UTF8, Encoding.Default.GetBytes(cipherText))); 
       strText = strText + "\n" + cipherText; 
       PdfData = strText.Split('\n'); 

      } 
      reader.Close(); 
     } 
     catch (Exception ex) 
     { 
     } 

     List<string> temp = PdfData.ToList(); 
     temp.RemoveAt(0); 
     list = temp.ConvertAll<string[]>(x => x.Split(' ').ToArray()); 
     List<string> columns = list.FirstOrDefault().ToList(); 
     DataTable dtTemp = new DataTable(); 
     columns.All(x => { dtTemp.Columns.Add(new DataColumn(x)); return true; }); 
     list.All(x => { dtTemp.Rows.Add(dtTemp.NewRow().ItemArray = x); return true; }); 
     return dtTemp; 
    } 
관련 문제