JAVA把pdf根据坐标_如何将pdf中的图像坐标转换为JSONfile?

我已编码创建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

{

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文件,不包括图像数据,只提取文本数据(内容,坐标,字体..) . )

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值