【问题标题】:iTextSharp returns only whitespaces string for specific PDF fileiTextSharp 仅返回特定 PDF 文件的空格字符串
【发布时间】:2020-07-26 13:36:39
【问题描述】:

我正在测试这个简单的代码:

using iTextSharp.text.pdf;
using iTextSharp.text.pdf.parser;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace PDF_TXT2
{
    class Program
    {
        [STAThread]
        static void Main(string[] args)
        {

            string path = args[0];

            string pathFileName = System.IO.Path.GetFileNameWithoutExtension(path);

            string pathFolder = System.IO.Path.GetDirectoryName(path);

            PdfReader reader = new PdfReader(path);

            string text = string.Empty;
            for (int page = 1; page <= reader.NumberOfPages; page++)
            {
                text += PdfTextExtractor.GetTextFromPage(reader, page);
            }
            reader.Close();

            Clipboard.SetText(text);
            MessageBox.Show(text);
        }
    }
}

这个特定的PDF 文件会导致一个空字符串。实际上不是空的,而是充满了空格。

你能帮我理解为什么吗?

非常感谢!

【问题讨论】:

    标签: pdf itext text-extraction


    【解决方案1】:

    您的 PDF 中的字体有这个条目

    /ToUnicode/Identity-H
    

    即ToUnicode 的值是 name Identity-H。

    不过,根据 PDF 规范,ToUnicode 的值必须是 stream!

    转Unicode 溪流 (可选) 包含将字符代码映射到 Unicode 值的 CMap 文件的流(参见 9.10,“文本内容的提取”)。

    因此,您文件中的 ToUnicode 映射无效,这可能导致文本提取过程中出现任意错误。

    【讨论】:

    • 你能告诉我你在哪里找到这些属性吗?
    • 我使用了 PDF 结构浏览器(本例中为 iText RUPS),查看了相关 PDF 页面的字体资源的内容。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-07-05
    • 1970-01-01
    • 1970-01-01
    • 2022-10-13
    • 1970-01-01
    • 2017-01-02
    • 2022-01-27
    相关资源
    最近更新 更多