java pdf dpi,使用pdfBox从PDF中提取的图像的DPI

I'm using java pdfBox library to validate single page pdf files with embedded images.

I know that pdf file itself doesen't contain the DPI information.

However the images that have the equal dimensions in the document have different sizes in pixels after extracting and no dpi meta information.

So is it possible to somehow calculate the image sizes relative to pdf page or to extract images with their dpi information (for png or jpeg image files) using pdfBox?

Thanks!

解决方案

Get the PrintImageLocations.java file from the PDFBOX src download. Here's an except of the source, only the last line is by me, and it will output the dpi:

float imageXScale = ctmNew.getXScale();

float imageYScale = ctmNew.getYScale();

System.out.println("position = " + ctmNew.getXPosition() + ", " + ctmNew.getYPosition());

// size in pixel

System.out.println("size = " + imageWidth + "px, " + imageHeight + "px");

// size in page units

System.out.println("size = " + imageXScale + "pu, " + imageYScale + "pu");

// size in inches

imageXScale /= 72;

imageYScale /= 72;

System.out.println("size = " + imageXScale + "in, " + imageYScale + "in");

// size in millimeter

imageXScale *= 25.4;

imageYScale *= 25.4;

System.out.println("size = " + imageXScale + "mm, " + imageYScale + "mm");

System.out.printf("dpi = %.0f dpi (X), %.0f dpi (Y) %n", image.getWidth() * 72 / ctmNew.getXScale(), image.getHeight() * 72 / ctmNew.getYScale());

And here's a sample output:

Found image [X0]

position = 0.0, 0.0

size = 2544px, 3523px

size = 610.56pu, 845.52pu

size = 8.48in, 11.743334in

size = 215.39198mm, 298.28067mm

dpi = 300 dpi (X), 300 dpi (Y)

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值