.net - GetDescentLine/GetAsecentLine 在 itextsharp 和 itext7 上产生不同的值
问题描述
我正在尝试将一些代码从itextsharp 移植到itext7。我在 itextsharp 中使用了自定义位置策略。我将代码移植到 itext7(IEventListener) 。令我惊讶的是,我发现GetDescentLine和GetAscentLine返回的底部和顶部范围在两个版本中是不同的。我在 Github 上提供了一个示例 Visual Studio 解决方案和一个示例 PDF。这是一个障碍,因为我们需要迁移到 .NET Standard/Core 并且 itextsharp 在 Core 上不受官方支持。
有什么建议么?
观察 - 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
解决方案
推荐阅读
- python - 如何使用matplotlib并行绘制具有两种颜色的线
- html - 创建一个带有居中环绕文本的菱形形状
- wordpress - 在 WordPress 上创建 Meta-Box,添加 editTable.js 并保存字段数据
- docker - 仍在执行“RUN apt-get update && apt-get install -y apache2”
- java - 使用 SpEL 在 @Query 中获取活动配置文件
- python - 无法使用 XGBoost 模型(版本 1.5.0)运行 docker 映像
- javascript - 没有重载匹配此调用。最后一个重载在打字稿中给出了以下错误
- python - pytorch 无法使用 gpu
- node.js - TypeError:无法读取未定义的nodejs请求http的属性'0'
- r - 将字符串 2000 年 1 月 1 日上午 12:00 转换为 r 中的日期时间