2016-12-02 91 views
1

讀表我的工作將PDF轉換爲文本。我可以正確地從PDF中獲取文本,但它在表格結構中很複雜。我知道PDF不支持表結構,但我認爲有一種方法可以正確獲取單元格。嗯,比如說:iTextSharp的如何在PDF文件

我想轉換爲文本是這樣的:

> This is first example. 

> This is second example. 

但是,當我將PDF轉換爲文本,theese DATAS看起來像這樣:

> This is This is 

> first example. second example. 

如何正確獲取值?

- 編輯:

下面是我怎麼將PDF轉換爲文本:

OpenFileDialog ofd = new OpenFileDialog(); 
     string filepath; 
     ofd.Filter = "PDF Files(*.PDF)|*.PDF|All Files(*.*)|*.*"; 

     if (ofd.ShowDialog() == DialogResult.OK) 
     { 
      filepath = ofd.FileName.ToString(); 

      string strText = string.Empty; 
      try 
      { 
       PdfReader reader = new PdfReader(filepath); 

       for (int page = 1; page < reader.NumberOfPages; page++) 
       { 
        ITextExtractionStrategy its = new iTextSharp.text.pdf.parser.LocationTextExtractionStrategy(); 
        string s = PdfTextExtractor.GetTextFromPage(reader, page, its); 

        s = Encoding.UTF8.GetString(ASCIIEncoding.Convert(Encoding.Default, Encoding.UTF8, Encoding.Default.GetBytes(s))); 
        strText += s; 
       } 
       reader.Close(); 
      } 
      catch (Exception ex) 
      { 
       MessageBox.Show(ex.Message); 
      } 
     } 
+0

能否請您分享您所使用的檢索文字的代碼? – Bassie

+0

@Bassie謝謝,我更新了我的帖子。 – pseudocode

+0

看起來這是不可能的,默認情況下,檢查這個可能的解決方案:http://stackoverflow.com/questions/7513209/using-locationtextextractionstrategy-in-itextsharp-for-text-coordinate/7515625#7515625 – Bassie

回答

1

爲了使我的評論的實際的答案...

您使用LocationTextExtractionStrategy爲文字提取:

ITextExtractionStrategy its = new iTextSharp.text.pdf.parser.LocationTextExtractionStrategy(); 
string s = PdfTextExtractor.GetTextFromPage(reader, page, its); 

This str ategy會從上到下排列從左到右的所有文本(實際上也考慮了文本行的角度)。因此,顯然不是您需要從具有多行內容單元格的表中提取文本。

根據有關文件也有不同的方法可以採取:

  • 使用iText的SimpleTextExtractionStrategy如果有問題的文檔中的文本繪製操作已經是一個想要在文本抽取的順序。
  • 使用自定義文本提取的策略,利用標籤信息,如果該文件表標籤正確的。
  • 使用其嘗試從文本安排,線路路徑,或背景顏色提示猜出表格單元格結構,並通過細胞中提取文本單元的複雜的自定義文本提取的策略。

在這種情況下,OP評論說,他SimpleTextExtractionStrategy改變LocationTextExtractionStrategy,然後它的工作。

相關問題