maven包 网上找
<dependency> <groupId>e-iceblue</groupId> <artifactId>spire.office.free</artifactId> <version>3.9.1</version> <scope>system</scope> <systemPath>${project.basedir}/src/main/resources/lib/spire.office.free-3.9.0.jar</systemPath> </dependency>
代码
/**
* java修改pdf文档页面的宽高
* 参数 inPath 修改后的新的绝对路径
* 参数 outPath pdf的路径
*/
public static String amendpage(String outPath, String inPath) {
//创建PdfDocument对象
PdfDocument originalDoc = new PdfDocument();
//加载PDF文件
originalDoc.loadFromFile(outPath);
//创建一个新的PdfDocument实例
PdfDocument newDoc = new PdfDocument();
//遍历所有PDF 页面
Dimension2D dimension2D = new Dimension();
for (int i = 0; i < originalDoc.getPages().getCount(); i++) {
PdfPageBase page = originalDoc.getPages().get(i);
PdfMargins margins = new PdfMargins(0, 0, 0, 0);
//设置新文档的页面大小为A4
PdfPageBase newPage = newDoc.getPages().add(PdfPageSize.Arch_A, margins);
//调整画布,设置内容也根据页面的大小进行缩放
double wScale = (PdfPageSize.Arch_A.getWidth()) / PdfPageSize.Arch_A.getWidth();
double hScale = (PdfPageSize.Arch_A.getHeight()) / PdfPageSize.Arch_A.getHeight();
newPage.getCanvas().translateTransform(wScale, hScale);
//复制原文档的内容到新文档
newPage.getCanvas().drawTemplate(page.createTemplate(), new Point2D.Float());
//保存PDF
newDoc.saveToFile(outPath);
}
newDoc.close();
return outPath;
}