您当前的位置:首页 > 计算机 > 编程开发 > Java

java获取PDF页面长宽

时间:05-15来源:作者:点击数:

java获取PDF页面长宽

1.引入依赖

<dependency>
    <groupId>org.bouncycastle</groupId>
    <artifactId>bcprov-jdk15on</artifactId>
    <version>1.70</version>
</dependency>
<dependency>
    <groupId>com.itextpdf</groupId>
    <artifactId>itextpdf</artifactId>
    <version>5.5.13.2</version>
</dependency>

2.获取长宽并转为cm

String filename = "C:\\Users\\Admin\\Desktop\\发票.pdf";
PdfReader reader = new PdfReader(filename); 
Rectangle pageSize = reader.getPageSize(1);
float height1 = pageSize.getHeight();
float width1 = pageSize.getWidth();
System.out.println("width = "+width1+"pt, height = "+height1 + "pt");

PdfReader pdfreader = new PdfReader(filename);
Document document = new Document(pdfreader.getPageSize(3));
float width2 = document.getPageSize().getWidth();
float height2 = document.getPageSize().getHeight();
System.out.println("widths = "+width2+"pt, heights = "+height2 + "pt");

double d = 0.0352778;
System.out.println("widths = "+ d * width2+ "cm, heights = "+ d *height2 +"cm");
方便获取更多学习、工作、生活信息请关注本站微信公众号城东书院 微信服务号城东书院 微信订阅号
推荐内容
相关内容
栏目更新
栏目热门