如何在 Java 中旋转 PDF 文件
在Java中以编程方式管理PDF对于生成按需报告、发票或账单至关重要。 旋转PDF页面以修复视角问题也很有价值。 这两项任务在Java中都可能具有挑战性。 本文将使用IronPDF Java库简化PDF页面旋转。
IronPDF Java库
IronPDF for Java帮助Java开发人员创建、编辑和操作PDF文档。 该库允许开发人员处理PDF文档布局和格式的几乎每个方面,例如一个或多个页面的当前旋转。
除了创建和操作PDF,IronPDF在将HTML文件转换为像素完美的PDF方面也非常有效。 IronPDF 可在不丢失任何格式的情况下渲染所有图像和文本。 表单组件在PDF文件中受支持。
IronPDF's JAR file can be downloaded and installed from Maven Central or from the product website directly.
使用Java旋转文档的步骤
前提条件
要创建一个可以旋转页面的PDF应用程序,您需要在计算机上下载并安装以下前提条件:
- JDK(Java开发工具包): 在您的计算机上安装最新版本的JDK以编译和运行PDF旋转应用程序。 JDK可以从官方网站下载。
- Maven: 需要安装Maven,因为它是一个主要用于Java项目的构建自动化工具。 Maven可以从Apache Maven网站下载。
IronPDF Java库: 现在您需要最新版本的IronPDF for Java库并应将其添加为依赖项。 将以下IronPDF Java依赖项添加到您项目的
pom.xml文件中:<dependency> <groupId>com.ironsoftware</groupId> <artifactId>ironpdf-jdk8</artifactId> <version>2021.9.3663</version> </dependency><dependency> <groupId>com.ironsoftware</groupId> <artifactId>ironpdf-jdk8</artifactId> <version>2021.9.3663</version> </dependency>XML您还需要在
pom.xml文件中添加Slf4j依赖项。<dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-simple</artifactId> <version>2.0.5</version> </dependency><dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-simple</artifactId> <version>2.0.5</version> </dependency>XML
一旦您下载并安装了所有的前提条件,您就可以在Java应用程序中使用项目进行页面方向任务。
添加必要的导入和许可证密钥
首先,将以下导入语句添加到主Java源文件的顶部:
import com.ironsoftware.ironpdf.PdfDocument;
import com.ironsoftware.ironpdf.edit.PageSelection;
import com.ironsoftware.ironpdf.page.PageRotation;
import com.ironsoftware.ironpdf.render.*;
import java.io.IOException;
import java.nio.file.*;import com.ironsoftware.ironpdf.PdfDocument;
import com.ironsoftware.ironpdf.edit.PageSelection;
import com.ironsoftware.ironpdf.page.PageRotation;
import com.ironsoftware.ironpdf.render.*;
import java.io.IOException;
import java.nio.file.*;接下来,在main方法中,调用License.setLicenseKey设置您购买时获得的有效产品许可证密钥(如果没有许可证密钥则跳过此步骤,或注册试用许可证密钥)。
License.setLicenseKey("Your license key");License.setLicenseKey("Your license key");以纵向或横向渲染PDF
IronPDF可以以纵向和横向旋转页面。
// Create render options with landscape orientation
ChromePdfRenderOptions renderOptions = new ChromePdfRenderOptions();
renderOptions.setPaperOrientation(PaperOrientation.LANDSCAPE);
// Render the URL as a PDF document
PdfDocument newPdf = PdfDocument.renderUrlAsPdf("https://ironpdf.com", renderOptions);
// Save the document to the specified path
newPdf.saveAs(Paths.get("assets/LandscapePdf.pdf"));// Create render options with landscape orientation
ChromePdfRenderOptions renderOptions = new ChromePdfRenderOptions();
renderOptions.setPaperOrientation(PaperOrientation.LANDSCAPE);
// Render the URL as a PDF document
PdfDocument newPdf = PdfDocument.renderUrlAsPdf("https://ironpdf.com", renderOptions);
// Save the document to the specified path
newPdf.saveAs(Paths.get("assets/LandscapePdf.pdf"));IronPDF默认使用纵向方向。 然而,开发人员可以在将内容从HTML,RTF,URL等转换为PDF文档时,使用[ChromePdfRenderOptions](/java/object-reference/api/com/Iron Software/ironpdf/render/ChromePdfRenderOptions.html)对象覆盖此方向。 setPaperOrientation方法接受PaperOrientation值作为参数,允许您根据需要更改生成PDF的纸张方向。
在上述代码中,PaperOrientation被设置为LANDSCAPE。 [PdfDocument](/java/object-reference/api/com/Iron Software/ironpdf/PdfDocument.html)类用于使用renderUrlAsPdf方法将URL转换为PDF文档,使用renderOptions作为第二个参数。
最后,使用saveAs方法将文档保存在指定目录中。
输出 PDF 文件
通过旋转角度旋转页面
对于现有文档,不能使用ChromePdfRenderOptions对象更改页面方向。 对于这些现有的PDF文档,页面方向只能通过基于旋转的转换进行调整。
// Load an existing PDF document from the specified path
PdfDocument existingPdf = PdfDocument.fromFile(Paths.get("assets/LandscapePdf.pdf"));
// Rotate the first page of the document 90 degrees clockwise
existingPdf.rotatePage(PageRotation.CLOCKWISE_90, PageSelection.firstPage());
// Rotate all pages of the document 270 degrees clockwise
existingPdf.rotateAllPages(PageRotation.CLOCKWISE_270);
// Save the modified document to the specified path
existingPdf.saveAs(Paths.get("assets/ExistingPdfRotated.pdf"));// Load an existing PDF document from the specified path
PdfDocument existingPdf = PdfDocument.fromFile(Paths.get("assets/LandscapePdf.pdf"));
// Rotate the first page of the document 90 degrees clockwise
existingPdf.rotatePage(PageRotation.CLOCKWISE_90, PageSelection.firstPage());
// Rotate all pages of the document 270 degrees clockwise
existingPdf.rotateAllPages(PageRotation.CLOCKWISE_270);
// Save the modified document to the specified path
existingPdf.saveAs(Paths.get("assets/ExistingPdfRotated.pdf"));上面的代码修改了前一节中创建的PDF文档。 它先前生成了整个文档为横向,但在这里,IronPDF的rotatePage仅将现有文档的firstPage旋转90度顺时针(使用CLOCKWISE_90)。 随后,rotateAllPages将每一页(包括第一页)旋转CLOCKWISE_270。
旋转后的PDF输出
IronPDF for Java
摘要
本文将演示如何创建一个横向方向的新文档。
IronPDF还为开发人员提供将PDF文档渲染为图像的方法,并从PDF中提取文本和内容。 此外,IronPDF还能够在PDF中渲染图表,通过密码增强安全性,甚至以编程方式处理数字签名。
IronPDF for Java是免费使用的,但用于部署目的,需要商业许可证,起价仅为$799。 您也可以访问IronPDF的完整版的免费试用,以在生产模式下测试其功能。
常见问题解答
如何在Java中旋转PDF页面?
要在Java中旋转PDF页面,可以使用IronPDF的Java库。使用rotatePage方法旋转单个页面,或使用rotateAllPages旋转文档内的所有页面。这些方法允许您指定旋转角度,如90或270度。
使用Java旋转PDF的设置要求是什么?
要使用IronPDF在Java中旋转PDF,确保您已安装JDK、Maven和IronPDF库。您还必须在项目的pom.xml文件中包含IronPDF和Slf4j的依赖项。
IronPDF可以在Java中将网页转换为PDF吗?
是的,IronPDF可以通过渲染HTML文件为像素完美的PDF,将网页转换为PDF,同时保持准确的文本和图像格式。
使用 IronPDF for Java 是否需要收费?
IronPDF for Java免费用于开发目的。然而,部署需要商业许可证,定价自基础层开始。
如何在Java中更改PDF的纸张方向?
要在Java中使用IronPDF更改PDF的纸张方向,请使用ChromePdfRenderOptions类,并在渲染PDF之前将PaperOrientation属性设置为纵向或横向。
我可以在哪里获得IronPDF Java库?
您可以从Maven Central或IronPDF产品官方网站下载IronPDF Java库。
IronPDF支持PDF中的表单字段吗?
是的,IronPDF支持PDF中的表单字段,允许开发人员以编程方式创建和操作表单组件。
IronPDF提供了哪些额外的PDF操作功能?
IronPDF提供多种用于PDF操作的功能,包括将PDF渲染为图像、提取文本和内容、渲染图表以及通过密码和数字签名增强文档安全性。










