itextPdf获取pdf文件宽高不准确

正常情况下我们通过下面方式获取宽高

java 复制代码
PdfReader reader = new PdfReader(file.getPath());
float width = reader.getPageSize(1).getWidth();
float height = reader.getPageSize(1).getHeight();

但是这样获取的宽高是不准确的,永远都是 宽 > 高,也就是横向。所以实际上是需要使用旋转角度来进行判断

java 复制代码
PdfReader reader = new PdfReader(file.getPath());
int rotation = reader.getPageRotation(1);
float width = 0f;
float height = 0f;
if (rotation == 270 || rotation == 90) {
    width = reader.getPageSize(1).getHeight();
    height = reader.getPageSize(1).getWidth();
}else{
    width = reader.getPageSize(1).getWidth();
    height = reader.getPageSize(1).getHeight();
}

当旋转为横向的时候,我们就把宽高倒置即可。

相关推荐
vx-程序开发2 分钟前
【毕设分享】05253基于Spring+Boot的作业提交与管理系统的设计与实现
java·spring boot·后端·spring·源码·课程设计·大作业
nice_lcj5202 小时前
Spring 核心串讲:把 IoC、AOP、Boot、事务连成体系
java·spring boot·spring·面试·ioc·aop·体系串讲
nice_lcj5203 小时前
Spring Boot 入门:几行配置怎么跑起来
java·spring boot·spring·面试·自动配置·起步依赖
泡海椒3 小时前
quick-pdf 饼图(JChartType.PIE)实战:五个客户层级五块扇区的配色与标签
pdf
CoderYanger5 小时前
Java EE 进阶:14.案例综合练习-博客系统(第三弹) :用户登录+强制登录
java·服务器·开发语言·数据库·spring boot·java-ee·mybatis
CoderYanger11 小时前
Java EE 进阶:13. Spring事务和事务传播机制(第二弹)
java·开发语言·数据库·spring boot·后端·spring·java-ee
行者-全栈开发15 小时前
SpringBoot 熔断降级实战:Resilience4j 与 Sentinel 双方案深度对比与落地
spring boot·后端·sentinel
头发还在的女程序员1 天前
家政小程序系统源码核销提交事务:多端操作怎样只确认一次服务结果
java·spring boot·源码·家政·核销事务
计算机–阿拉升学长1 天前
基于SpringBoot的番茄种植水肥一体化管理系统 — 系统设计与实现
java·spring boot·后端·mysql·vue·javaweb
计算机毕设定制辅导-无忧学长1 天前
《基于SpringBoot的肩周炎健康管理系统设计与实现》
java·vue.js·spring boot·mysql·肩周炎健康管理系统