首页 > 解决方案 > GetDescentLine/GetAsecentLine 在 itextsharp 和 itext7 上产生不同的值

问题描述

我正在尝试将一些代码从itextsharp 移植itext7。我在 itextsharp 中使用了自定义位置策略。我将代码移植到 itext7(IEventListener) 。令我惊讶的是,我发现GetDescentLineGetAscentLine返回的底部和顶部范围在两个版本中是不同的。我在 Github 上提供了一个示例 Visual Studio 解决方案和一个示例 PDF。这是一个障碍,因为我们需要迁移到 .NET Standard/Core 并且 itextsharp 在 Core 上不受官方支持。

有什么建议么?

观察 - 1 在此处输入图像描述

观察 - 2

比较两个连续句子之间的垂直间隙,使用上句的底部和下句顶部。我发现itextSharp产生了积极的差距。在同一个文档上使用itext7,我得到一个负差距。负差距值意味着什么?它会暗示两个句子之间的物理重叠,但在视觉上我没有看到两个句子相互接触。 在此处输入图像描述

代码片段 - itextsharp

public void RenderText_inner(TextRenderInfo renderInfo)
{
    var bottomLeft = renderInfo.GetDescentLine().GetStartPoint();
    var topRight = renderInfo.GetAscentLine().GetEndPoint();
    double x1 = bottomLeft[Vector.I1];
    double x2 = topRight[Vector.I1];
    double y1 = bottomLeft[Vector.I2];
    double y2 = topRight[Vector.I2];
    var tb = new TextBlock
    {
        Text= renderInfo.GetText(),
        Bottom=y1,
        Top=y2,
        Left=x1,
        Right=x2
    };
    Blocks.Add(tb);
}

代码片段 - itext7

    using (var stm = new System.IO.MemoryStream(contents))
    {
        using (var pdfReader = new iText.Kernel.Pdf.PdfReader(stm))
        {
            using (iText.Kernel.Pdf.PdfDocument doc = new iText.Kernel.Pdf.PdfDocument(pdfReader))
            {
                int numOfPages = doc.GetNumberOfPages();
                for (int page = 1; page <= numOfPages; page++)
                {
                    var pdfPage = doc.GetPage(page);
                    var pg = new Page();
                    var rotation = pdfPage.GetPageSizeWithRotation();
                    pg.Height = rotation.GetHeight();
                    pg.Width = rotation.GetWidth();
                    var customListener = new CustomEventListener();
                    var parser = new PdfCanvasProcessor(customListener);
                    parser.ProcessPageContent(pdfPage);
                    var lstBlocks = customListener.Blocks;
                    pg.Blocks = customListener.Blocks.ToArray();
                    lstPages.Add(pg);
                }
            }
        }
    }

private void RenderText_inner(TextRenderInfo textinfo)
{
    string text = textinfo.GetText();
    var bottomLeft = textinfo.GetDescentLine().GetStartPoint();
    var topRight = textinfo.GetAscentLine().GetEndPoint();
    double x1 = bottomLeft.Get(Vector.I1);
    double x2 = topRight.Get(Vector.I1);
    double y1 = bottomLeft.Get(Vector.I2);
    double y2 = topRight.Get(Vector.I2);
    double fontht = textinfo.GetFontSize();
    var tb = new TextBlock
    {
        Text = textinfo.GetText(),
        Bottom = y1,
        Top = y2,
        Left = x1,
        Right = x2
    };
    Blocks.Add(tb);

Github

完整示例位于此处 https://github.com/sdg002/itext.Demo 使用 VS 2017 编写

观察 1
的单元测试方法 单元测试方法Fire_Both_itext5_And_itext7将调用在 itextsharp 和 itext7 上编写的类库。您可以在已读取的第一个字符中看到异常。

观察 2
的单元测试方法 单元测试方法Fire_Both_itext5_And_itext7_Compare_2ConsecutiveLines将使用两个版本计算垂直间隙。

样本文件

https://github.com/sdg002/itext.Demo

UnitTestProject/Data/CS15.page6.pdf

更新
添加了观察 2

标签: .netitextitext7

解决方案


推荐阅读