pdf解析框架有 pdfrenderer、pdfbox、icepdf等开源组件。
目前对比发现icepdf最为强大,能够解析绝大多数的pdf文件。
不过今天发现icepdf存在一些不足之处,有的pdf文件解析出现页面空白。
经过定位发现两处bug导致处理pdf页面空白。
http://jira.icesoft.org/browse/PDF-396
1.问题一
ContentParser.java中parseText方法存在异常
...
/**
* Tranformation matrix
* tm = |f1 f2 0|
* |f3 f4 0|
* |f5 f6 0|
*/
else if (nextToken.equals(PdfOps.Tm_TOKEN)) {
// collectTokenFrequency(PdfOps.Tm_TOKEN);
shift = 0;
previousAdvance = 0;
advance.setLocation(0, 0);
float f6 = ((Number) stack.pop()).floatValue();
float f5 = ((Number) stack.pop()).floatValue();
float f4 = ((Number) stack.pop()).floatValue();
float f3 = ((Number) stack.pop()).floatValue();
float f2 = ((Number) stack.pop()).floatValue();
float f1 = ((Number) stack.pop()).floatValue();
stack.pop的时候如果栈中没有数据了会报EmptyStackException,导致循环退出,整页显示为空白。
修改如下,
float f6 = stack.isEmpty()? 0 : ((Number) stack.pop()).floatValue();
float f5 = stack.isEmpty()? 0 : ((Number) stack.pop()).floatValue();
float f4 = stack.isEmpty()? 0 : ((Number) stack.pop()).floatValue();
float f3 = stack.isEmpty()? 0 : ((Number) stack.pop()).floatValue();
float f2 = stack.isEmpty()? 0 : ((Number) stack.pop()).floatValue();
float f1 = stack.isEmpty()? 0 : ((Number) stack.pop()).floatValue();
增加stack为空的判断,并且给默认值0.
问题2:ChunkingInputStream 解压缩存在未捕获异常,导致整页pdf文件显示空白。
在解压缩格式不正确的情况下,增加异常捕获能够最大程度的将内容显示出来。不至于整页空白。
修改如下:
ChunkingInputStream.java
protected int fillBufferFromInputStream(int offset, int length) throws IOException {
int read = 0;
while (read < length) {
int currRead = 0;
try{
currRead = in.read(buffer, offset + read, length - read);
if (currRead < 0 && read == 0)
return currRead;
if (currRead <= 0)
break;
read += currRead;
}
//增加异常捕获
catch (ZipException ze)
{
ze.printStackTrace();
return -1;
}
}
return read;
}
icepdf 解析pdf文件空白页bug修改。
猜你喜欢
转载自wuwen-java.iteye.com/blog/1531148
今日推荐
周排行