操作PDF文档功能的相关开源项目探索—iTextSharp 和PDFBox
转自www.th7.cn第七城市
操作PDF文档功能的相关开源项目探索—iTextSharp?和PDFBoxfont));
??????????????? document.Close();
??????????? }
??????????? catch (Exception de)
??????????? {
??????????????? Console.WriteLine(de.StackTrace);
??????????? }
???????2.从http://sourceforge.net/projects/itextsharp/?下载扩展字体库iTextAsianCmaps.dll 和iTextAsian.dll,支持亚洲字体。
下载界面如下:
?
????
??????? /// <summary>
??????? /// 创建中文字体(实现中文)
??????? /// </summary>
??????? /// <returns></returns>
??????? public static iTextSharp.text.Font CreateChineseFont()
??????? {
??????????? BaseFont.AddToResourceSearch("iTextAsian.dll");
??????????? BaseFont.AddToResourceSearch("iTextAsianCmaps.dll"); //"STSong-Light", "UniGB-UCS2-H",
??????????? BaseFont baseFT=BaseFont.CreateFont("STSong-Light", "UniGB-UCS2-H", BaseFont.EMBEDDED);
??????????? iTextSharp.text.Font font = new iTextSharp.text.Font(baseFT);
??????????? return font;
??????? }
"UniGB-UCS2-H" "UniGB-UCS2-V"是简体中文。 "STSong-Light"是字体名称。BaseFont.EMBEDDED是将字体嵌入文档内。
其次,我接下来尝试在使用iTextSharp读对象类时,指定字体库,可是很遗憾没有相应方法。请参照:http://www.cnblogs.com/diction/articles/1120984.html?(提取文本不支持中文)而且,即使有也很不灵活,因为你不可能预知PDF文档中使用的字体,PDF文档中可能有多种字体。后来,搜索网页相关信息发现:原来iTextSharp的操作PDF文档优势是PDF文档的创建。
需求是学习和工作的动力
???? 我的原始目标是找到PDF文档内容提取为文本的方法,我转向《How to parse PDF files》?该文章完整讲述了PDF文档提取文本的方法和整个解决过程思路,我会单独转载该文章,希望不能访问国外网的网友也能看到。PDFBox的下载http://sourceforge.net/projects/pdfbox/files/?下载解压后里面内容很丰富,

所有需要的dll都包含在Bin文件夹里面
?

?
"PDFBox is a Java PDF Library. This project will allow access to all of the components in a PDF document. More PDF manipulation features will be added as the project matures. This ships with a utility to take a PDF document and output a text file. "
PDFBox是个JAVA开源项目,里面使用IKVM.NET开源项目http://www.ikvm.net/?支持JAVA类库在.NET中调用。
IKVM.NET is an implementation of Java for Mono and the Microsoft .NET Framework. It includes the following components:
A Java Virtual Machine implemented in .NET A .NET implementation of the Java class libraries Tools that enable Java and .NET interoperability对IKVM.NET的学习,对以后在.NET下使用JAVA类库很有帮助,其实IKVM.Runtime.dll 就是封装了JAVA类库的运行环境。
需要添加的DLL有:FontBox-0.1.0-dev.dll、IKVM.GNU.Classpath.dll、IKVM.Runtime.dll、PDFBox-0.7.3.dll
PDFBox使用实例代码如下:请参照:http://www.cnblogs.com/wuhenke/archive/2010/04/16/1713949.html
??????? private static string parseUsingPDFBox(string filename)
??????? {
??????????? PDDocument doc = PDDocument.load(filename);
??????????? PDFTextStripper stripper = new PDFTextStripper();
???????????
??????????? return stripper.getText(doc);
??????? }
PDFBox功能很强大,有时间值得好好学习一下。
?
?
参考:
http://www.codeproject.com/kb/cpp/ExtractPDFText.aspx?df=100&forumid=47947
http://www.codeproject.com/KB/string/pdf2text.aspx
http://www.cnblogs.com/hardrock/
http://www.ikvm.net/