跳至頁尾內容
使用 IRONPDF FOR JAVA

如何在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 應用程序,您需要在電腦上下載並安裝以下必備軟體:

  1. JDK(Java 開發工具包):在您的電腦上安裝最新版本的 JDK,以編譯和執行 PDF 旋轉應用程式。 可從官方網站下載 JDK。
  2. Maven:需要安裝 Maven,因為它是一個主要用於 Java 專案的建置自動化工具。 可以從Apache Maven 網站下載 Maven。
  3. IronPDF Java 庫:現在您需要最新版本的 IronPDF 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

接下來,在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預設使用縱向模式。 但是,開發者可以使用ChromePdfRenderOptions對象,在將 HTML、RTF、URL 等內容轉換為 PDF 文件時,覆寫此方向。 setPaperOrientation方法接受一個PaperOrientation值作為參數,讓您可以根據需要變更產生的 PDF 的紙張方向。

在上面的程式碼中, PaperOrientation設定為LANDSCAPEPdfDocument類別用於使用renderUrlAsPdf 方法將 URL 轉換為 PDF 文檔,並將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 的rotatePage只會將現有文檔的firstPage順時針旋轉 90 度(使用CLOCKWISE_90 )。 之後, rotateAllPages將每一頁(包括第一頁)按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 渲染成圖像、提取文字和內容、渲染圖表以及使用密碼和數位簽名增強文件安全性。

柯蒂斯·週
技術撰稿人

Curtis Chau擁有卡爾頓大學電腦科學學士學位,專長於前端開發,精通Node.js、TypeScript、JavaScript和React。他熱衷於打造直覺美觀的使用者介面,喜歡使用現代框架,並擅長撰寫結構清晰、視覺效果出色的使用者手冊。

除了開發工作之外,柯蒂斯對物聯網 (IoT) 也抱有濃厚的興趣,致力於探索硬體和軟體整合的創新方法。閒暇時,他喜歡玩遊戲和製作 Discord 機器人,將他對科技的熱愛與創造力結合。