如何使用itextpdf获取(以mm为单位)pdf页面的宽度和高度

package getpdfinfo;

import java.io.IOException;

import com.itextpdf.text.Document;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.PdfReader;

public class GetPdfinfo {

	public static void main(String[] args) throws Exception {

		String filename = "F:\\bomei2019\\jianyuanpdf\\012def29b6df42a992701a1ebb781ce4.pdf";
		// TODO Auto-generated method stub
           PdfReader reader = new PdfReader(filename);
           //或者下面的也可以
//           PdfReader reader = new PdfReader(new FileInputStream(filename));
           Rectangle pageSize = reader.getPageSize(1);
           float height = pageSize.getHeight();
           float width = pageSize.getWidth();
           System.out.println("width = "+width+", height = "+height);
           System.out.println("换算后:"+"width = "+ (width*25.4/72) +", height = "+ (height*25.4/72) );
           
//           Document document = new Document();
//           document.getPageSize().getHeight();
//           document.getPageSize().getWidth();
           PdfReader pdfreader = new PdfReader(filename);     
           Document document = new Document(pdfreader.getPageSize(1)); 
//			Document document = new Document(PageSize.A4.rotate(), 0, 0, 30, 0);
			// 获取页面宽度
			float widths = document.getPageSize().getWidth();
			// 获取页面高度
			float heights = document.getPageSize().getHeight();
			System.out.println("widths = "+widths+", heights = "+heights);

	}

}

PDF中的测量单位是points,传统的图形行业测量单位. Adobe使用以下定义:
1 pt = 1/72 inch
并且由于将一英寸定义为正好为25.4毫米,可以使用公式将点数转换为mm
mm = pt*25.4 / 72

你可能感兴趣的:(Java)