跳過到頁腳內容
使用 IRONPDF FOR JAVA

如何在 Java 中旋轉 PDF 文件

在 Java 中以編程方式管理 PDF 是生成按需報告、發票或賬單的關鍵。 旋轉 PDF 頁面以解決視角問題也是非常有價值的。 在 Java 中這兩項任務可能很具挑戰性。 本文將使用 IronPDF Java 庫來簡化 PDF 頁面旋轉。

IronPDF Java 庫

IronPDF for Java helps Java developers 創建、編輯和操作 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 應用程序,您需要在計算機上下載並安裝以下先決條件:

  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 應用程序中的頁面方向任務。

添加必要的導入和許可證密鑰

首先,將以下 import 語句添加到主 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

接下來,在 main 方法中調用 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 對象覆蓋該方向。 setPaperOrientation 方法將 PaperOrientation 值作為參數,允許您根據需要改變生成的 PDF 的紙張方向。

在上面的代碼中,PaperOrientation 設置為 LANDSCAPE。 A PdfDocument class is used to convert a URL to a PDF document using the renderUrlAsPdf method with renderOptions as the second argument.

最後,使用 saveAs 方法在指定的目錄中保存文檔。

如何在 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 的 rotatePage 僅將現有文檔的 firstPage 旋轉 90 度順時針(使用 CLOCKWISE_90)。 之後,rotateAllPages 將每一頁(包括第一頁)旋轉 CLOCKWISE_270

如何在 Java 中旋轉 PDF 文件,圖 2:旋轉後的 PDF 輸出 旋轉後的 PDF 輸出

Read more about page orientation in the Code Examples section.

如何在 Java 中旋轉 PDF 文件,圖 3:IronPDF for Java IronPDF for Java

總結

本文演示了如何創建一個新的橫向文檔。

IronPDF 還為開發人員提供了將 PDF 文檔渲染為圖像並從 PDF 中提取文本和內容的方法。 Additionally, IronPDF is also capable of rendering charts in PDFs, enhancing security with passwords, and even handling digital signatures programmatically.

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 中心或官方 IronPDF 產品網站下載 IronPDF Java 庫。

IronPDF 支持 PDF 中的表單字段嗎?

是的,IronPDF 支持 PDF 中的表單字段,允許開發人員創建和以程式化的方式操控表單組件。

IronPDF 提供哪些附加功能來進行 PDF 操作?

IronPDF 提供各種功能用於 PDF 操作,包括將 PDF 呈現為圖像、提取文本和內容、呈現圖表,以及通過密碼和數字簽名增強文檔安全性。

Darrius Serrant
全棧軟件工程師 (WebOps)

Darrius Serrant 擁有邁阿密大學計算機科學學士學位,目前任職於 Iron Software 的全栈 WebOps 市場營銷工程師。從小就迷上編碼,他認為計算既神秘又可接近,是創意和解決問題的完美媒介。

在 Iron Software,Darrius 喜歡創造新事物,並簡化複雜概念以便於理解。作為我們的駐場開發者之一,他也自願教學生,分享他的專業知識給下一代。

對 Darrius 來說,工作令人滿意因為它被重視且有實際影響。