我已编码创建html页面包含图像提取pdf文档中的页面 .
我试图从pdf中提取图像然后我成功地从pdf中提取图像并使用PDFBox lib将图像应用到html页面 . 但是我没有在html页面中提取图像坐标 .
所以搜索了如何在pdf中提取图像坐标,我尝试使用PDFBox库提取pdf中的图像坐标 .
下面的代码:
public static void main(String[] args) throws Exception
{
try
{
PDDocument document = PDDocument.load(
"/Users/tmdtjq/Downloads/PDFTest/test.pdf" );
PrintImageLocations printer = new PrintImageLocations();
List allPages = document.getDocumentCatalog().getAllPages();
for( int i=0; i<allPages.size(); i++ )
{
PDPage page = (PDPage)allPages.get( i );
int pageNum = i+1;
System.out.println( "Processing page: " + pageNum );
printer.processStream( page, page.findResources(),
page.getContents().getStream() );
}
}
finally
{
}
}
protected void processOperator( PDFOperator operator, List arguments ) throws IOException
{
String operation = operator.getOperation();
if( operation.equals( "Do" ) )
{
COSName objectName = (COSName)arguments.get( 0 );
Map xobjects = getResources().getXObjects();
PDXObject xobject = xobjects.get( objectName.getName() );
if( xobject instanceof PDXObjectImage )
{
try
{
PDXObjectImage image = (PDXObjectImage)xobject;
PDPage page = getCurrentPage();
Matrix ctm = getGraphicsState().getCurrentTransformationMatrix();
double rotationInRadians =(page.findRotation() * Math.PI)/180;
AffineTransform rotation = new AffineTransform();
rotation.setToRotation( rotationInRadians );
AffineTransform rotationInverse = rotation.createInverse();
Matrix rotationInverseMatrix = new Matrix();
rotationInverseMatrix.setFromAffineTransform( rotationInverse );
Matrix rotationMatrix = new Matrix();
rotationMatrix.setFromAffineTransform( rotation );
Matrix unrotatedCTM = ctm.multiply( rotationInverseMatrix );
float xScale = unrotatedCTM.getXScale();
float yScale = unrotatedCTM.getYScale();
float xPosition = unrotatedCTM.getXPosition();
float yPosition = unrotatedCTM.getYPosition();
System.out.println( "Found image[" + objectName.getName() + "] " +
"at " + xPosition + "," + yPosition +
" size=" + (xScale/100f*image.getWidth()) + "," + (yScale/100f*image.getHeight() ));
}
catch( NoninvertibleTransformException e )
{
throw new WrappedIOException( e );
}
}
}
}
输出打印X,Y图像中的位置为全0.0,0.0 .
我认为因为getGraphicsState()是返回graphicsState的方法 .
但我希望将特定的图像坐标应用于PDF页面的高度,宽度以便创建html页面 .
我想也许是从PDF中的图像坐标中提取JSON的解决方案 .
请将PDF中的图像坐标引入JSON工具或建议PDF库 .
(我已经在FlexPaper中使用了pdf2json工具 . 这个工具从PDF页面中提取JSON文件,不包括图像数据,只提取文本数据(内容,坐标,字体..) . )
1 回答
我能够找到搜索
cm
运算符的图像 . 我用以下方式覆盖PDFTextStripper
:注意:它没有考虑旋转和镜像!当然,如果一个人不想与图像一起查找文本,可以使用
PDFStreamEngine
而不是PDFTextStripper
.