【发布时间】:2014-09-13 07:08:26
【问题描述】:
我正在寻找实现类似于
输出的安静的 java 解决方案的最简单方法pdftotext -layout FILE
在 linux 机器上。 (当然它也应该很便宜)
我刚刚尝试了一些 IText、PDFBox 和 PDFTextStream 的代码 sn-ps。迄今为止最准确的解决方案是 PDFTextStream,它使用 VisualOutputTarget 来获得我的文件的良好表示。
所以我的列布局被认为是正确的,我可以使用它。 但是IText也应该有解决方案,或者?
我发现的每一个简单的 sn-p 都会生成简单的有序字符串,这些字符串是一团糟(混乱的行/列/行)。是否有任何可能更容易并且可能不涉及自己的策略的解决方案?或者有没有我可以使用的开源策略?
// 我按照 mkl 的说明编写并拥有如下策略对象:
package com.test.pdfextractiontest.itext;
import ...
public class MyLocationTextExtractionStrategy implements TextExtractionStrategy {
/** set to true for debugging */
static boolean DUMP_STATE = false;
/** a summary of all found text */
private final List<TextChunk> locationalResult = new ArrayList<TextChunk>();
public MyLocationTextExtractionStrategy() {
}
@Override
public void beginTextBlock() {
}
@Override
public void endTextBlock() {
}
private boolean startsWithSpace(final String str) {
if (str.length() == 0) {
return false;
}
return str.charAt(0) == ' ';
}
private boolean endsWithSpace(final String str) {
if (str.length() == 0) {
return false;
}
return str.charAt(str.length() - 1) == ' ';
}
private List<TextChunk> filterTextChunks(final List<TextChunk> textChunks, final TextChunkFilter filter) {
if (filter == null) {
return textChunks;
}
final List<TextChunk> filtered = new ArrayList<TextChunk>();
for (final TextChunk textChunk : textChunks) {
if (filter.accept(textChunk)) {
filtered.add(textChunk);
}
}
return filtered;
}
protected boolean isChunkAtWordBoundary(final TextChunk chunk, final TextChunk previousChunk) {
final float dist = chunk.distanceFromEndOf(previousChunk);
if (dist < -chunk.getCharSpaceWidth() || dist > chunk.getCharSpaceWidth() / 2.0f) {
return true;
}
return false;
}
public String getResultantText(final TextChunkFilter chunkFilter) {
if (DUMP_STATE) {
dumpState();
}
final List<TextChunk> filteredTextChunks = filterTextChunks(this.locationalResult, chunkFilter);
Collections.sort(filteredTextChunks);
final StringBuffer sb = new StringBuffer();
TextChunk lastChunk = null;
for (final TextChunk chunk : filteredTextChunks) {
if (lastChunk == null) {
sb.append(chunk.text);
} else {
if (chunk.sameLine(lastChunk)) {
if (isChunkAtWordBoundary(chunk, lastChunk) && !startsWithSpace(chunk.text)
&& !endsWithSpace(lastChunk.text)) {
sb.append(' ');
}
final Float dist = chunk.distanceFromEndOf(lastChunk)/3;
for(int i = 0; i<Math.round(dist); i++) {
sb.append(' ');
}
sb.append(chunk.text);
} else {
sb.append('\n');
sb.append(chunk.text);
}
}
lastChunk = chunk;
}
return sb.toString();
}
返回一个带有结果文本的字符串。 */ @覆盖 公共字符串 getResultantText() {
return getResultantText(null);
}
private void dumpState() {
for (final TextChunk location : this.locationalResult) {
location.printDiagnostics();
System.out.println();
}
}
@Override
public void renderText(final TextRenderInfo renderInfo) {
LineSegment segment = renderInfo.getBaseline();
if (renderInfo.getRise() != 0) {
final Matrix riseOffsetTransform = new Matrix(0, -renderInfo.getRise());
segment = segment.transformBy(riseOffsetTransform);
}
final TextChunk location =
new TextChunk(renderInfo.getText(), segment.getStartPoint(), segment.getEndPoint(),
renderInfo.getSingleSpaceWidth(),renderInfo);
this.locationalResult.add(location);
}
public static class TextChunk implements Comparable<TextChunk> {
/** the text of the chunk */
private final String text;
/** the starting location of the chunk */
private final Vector startLocation;
/** the ending location of the chunk */
private final Vector endLocation;
/** unit vector in the orientation of the chunk */
private final Vector orientationVector;
/** the orientation as a scalar for quick sorting */
private final int orientationMagnitude;
private final TextRenderInfo info;
private final int distPerpendicular;
private final float distParallelStart;
private final float distParallelEnd;
/** the width of a single space character in the font of the chunk */
private final float charSpaceWidth;
public TextChunk(final String string, final Vector startLocation, final Vector endLocation,
final float charSpaceWidth,final TextRenderInfo ri) {
this.text = string;
this.startLocation = startLocation;
this.endLocation = endLocation;
this.charSpaceWidth = charSpaceWidth;
this.info = ri;
Vector oVector = endLocation.subtract(startLocation);
if (oVector.length() == 0) {
oVector = new Vector(1, 0, 0);
}
this.orientationVector = oVector.normalize();
this.orientationMagnitude =
(int) (Math.atan2(this.orientationVector.get(Vector.I2), this.orientationVector.get(Vector.I1)) * 1000);
final Vector origin = new Vector(0, 0, 1);
this.distPerpendicular = (int) startLocation.subtract(origin).cross(this.orientationVector).get(Vector.I3);
this.distParallelStart = this.orientationVector.dot(startLocation);
this.distParallelEnd = this.orientationVector.dot(endLocation);
}
public Vector getStartLocation() {
return this.startLocation;
}
public Vector getEndLocation() {
return this.endLocation;
}
public String getText() {
return this.text;
}
public float getCharSpaceWidth() {
return this.charSpaceWidth;
}
private void printDiagnostics() {
System.out.println("Text (@" + this.startLocation + " -> " + this.endLocation + "): " + this.text);
System.out.println("orientationMagnitude: " + this.orientationMagnitude);
System.out.println("distPerpendicular: " + this.distPerpendicular);
System.out.println("distParallel: " + this.distParallelStart);
}
public boolean sameLine(final TextChunk as) {
if (this.orientationMagnitude != as.orientationMagnitude) {
return false;
}
if (this.distPerpendicular != as.distPerpendicular) {
return false;
}
return true;
}
public float distanceFromEndOf(final TextChunk other) {
final float distance = this.distParallelStart - other.distParallelEnd;
return distance;
}
public float myDistanceFromEndOf(final TextChunk other) {
final float distance = this.distParallelStart - other.distParallelEnd;
return distance;
}
@Override
public int compareTo(final TextChunk rhs) {
if (this == rhs) {
return 0; // not really needed, but just in case
}
int rslt;
rslt = compareInts(this.orientationMagnitude, rhs.orientationMagnitude);
if (rslt != 0) {
return rslt;
}
rslt = compareInts(this.distPerpendicular, rhs.distPerpendicular);
if (rslt != 0) {
return rslt;
}
return Float.compare(this.distParallelStart, rhs.distParallelStart);
}
private static int compareInts(final int int1, final int int2) {
return int1 == int2 ? 0 : int1 < int2 ? -1 : 1;
}
public TextRenderInfo getInfo() {
return this.info;
}
}
@Override
public void renderImage(final ImageRenderInfo renderInfo) {
// do nothing
}
public static interface TextChunkFilter {
public boolean accept(TextChunk textChunk);
}
}
如您所见,大部分与原始类相同。我刚刚添加了这个:
final Float dist = chunk.distanceFromEndOf(lastChunk)/3;
for(int i = 0; i<Math.round(dist); i++) {
sb.append(' ');
}
到 getResultantText 方法以用空格扩展间隙。 但问题来了:
距离似乎不准确或不准确。结果看起来像
这个:
有没有人知道如何计算更好的距离或价值?我认为这是因为原始字体类型是 ArialMT 并且我的编辑器在 courier 中,但是要使用此表,建议我可以在正确的位置拆分表格以获取我的数据。由于值 usw 的浮动开始和结束,这很困难。
:-/
【问题讨论】:
-
这应该很容易通过复制
LocationTextExtractionStrategy并稍微更改其方法getResultantText(TextChunkFilter)来实现。不幸的是,必要的数据在该类中是私有的。因此,从它派生是行不通的(也就是说,没有反射)。 -
mhh 我刚刚尝试了标准的 LocationTextExtractionStrategy 并且也很乱在线订购。 :D
-
一切都颠倒了 - 令人惊讶的是,它明确地命令。或者你使用一些 RTL 写作? 在错误的地方充满了空格 - 我认为空格太少无法代表原始格式,但太多了吗?你能分享有问题的PDF文件吗?
-
很遗憾没有。一些分类数据;)但我现在让它正常工作了,......它只是我们 Sonatype 关系中的旧版本的 IText。但是现在如果文本块在同一行并且缩进,我如何在文本块之间获得更多空间?
-
如前所述,您必须编辑
LocationTextExtractionStrategy的副本,更准确地说是它的方法getResultantText(TextChunkFilter),不仅要在同一行上为块插入一个空格,并用空格分隔;相反,空格的数量需要填补足够大的空白。