现在的位置: 首页 > 综合 > 正文

将PDF转为TXT文本格式提取中文

2012年09月16日 ⁄ 综合 ⁄ 共 1697字 ⁄ 字号 评论关闭

 以下的读取方式,针对个别PDF文件会出现读取内容为乱码的现象,如果有高手有其他办法可以解决,就提供下,大家一起学习

 

winform代码

首先要在网上下载:PDFBox

通过引用两个DLL:
IKVM.GNU.Classpath.dll
PDFBox-0.7.3.dll

将两个DLL放在项目的BIN目录下:
FontBox-0.1.0-dev.dll
IKVM.Runtime.dll

//引用的空间,由项目饮用的动态DLL后才能使用
using org.pdfbox.pdmodel;
using org.pdfbox.util;

//按钮事件中的代码
private void button2_Click(object sender, System.EventArgs e)
{

PDDocument txtTmp = PDDocument.load(文件物理路径)
                PDFTextStripper pdfStripper 
= new PDFTextStripper();
                textBox1.Text 
= pdfStripper.getText(txtTmp);
                textBox1.Text 
+= textBox1.Text.Length;

/* 如下的以后使用参考
DialogResult dr = folderDialog.ShowDialog();
            if(dr != DialogResult.OK)
            {
                return;
            }
            //选择文件的路径
            folderPath  = folderDialog.SelectedPath;

DirectoryInfo dirPDF = new DirectoryInfo(folderPath);
            if(!dirPDF.Exists)
            {
                MessageBox.Show("您录入的文件路径有误,请核实!");
                return;
            }

            FileInfo[] fileOldSystem = dirPDF.GetFiles("*.pdf");
            if(fileOldSystem.Length == 0)
            {
                MessageBox.Show("您选择的文件路径下没有PDF文件,请核实!");
                return;
            }

            foreach(FileInfo file in fileOldSystem)
            {
                PDDocument txtTmp = PDDocument.load(file.FullName.ToLower());
                PDFTextStripper pdfStripper = new PDFTextStripper();
                textBox1.Text = pdfStripper.getText(txtTmp);
                textBox1.Text += textBox1.Text.Length;

                StreamWriter sw = File.AppendText(file.FullName.ToLower().Replace(".pdf",".txt"));
                sw.Write(textBox1.Text);
                sw.Flush();
                sw.Close();

            }

*/

}

 

 

 

抱歉!评论已关闭.