跳至页脚内容
在 JAVA 中使用 IRONPDF

如何在 Java 中旋转 PDF 文件

在Java中以编程方式管理PDF对于生成按需报告、发票或账单至关重要。 旋转PDF页面以修复视角问题也很有价值。 这两项任务在Java中都可能具有挑战性。 本文将使用IronPDF Java库简化PDF页面旋转。

IronPDF Java库

IronPDF 适用于 Java帮助Java开发人员创建、编辑和操作PDF文档。 该库允许开发人员处理PDF文档布局和格式的几乎每个方面,例如一个或多个页面的当前旋转。

除了创建和操作PDF,IronPDF在将HTML文件转换为像素完美的PDF方面也非常有效。 IronPDF 可在不丢失任何格式的情况下渲染所有图像和文本。 表单组件在PDF文件中受支持。

IronPDF的JAR文件可以从Maven Central或直接从产品网站下载安装。

使用Java旋转文档的步骤

前提条件

要创建一个可以旋转页面的PDF应用程序,您需要在计算机上下载并安装以下前提条件:

  1. JDK(Java开发工具包): 在您的计算机上安装最新版本的JDK以编译和运行PDF旋转应用程序。 JDK可以从官方网站下载。
  2. Maven: 需要安装Maven,因为它是一个主要用于Java项目的构建自动化工具。 Maven可以从Apache Maven网站下载。
  3. 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
  4. 您还需要在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.*;
JAVA

接下来,在License.setLicenseKey来设置您在购买时获得的有效产品许可证密钥(如果您没有许可证密钥,请跳过此步骤,或注册试用许可证密钥)。

License.setLicenseKey("Your license key");
License.setLicenseKey("Your license key");
JAVA

以纵向或横向渲染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"));
JAVA

IronPDF默认使用纵向方向。 然而,开发者可以在将HTML、RTF、URL等内容转换为PDF文档时,通过ChromePdfRenderOptions对象来覆盖此方向设置。 PaperOrientation值作为参数,允许您根据需要更改生成的PDF的纸张方向。

在上述代码中,LANDSCAPEPdfDocument类用于使用renderOptions作为第二个参数。

最后,文档使用[saveAs](/java/object-reference/api/com/ironsoftware/ironpdf/PdfDocument.html#saveAs(java.lang.String)方法保存在指定目录中。

如何在Java中旋转PDF文件,图1:输出PDF文件 输出 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"));
JAVA

上面的代码修改了前一节中创建的PDF文档。 以前生成的整个文档都是横向的,但在这里,IronPDF的CLOCKWISE_90)。 之后,CLOCKWISE_270

如何在Java中旋转PDF文件,图2:旋转后的PDF输出 旋转后的PDF输出

页面方向代码示例部分中阅读更多内容。

如何在Java中旋转PDF文件,图3:IronPDF for Java IronPDF 适用于 Java

摘要

本文将演示如何创建一个横向方向的新文档。

IronPDF还为开发人员提供将PDF文档渲染为图像的方法,并从PDF中提取文本和内容。 此外,IronPDF还能够在PDF中渲染图表,通过密码增强安全性,甚至以编程方式处理数字签名

IronPDF for Java可免费使用,但用于部署目的,它需要一个商业许可证,起价仅为$999。 您也可以访问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,同时保持准确的文本和图像格式。

using 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渲染为图像、提取文本和内容、渲染图表以及通过密码和数字签名增强文档安全性。

Darrius Serrant
全栈软件工程师(WebOps)

Darrius Serrant 拥有迈阿密大学的计算机科学学士学位,目前在 Iron Software 担任全栈 WebOps 市场工程师。从小就被编码吸引,他认为计算机既神秘又易于接触,使其成为创意和问题解决的理想媒介。

在 Iron Software,Darrius 喜欢创造新事物,并简化复杂概念以使其更易理解。作为我们常驻的开发者之一,他还自愿教授学生,与下一代分享他的专业知识。

对于 Darrius 来说,他的工作令人满意,因为它被重视并产生真正的影响。

钢铁支援团队

我们每周 5 天,每天 24 小时在线。
聊天
电子邮件
打电话给我