2012-08-31 227 views
0

pdfbox問題PDFBox拋出異常:空引用異常

我使用pdfbox從PDF中提取文本到我的richtextbox。

我不知道有什麼問題,但也有PDF是好的,但也有PDF拋出異常,異常是:

對象引用不設置到對象的實例。

這裏是我的代碼:

using org.pdfbox.pdmodel; 
using org.pdfbox.util; 

private void pdfButton_Click(object sender, EventArgs e) 
{ 
    OpenFileDialog openFD = new OpenFileDialog(); 
    openFD.FileName = ""; 
    openFD.InitialDirectory = "C:\\"; 
    openFD.Filter = "All PDF Files|*.PDF"; 
    openFD.Title = "Browse all PDF files"; 

    if (openFD.ShowDialog() == DialogResult.OK) 
    { 
     try 
     { 
      pdf_filename = Path.GetFileNameWithoutExtension(openFD.Filename); 
      PDDocument pdfFile = PDDocument.load(openFD.Filename); 

      PDFTextStripper pdfStripper = new PDFTextStripper(); 
      richtextBox1.Text = pdfStripper.getText(pdfFile); 
      textBox1.Text = Path.GetFileName(openFD.Filename); 
     } 
     catch (Exception error) 
     { 
      MessageBox.Show(error.ToString()); 
     } 
    } 
} 
+2

在哪一行,你得到的異常? – tomsv

+0

似乎pdfFile或pdfStripper爲空。異常發生在哪一行? – codeteq

+0

異常被catch語句捕獲 – jomsk1e

回答

0

我固定使用iTextSharp的問題。這是我的同事建議的,我通過iTextSharp更改了PDFBox。

如果有人有相同的問題,因爲我這裏的工作代碼:

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


private void pdfButton_Click(object sender, EventArgs e) 
{ 
    OpenFileDialog openFD = new OpenFileDialog(); 
    openFD.FileName = ""; 
    openFD.InitialDirectory = "C:\\"; 
    openFD.Filter = "All PDF Files|*.PDF"; 
    openFD.Title = "Browse all PDF files"; 

    if (openFD.ShowDialog() == DialogResult.OK) 
    { 
     try 
     { 
      pdf_filename = Path.GetFileNameWithoutExtension(openFD.Filename); 

      richtextBox1.Text = ReadPdf(openFD.FileName); 
      textBox1.Text = Path.GetFileName(openFD.Filename); 
     } 

     catch (Exception error) 
     { 
      MessageBox.Show(error.ToString()); 
     } 

    } 
} 


private string ReadPdf(string filename) 
{ 

    if (!File.Exists(filename)) return string.Empty; 

    PdfReader reader = new PdfReader(filename); 

    string text = string.Empty; 

    for (int page = 1; page <= reader.NumberOfPages; page++) 
    { 

     text += PdfTextExtractor.GetTextFromPage(reader, page); 

    } 

    return text; 
}