从pdf文档中获取文本itextsharp

时间:2015-10-16 20:23:46

标签: c# .net pdf itextsharp

我尝试使用iTextSharp从pdf文档中获取文本, 如果pdf文件是英文文本(拉丁字符),它的效果很好。 如果我试图从带有西里尔字符的pdf文档中获取文本,则输出只是问号。是否有一些设置,或者不支持西里尔文? 这是创建pdf的代码:

string testText = "зззi";
        string tmpFile = @"C:\items\test.pdf";
        string myFont = @"C:\windows\fonts\verdana.ttf";
        iTextSharp.text.Rectangle pgeSize = new iTextSharp.text.Rectangle(595, 792);
        iTextSharp.text.Document doc = new iTextSharp.text.Document(pgeSize, 10, 10, 10, 10);
        iTextSharp.text.pdf.PdfWriter wrtr;
        wrtr = iTextSharp.text.pdf.PdfWriter.GetInstance(doc,
            new System.IO.FileStream(tmpFile, System.IO.FileMode.Create));
        doc.Open();
        doc.NewPage();
        iTextSharp.text.pdf.BaseFont bfR;

        bfR = BaseFont.CreateFont(myFont, BaseFont.IDENTITY_H, BaseFont.EMBEDDED);

        iTextSharp.text.BaseColor clrBlack =
            new iTextSharp.text.BaseColor(0, 0, 0);
        iTextSharp.text.Font fntHead =
            new iTextSharp.text.Font(bfR, 34, iTextSharp.text.Font.NORMAL, clrBlack);

        iTextSharp.text.Paragraph pgr =
            new iTextSharp.text.Paragraph(testText, fntHead);
        doc.Add(pgr);
        doc.Close();

这是检索文本的代码:

PdfReader reader1
            = new PdfReader("c:/items/test.pdf");
        Console.WriteLine(PdfTextExtractor.GetTextFromPage(reader1, 1, new SimpleTextExtractionStrategy()));
        Console.ReadLine();

输出是:??? i

编辑2 我设法从我创建的pdf中读取文本,但仍无法从随机pdf中获取文本。如何检查pdf是否提供了文本提取所需的信息?

0 个答案:

没有答案