200字范文,内容丰富有趣,生活中的好帮手!
200字范文 > java 修改pdf文档的页面的大小

java 修改pdf文档的页面的大小

时间:2019-06-29 09:25:52

相关推荐

java 修改pdf文档的页面的大小

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;

}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。