gpt4 book ai didi

java - PDFBox 将现有 PDF 的所有内容旋转 90 度

转载 作者:行者123 更新时间:2023-11-29 08:29:14 28 4
gpt4 key购买 nike

如标题所述,我正在尝试将 pdf 页面从横向旋转为纵向,反之亦然,同时保留所有现有格式。

我在堆栈溢出和外部尝试了多种方法/指南,包括阅读现有文档。不幸的是,我仍然没有找到答案。

设置 PDFPage 的旋转很简单,例如 (page.setRotation(90))。或者通过 Matrix 和 Content Stream 设置 Rotation。然而,似乎没有一个能按要求工作。

Iterator < PDDocument > iterator = pages.listIterator();
PDDocument temp = null;
while (iterator.hasNext()) {
temp = iterator.next();

-(..Code goes Here..) -

/** Set Page rotation test 1**/
PDPage page = temp.getDocumentCatalog().getPages().get(0);
page.setRotation(90);

/** Set Page rotation test 2**/
PDPage page = temp.getDocumentCatalog().getPages().get(0);
PDPageContentStream cs = new PDPageContentStream(temp, page, PDPageContentStream.AppendMode.PREPEND, false, false);
Matrix matrix = Matrix.getRotateInstance(Math.toRadians(90), 0, 0);
cs.transform(matrix);
cs.close();

-(..Other tests etc..) -

String filePath = "....filePath"
temp.save(filePath);
temp.close();
}

最佳答案

首先检查页面是否旋转。然后旋转页面。

PDRectangle pageSize = page.getMediaBox();
if ((pageSize.getWidth() <= pageSize.getHeight()) && (page.getRotation() == 90 || page.getRotation() == 270)) {
page.setRotation(0); //Rotate Landscape
} else
page.setRotation(90); //Rotate Portrait
}

关于java - PDFBox 将现有 PDF 的所有内容旋转 90 度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49676233/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com