问题描述
privatevoidbutton1_Click(objectsender,EventArgse){strings="";PdfReaderpdfr=newPdfReader("d:\sj.pdf");IList<Dictionary<string,object>>list=SimpleBookmark.GetBookmark(pdfr,pdfr.Catalog,true);foreach(Dictionary<string,object>iinlist){stringtitle=(string)i["Title"];string[]pagenum=((string)i["Page"]).Split('');s+=title.Substring(0,title.Length-1)+""+pagenum[0]+"n";}richTextBox1.Text=s;}
ITextSharp.dll当中的SimpleBookmark只能读取简单的bookmarks比如这种bookmark1bookmark2bookmark3bookmark4bookmark5bookmark6如果复杂一点,比如这种,就不行,不知道到有没有解决办法??bookmark1bookmark2bookmark3bookmark4bookmark5bookmark6
解决方案
本帖最后由 hqwjohn 于 2014-12-12 01:25:47 编辑
解决方案二:
c#使用itextsharp读取pdf文件的文本内容publicstringReadPdfFile(stringfileName){StringBuildertext=newStringBuilder();if(File.Exists(fileName)){PdfReaderpdfReader=newPdfReader(fileName);for(intpage=1;page<=pdfReader.NumberOfPages;page++){ITextExtractionStrategystrategy=newSimpleTextExtractionStrategy();stringcurrentText=PdfTextExtractor.GetTextFromPage(pdfReader,page,strategy);currentText=Encoding.UTF8.GetString(ASCIIEncoding.Convert(Encoding.Default,Encoding.UTF8,Encoding.Default.GetBytes(currentText)));text.Append(currentText);}pdfReader.Close();}returntext.ToString();}
解决方案三:
感谢你的回复,但是你的回答似乎没有针对我提的问题
解决方案四:
usingMicrosoft.VisualBasic;usingSystem;usingSystem.Collections;usingSystem.Collections.Generic;usingSystem.Data;usingSystem.Diagnostics;usingiTextSharp.text;usingiTextSharp.text.pdf;publicclassForm1{privatevoidButton1_Click(System.Objectsender,System.EventArgse){PdfReaderobjReader=default(PdfReader);Label1.Text="";objReader=newPdfReader("D:\梅英精選集.pdf");//讀取書籤Label1.Text+="書籤:"+ShowBookMark(SimpleBookmark.GetBookmark(objReader),0)+Constants.vbNewLine;objReader.Close();}publicstringShowBookMark(IList<Dictionary<string,object>>objBookMark,intintLevel){Dictionary<string,object>objItem;stringstrBookMark;stringstrSpace;strBookMark="";strSpace=" ";//空白字串用來取得內縮字元foreach(Dictionary<string,object>objItem_loopVariableinobjBookMark){objItem=objItem_loopVariable;strBookMark+=strSpace.Substring(0,intLevel)+objItem["Title"]+objItem["Page"].split("")(0)+Constants.vbNewLine;if(objItem.ContainsKey("Kids"))strBookMark+=ShowBookMark(objItem["Kids"],intLevel+1);//如果有子項目則遞迴呼叫ShowBookMark顯示書籤}returnstrBookMark;}}