使用itextsharp阅读PDF,其中PDF语言为非英语

Rah*_*put 6 .net pdf ms-word itextsharp c#-4.0

我正在尝试使用C#中的itextsharp 读取 PDF文件,它将此pdf转换为word文件.当我尝试使用英语时,它还需要保持表格形式和单词字体pdf它可以完美地工作但是使用一些印度语,如印地语,马拉地语它不起作用.

 public string ReadPdfFile(string Filename)
        {

            string strText = string.Empty;
            StringBuilder text = new StringBuilder();
            try
            {
                PdfReader reader = new PdfReader((string)Filename);
                if (File.Exists(Filename))
                {
                    PdfReader pdfReader = new PdfReader(Filename);

                    for (int page = 1; page <= pdfReader.NumberOfPages; page++)
                    {                        ITextExtractionStrategy strategy = new SimpleTextExtractionStrategy();
                        string currentText = PdfTextExtractor.GetTextFromPage(pdfReader, page, strategy);

                        text.Append(currentText);
                        pdfReader.Close();
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            textBox1.Text = text.ToString();
            return text.ToString(); ;
        }
Run Code Online (Sandbox Code Playgroud)

mkl*_*mkl 16

我检查了你的文件,特别关注你的样本"मतद|र"被提取为文档页面最上面一行的"मतदरर".

简而言之:

您的文档本身提供的信息,例如头条中的字形"मतद|र"代表文本"मतदरर".您应该向文档的来源询问文档版本,其中字体信息不会产生误导.如果那是不可能的,你应该去OCR.

详细地:

第一页的第一行由页面内容流中的以下操作生成:

/9 280 Tf
(-12"!%$"234%56*5) Tj
Run Code Online (Sandbox Code Playgroud)

第一行选择大小为280的名为/ 9的字体(页面开头的操作将所有内容缩放0.05倍;因此,您在文件中观察到的有效大小为14个单位).

第二行导致打印字形.使用该字体的自定义编码在括号之间引用这些字形.

当程序试图提取文本时,它必须使用来自字体的信息从这些字形引用中推导出实际字符.

使用以下对象定义PDF第一页上的font / 9:

242 0 obj<<
    /Type/Font/Name/9/BaseFont 243 0 R/FirstChar 33/LastChar 94
    /Subtype/TrueType/ToUnicode 244 0 R/FontDescriptor 247 0 R/Widths 248 0 R>>
endobj
243 0 obj/CDAC-GISTSurekh-Bold+0
endobj 
247 0 obj<<
    /Type/FontDescriptor/FontFile2 245 0 R/FontBBox 246 0 R/FontName 243 0 R
    /Flags 4/MissingWidth 946/StemV 0/StemH 0/CapHeight 500/XHeight 0
    /Ascent 1050/Descent -400/Leading 0/MaxWidth 1892/AvgWidth 946/ItalicAngle 0>>
endobj 
Run Code Online (Sandbox Code Playgroud)

所以没有/ Encoding元素,但至少有一个/ ToUnicode映射的引用.因此,提取文本的程序必须依赖于给定的/ ToUnicode映射.

从(-12"!%$"234%56*5)中提取文本时,/ ToUnicode引用的流包含以下感兴趣的映射:

<21> <21> <0930>
<22> <22> <0930>
<24> <24> <091c>
<25> <25> <0020>
<2a> <2a> <0031>
<2d> <2d> <092e>
<31> <31> <0924>
<32> <32> <0926>
<33> <33> <0926>
<34> <34> <002c>
<35> <35> <0032>
<36> <36> <0030>
Run Code Online (Sandbox Code Playgroud)

(在这里你可以看到多个字符代码被映射到同一个unicode代码点...)

因此,文本提取必须导致:

- = 0x2d -> 0x092e = ?
1 = 0x31 -> 0x0924 = ?
2 = 0x32 -> 0x0926 = ?
" = 0x22 -> 0x0930 = ?    instead of  |
! = 0x21 -> 0x0930 = ?
% = 0x25 -> 0x0020 =  
$ = 0x24 -> 0x091c = ?
" = 0x22 -> 0x0930 = ?
2 = 0x32 -> 0x0926 = ?
3 = 0x33 -> 0x0926 = ?
4 = 0x34 -> 0x002c = ,
% = 0x25 -> 0x0020 =  
5 = 0x35 -> 0x0032 = 2
6 = 0x36 -> 0x0030 = 0
* = 0x2a -> 0x0031 = 1
5 = 0x35 -> 0x0032 = 2
Run Code Online (Sandbox Code Playgroud)

因此,从第一个文档页面上的标题中提取的文本iTextSharp(以及Adobe Reader!)正是其字体信息中声明的文档正确无误的内容.

由于这个原因是字体定义中的误导性映射信息,因此整个文档中存在误解并不奇怪.

  • 更好的解决方案是适当的源文档.OCR的工作原理是将PDF页面渲染为位图图形(例如使用PDFBox)并对其应用OCR.我没有经验,哪种OCR软件对这项工作有好处.如果你想接受dare,你可能想要创建一些代码,只渲染给定PDF中字体中包含的字形,对它们进行OCR,导出正确的**/ToUnicode**表,并将这些表添加到相应PDF中的字体. (2认同)