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

如何在 Java 中旋轉 PDF 文件

在Java中程式化管理PDF對於生成隨需報告、發票或帳單至關重要。 將PDF頁面旋轉以修正視角問題也是很有價值的。 這兩個任務在Java中都具有挑戰性。 本文將使用IronPDF Java Library來簡化PDF頁面旋轉。

IronPDF Java Library

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應用程式,您需要在您的計算機上下載並安裝以下先決條件:

  1. JDK(Java開發套件): 在您的計算機上安裝最新版本的JDK以編譯和運行PDF旋轉應用程式。 JDK可從官方網站下載。
  2. Maven: 需要安裝Maven,因為它是一個主要用於Java項目的構建自動化工具。 可以從Apache Maven網站下載Maven。
  3. IronPDF Java Library: 現在您需要最新版的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、RTFs、URLs等內容轉換為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 for Java

總結

本文演示瞭如何創建一個橫向方向的新文件。

IronPDF還提供開發者將PDF文件渲染為圖像的方法,並提取PDF中的文字和內容。 此外,IronPDF還能夠在PDF中渲染圖表,通過密碼來增強安全性,甚至以程式化方式處理數位簽名

IronPDF for Java免費使用,但出於部署目的,需要一個商業授權,起價僅$799。 您還可以訪問IronPDF完整版的免費試用,以測試其在生產模式中的功能。

常見問題解答

如何在Java中旋轉PDF頁面?

若要在 Java 中旋轉 PDF 頁面,可以使用 IronPDF 的 Java 程式庫。使用rotatePage方法旋轉單一頁面,或使用rotateAllPages方法旋轉文件中的所有頁面。這些方法可讓您指定旋轉角度,例如 90 度或 270 度。

使用 Java 旋轉 PDF 需要哪些設定?

若要在 Java 中使用IronPDF旋轉 PDF 文件,請確保已安裝 JDK、Maven 和IronPDF庫。此外,還必須將IronPDF和 Slf4j 依賴項新增至專案的pom.xml檔案中。

IronPDF能否用 Java 將網頁轉換為 PDF?

是的, IronPDF可以將網頁轉換為 PDF,透過將 HTML 檔案渲染成像素級完美的 PDF,保持準確的文字和圖像格式。

使用IronPDF for Java 是否需要付費?

IronPDF for Java 可免費開發用途。但部署需要商業許可,價格從基礎套餐起。

如何在Java中變更PDF的紙張方向?

若要使用IronPDF在 Java 中變更 PDF 的紙張方向,請使用ChromePdfRenderOptions類,並在渲染 PDF 之前將PaperOrientation屬性設定為 portrait 或 landscape。

我可以在哪裡取得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 來說,工作令人滿意因為它被重視且有實際影響。

Iron Support Team

We're online 24 hours, 5 days a week.
Chat
Email
Call Me