日期:2014-05-17 浏览次数:20633 次
private void Form1_Load(object sender, EventArgs e)
{
string path = Application.StartupPath + "\\大话设计模式.pdf";
FileInfo file = new FileInfo(path);
getStringFormPDF(file);
//getBook(file);
}
public void getBook(FileInfo PDF)
{
Hashtable ht = new Hashtable();
PDDocument doc = PDDocument.load(PDF.FullName);
PDDocumentOutline root = doc.getDocumentCatalog().getDocumentOutline();
PDOutlineItem item = root.getFirstChild();
while (item != null)
{
MessageBox.Show("Item:" + item.getTitle());
PDOutlineItem child = item.getFirstChild();
while (child != null)
{
MessageBox.Show(" Child:" + child.getTitle());
child = child.getNextSibling();
}
//ht.Add(item,1);
item = item.getNextSibling();
}
}
public string getStringFormPDF(FileInfo val_PDFInfo)
{
PDDocument doc = PDDocument.load(val_PDFInfo.FullName);
PDDocumentOutline root = doc.getDocumentCatalog().getDocumentOutline();
PDOutlineItem item = root.getFirstChild();
PDFTextStripper pdfStripper = new PDFTextStripper();
while (item != null)
{
//MessageBox.Show("Item:" + item.getTitle());
if (item.getTitle().Trim()=="目录")
{
item = pdfStripper.getStartBookmark();//开始书签
}
//[color=#FF0000]问题在这,进入第一个“目录”后,到这里就报错了。[/color]
if (item.getTitle().Trim() == "目录2")//[color=#FF0000]这里报错[/color]
{
item = pdfStripper.getEndBookmark();//结束书签
}
//PDOutlineItem child = item.getFirstChild();
//while (child != null)
//{
// MessageBox.Show(" Child:" + child.getTitle());
// child = child.getNextSibling();
//}
item = item.getNextSibling();
}
string text = pdfStripper.getText(doc);
return string.Empty; ;
}