- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试从 PDF 文件中获取所有单词及其位置坐标。我在 .NET
上成功使用了 Acrobat API .现在,我正在尝试使用免费 API 获得相同的结果,例如 iTextSharp(.NET
版本)。我可以使用 PRTokeniser
获取文本(逐行) ,但我不知道如何获得线的坐标,更不用说每个单词的坐标了。
最佳答案
我的帐户对 Mark Storer 的回答太新了。
我无法直接使用 LocationTextExtracationStrategy (我想我一定是做错了什么)。当我使用 LocationTextExtracationStrategy 时,我能够获取文本,但我无法弄清楚如何获取每个字符串(或字符串行)的坐标。
我最终继承了 LocationTextExtracationStrategy 并公开了我想要的数据,因为它内部确实有它。
我也想在 .net 中使用它...所以这里是我放在一起的一个草率的 C# 版本。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using iTextSharp.text.pdf.parser;
namespace PdfHelper
{
/// <summary>
/// Taken from http://www.java-frameworks.com/java/itext/com/itextpdf/text/pdf/parser/LocationTextExtractionStrategy.java.html
/// </summary>
class LocationTextExtractionStrategyEx : LocationTextExtractionStrategy
{
private List<TextChunk> m_locationResult = new List<TextChunk>();
private List<TextInfo> m_TextLocationInfo = new List<TextInfo>();
public List<TextChunk> LocationResult
{
get { return m_locationResult; }
}
public List<TextInfo> TextLocationInfo
{
get { return m_TextLocationInfo; }
}
/// <summary>
/// Creates a new LocationTextExtracationStrategyEx
/// </summary>
public LocationTextExtractionStrategyEx()
{
}
/// <summary>
/// Returns the result so far
/// </summary>
/// <returns>a String with the resulting text</returns>
public override String GetResultantText()
{
m_locationResult.Sort();
StringBuilder sb = new StringBuilder();
TextChunk lastChunk = null;
TextInfo lastTextInfo = null;
foreach (TextChunk chunk in m_locationResult)
{
if (lastChunk == null)
{
sb.Append(chunk.Text);
lastTextInfo = new TextInfo(chunk);
m_TextLocationInfo.Add(lastTextInfo);
}
else
{
if (chunk.sameLine(lastChunk))
{
float dist = chunk.distanceFromEndOf(lastChunk);
if (dist < -chunk.CharSpaceWidth)
{
sb.Append(' ');
lastTextInfo.addSpace();
}
//append a space if the trailing char of the prev string wasn't a space && the 1st char of the current string isn't a space
else if (dist > chunk.CharSpaceWidth / 2.0f && chunk.Text[0] != ' ' && lastChunk.Text[lastChunk.Text.Length - 1] != ' ')
{
sb.Append(' ');
lastTextInfo.addSpace();
}
sb.Append(chunk.Text);
lastTextInfo.appendText(chunk);
}
else
{
sb.Append('\n');
sb.Append(chunk.Text);
lastTextInfo = new TextInfo(chunk);
m_TextLocationInfo.Add(lastTextInfo);
}
}
lastChunk = chunk;
}
return sb.ToString();
}
/// <summary>
///
/// </summary>
/// <param name="renderInfo"></param>
public override void RenderText(TextRenderInfo renderInfo)
{
LineSegment segment = renderInfo.GetBaseline();
TextChunk location = new TextChunk(renderInfo.GetText(), segment.GetStartPoint(), segment.GetEndPoint(), renderInfo.GetSingleSpaceWidth(), renderInfo.GetAscentLine(), renderInfo.GetDescentLine());
m_locationResult.Add(location);
}
public class TextChunk : IComparable, ICloneable
{
string m_text;
Vector m_startLocation;
Vector m_endLocation;
Vector m_orientationVector;
int m_orientationMagnitude;
int m_distPerpendicular;
float m_distParallelStart;
float m_distParallelEnd;
float m_charSpaceWidth;
public LineSegment AscentLine;
public LineSegment DecentLine;
public object Clone()
{
TextChunk copy = new TextChunk(m_text, m_startLocation, m_endLocation, m_charSpaceWidth, AscentLine, DecentLine);
return copy;
}
public string Text
{
get { return m_text; }
set { m_text = value; }
}
public float CharSpaceWidth
{
get { return m_charSpaceWidth; }
set { m_charSpaceWidth = value; }
}
public Vector StartLocation
{
get { return m_startLocation; }
set { m_startLocation = value; }
}
public Vector EndLocation
{
get { return m_endLocation; }
set { m_endLocation = value; }
}
/// <summary>
/// Represents a chunk of text, it's orientation, and location relative to the orientation vector
/// </summary>
/// <param name="txt"></param>
/// <param name="startLoc"></param>
/// <param name="endLoc"></param>
/// <param name="charSpaceWidth"></param>
public TextChunk(string txt, Vector startLoc, Vector endLoc, float charSpaceWidth, LineSegment ascentLine, LineSegment decentLine)
{
m_text = txt;
m_startLocation = startLoc;
m_endLocation = endLoc;
m_charSpaceWidth = charSpaceWidth;
AscentLine = ascentLine;
DecentLine = decentLine;
m_orientationVector = m_endLocation.Subtract(m_startLocation).Normalize();
m_orientationMagnitude = (int)(Math.Atan2(m_orientationVector[Vector.I2], m_orientationVector[Vector.I1]) * 1000);
// see http://mathworld.wolfram.com/Point-LineDistance2-Dimensional.html
// the two vectors we are crossing are in the same plane, so the result will be purely
// in the z-axis (out of plane) direction, so we just take the I3 component of the result
Vector origin = new Vector(0, 0, 1);
m_distPerpendicular = (int)(m_startLocation.Subtract(origin)).Cross(m_orientationVector)[Vector.I3];
m_distParallelStart = m_orientationVector.Dot(m_startLocation);
m_distParallelEnd = m_orientationVector.Dot(m_endLocation);
}
/// <summary>
/// true if this location is on the the same line as the other text chunk
/// </summary>
/// <param name="textChunkToCompare">the location to compare to</param>
/// <returns>true if this location is on the the same line as the other</returns>
public bool sameLine(TextChunk textChunkToCompare)
{
if (m_orientationMagnitude != textChunkToCompare.m_orientationMagnitude) return false;
if (m_distPerpendicular != textChunkToCompare.m_distPerpendicular) return false;
return true;
}
/// <summary>
/// Computes the distance between the end of 'other' and the beginning of this chunk
/// in the direction of this chunk's orientation vector. Note that it's a bad idea
/// to call this for chunks that aren't on the same line and orientation, but we don't
/// explicitly check for that condition for performance reasons.
/// </summary>
/// <param name="other"></param>
/// <returns>the number of spaces between the end of 'other' and the beginning of this chunk</returns>
public float distanceFromEndOf(TextChunk other)
{
float distance = m_distParallelStart - other.m_distParallelEnd;
return distance;
}
/// <summary>
/// Compares based on orientation, perpendicular distance, then parallel distance
/// </summary>
/// <param name="obj"></param>
/// <returns></returns>
public int CompareTo(object obj)
{
if (obj == null) throw new ArgumentException("Object is now a TextChunk");
TextChunk rhs = obj as TextChunk;
if (rhs != null)
{
if (this == rhs) return 0;
int rslt;
rslt = m_orientationMagnitude - rhs.m_orientationMagnitude;
if (rslt != 0) return rslt;
rslt = m_distPerpendicular - rhs.m_distPerpendicular;
if (rslt != 0) return rslt;
// note: it's never safe to check floating point numbers for equality, and if two chunks
// are truly right on top of each other, which one comes first or second just doesn't matter
// so we arbitrarily choose this way.
rslt = m_distParallelStart < rhs.m_distParallelStart ? -1 : 1;
return rslt;
}
else
{
throw new ArgumentException("Object is now a TextChunk");
}
}
}
public class TextInfo
{
public Vector TopLeft;
public Vector BottomRight;
private string m_Text;
public string Text
{
get { return m_Text; }
}
/// <summary>
/// Create a TextInfo.
/// </summary>
/// <param name="initialTextChunk"></param>
public TextInfo(TextChunk initialTextChunk)
{
TopLeft = initialTextChunk.AscentLine.GetStartPoint();
BottomRight = initialTextChunk.DecentLine.GetEndPoint();
m_Text = initialTextChunk.Text;
}
/// <summary>
/// Add more text to this TextInfo.
/// </summary>
/// <param name="additionalTextChunk"></param>
public void appendText(TextChunk additionalTextChunk)
{
BottomRight = additionalTextChunk.DecentLine.GetEndPoint();
m_Text += additionalTextChunk.Text;
}
/// <summary>
/// Add a space to the TextInfo. This will leave the endpoint out of sync with the text.
/// The assumtion is that you will add more text after the space which will correct the endpoint.
/// </summary>
public void addSpace()
{
m_Text += ' ';
}
}
}
}
关于.net - 使用 itextsharp 从 Pdf 文件中提取文本和文本矩形坐标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4577789/
表架构 DROP TABLE bla; CREATE TABLE bla (id INTEGER, city INTEGER, year_ INTEGER, month_ INTEGER, val I
我需要拆分字符串/或从具有以下结构的字符串中获取更容易的子字符串。 字符串将来自 window.location.pathname 或 window.location.href,看起来像 text/n
每当将对象添加到数组中时,我都会尝试更新 TextView ,并在 TextView 中显示该文本,如下所示: "object 1" "object 2" 问题是,每次将新对象添加到数组时,它都会覆盖
我目前正在寻找使用 Java 读取网站可见文本并将其存储为纯文本字符串的方法。 换句话说,我想转换成这样: Hello stupid World进入“ Hello World ” 或者类似的东西 Un
我正在尝试以文本和 HTML 格式发送电子邮件,但无法正确发送正确的 header 。特别是,我想设置 Content-Type header ,但我找不到如何为 html 和文本部分单独设置它。 这
我尝试了上面的代码,但我无法绑定(bind)文本,我怎样才能将资源内部文本 bloc
我刚刚完成了 Space Shooter 教程,由于没有 GUIText 对象,所以我创建了 UI.Text 对象并进行了相应的编码。它在统一播放器中有效,但在构建 Web 应用程序后无效。我花了一段
我有这个代码: - (IBAction)setButtonPressed:(id)sender { NSUserDefaults *sharedDefaults = [[NSUserDefau
抱歉标题含糊不清,但我想不出我想在标题中做什么。无论如何,对于图像上的文本,我使用了 JLabel 文本并将其添加到图标中。 JLabel icon = new JLabel(new Imag
关闭。这个问题是not reproducible or was caused by typos .它目前不接受答案。 这个问题是由于错别字或无法再重现的问题引起的。虽然类似的问题可能是on-topi
我在将 Twitter 嵌入到我从 HTML 5 转换的 wordpress 运行网站时遇到问题。 我遇到的问题是推文不是我的自定义字体... 这是我无法使用任何 css 定位的 HTML 代码,我正
我正在尝试找到解决由于使用以下形式的代码而导致的冗余字符串连接问题的最佳方法: logger.debug("Entering loop, arg is: " + arg) // @1 在大多数情况下,
我写了这个测试 @Test public void removeRequestTextFromRouteError() throws Exception { String input = "F
我目前正在创建一个正则表达式来拆分所有匹配以下格式的字符串:&[文本],并且需要获取文本。字符串可能类似于:something &[text] &[text] everything &[text] 等
有没有办法将标题文本从一个词变形为另一个词,同时保留两个词中使用的字母?我看过的许多 css 文本动画大多是视觉的,很少有旋转整个单词的。 我想要做的是从一个词过渡,例如“BEACH”到“CHANGE
总结matplotlib绘图如何设置坐标轴刻度大小和刻度。 上代码: ?
我在容器 (1) 中创建了容器 (2)。你能帮忙如何向容器(1)添加文本吗?下面是我的代码 return Scaffold( body: Padding( padding: c
我似乎找不到任何人或任何人这样做过。我试图限制我们使用的图像数量,并想创建一个带有渐变作为其“颜色”的文本,并在其周围设置渐变轮廓/描边 到目前为止,我还没有看到任何将两者结合在一起的东西。 我可以自
我正在为视频游戏暗黑破坏神 2 使用 discord.py 构建一个不和谐机器人。其中一项功能要求机器人从暗黑破坏神 2 屏幕截图中提取项目的名称和属性。我目前正在为此使用 pytesseract,但
我很难弄清楚如何旋转 strip.text theme 中的属性来自 ggplot2 .我使用的是 R 版本 3.4.2 和 ggplot2 版本 2.2.1。 以下是 MWE 的数据。 > dput
我是一名优秀的程序员,十分优秀!