我正在尝试使用iText7从PDF提取文本。我正在使用IEventListener来获取页面的所有部分,尽管某些文本已旋转。我可以找到有关如何将旋转后的文本插入PDF的示例,但是找不到有关如何分辨给定文本段是否旋转的任何信息。
有人可以帮忙吗?
public void EventOccurred(IEventData data, EventType type)
{
PdfPart part = null;
switch (type)
{
case EventType.BEGIN_TEXT:
break;
case EventType.RENDER_TEXT:
part = new PdfTextPart(PageNumber, data as TextRenderInfo);
Parts.Add(part);
break;
case EventType.END_TEXT:
break;
case EventType.RENDER_IMAGE:
var imageData = data as ImageRenderInfo;
//this.HandleImage(imageData);
break;
case EventType.RENDER_PATH:
part = new PdfLinePart(PageNumber, data as PathRenderInfo);
Parts.Add(part);
break;
case EventType.CLIP_PATH_CHANGED:
break;
default:
break;
}
}
public PdfTextPart(Int32 pageNumber, TextRenderInfo info) : base(pageNumber)
{
Text = info.GetText();
var font = info.GetFont().GetFontProgram().GetFontNames();
Font = font.GetFontName();
if (font.IsItalic()) { this.IsItalic = true; }
if (font.IsBold()) { this.IsBold = true; }
if (font.IsUnderline()) { this.IsUnderline = true; }
}
TextRenderInfo
有一条基线。该基线是a LineSegment
,因此具有起点和终点。现在,您只需要确定这两个点之间的线的角度即可。
即为TextRenderInfo info
:
LineSegment baseline = info.GetBaseline();
Vector startPoint = baseline.GetStartPoint();
Vector endPoint = baseline.GetEndPoint();
Vector direction = endLocation.Subtract(startLocation);
double angle = Math.Atan2(direction.Get(Vector.I2), direction.Get(Vector.I1));
结果显然在弧度上。
您可能还必须考虑页面旋转(如果我没记错的话),该页面旋转没有计算到上述坐标中。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句