【问题标题】:C# iText7 text coordinate extraction questionC# iText7文本坐标提取题
【发布时间】:2021-04-29 09:51:18
【问题描述】:

我正在使用 iText7 开发 PDF 文本提取器,并注意到某个 PDF 上的奇怪文本坐标。大多数文档似乎在页面的高度和宽度内产生 x 和 y 坐标,但有一个似乎产生负数。我想知道这里是否有处理负坐标的标准方法。这种基本方法是使用 PDF 中的正英寸测量值,并将它们映射到 iText7 提取的文本和坐标,每点英寸的比例为 1/72。

我是从 LocationTextExtractionStrategy 派生的,代码如下:

        private class LocationTextListStrategy : LocationTextExtractionStrategy
        {
            private readonly List<TextRect> _textRects = new List<TextRect>();

            public List<TextRect> TextRects() => _textRects;

            public override void EventOccurred(IEventData data, EventType type)
            {
                if (!type.Equals(EventType.RENDER_TEXT))
                    return;

                var renderInfo = (TextRenderInfo)data;
                var text = renderInfo.GetCharacterRenderInfos();

                foreach (var t in text)
                {
                    if (string.IsNullOrWhiteSpace(t.GetText()))
                        continue;

                    AddTextRect(t);
                }
            }

            private void AddTextRect(TextRenderInfo t)
            {
                var letterStart = t.GetBaseline().GetStartPoint();
                var letterEnd = t.GetAscentLine().GetEndPoint();

                var newTextRect = new TextRect(
                    text: t.GetText(),
                    l: letterStart.Get(0),
                    r: letterEnd.Get(0),
                    t: letterEnd.Get(1),
                    b: letterStart.Get(1));
                
                _textRects.Add(newTextRect);
            }
        }

【问题讨论】:

    标签: c# itext7


    【解决方案1】:

    每个 PDF 页面都可以有自己的自定义坐标系。原点通常位于页面的左下角,但不是必需的。

    Key Type Value
    MediaBox rectangle (Required; inheritable) A rectangle (see 7.9.5, "Rectangles"), expressed in default user space units, that shall define the boundaries of the physical medium on which the page shall be displayed or printed (see 14.11.2, "Page boundaries").
    CropBox rectangle (Optional; Inheritable) A rectangle, expressed in default user space units, that shall define the visible region of default user space. When the page is displayed or printed, its contents shall be clipped (cropped) to this rectangle (see 14.11.2, "Page boundaries"). Default value: the value of MediaBox.

    (ISO 32000-2:2017,表 31 - 页面中的条目)

    因此,始终解释相对于它们所指页面的裁剪框的坐标。

    iText 7 类 PdfPage 具有匹配的 getter。

    【讨论】:

    • 感谢您的回复!我很快就会对此进行调查。
    • 裁剪框、媒体框和页面大小矩形在我的边缘案例文档中似乎都相同。每个{宽度:792,高度:612,x:0,y:0}。您还有其他想法吗?
    • 我必须分析有问题的文档。页面裁剪框外可能只是不可见的文本。
    • 好的。由于 PII 问题,我无法附上问题文档,但感谢您抽出宝贵时间。
    猜你喜欢
    • 2022-10-13
    • 2011-11-16
    • 2011-09-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多