如何在 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 專案的建置自動化工具。 可以從Apache Maven 網站下載 Maven。
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您還需要在
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預設使用縱向模式。 但是,開發者可以使用ChromePdfRenderOptions對象,在將 HTML、RTF、URL 等內容轉換為 PDF 文件時,覆寫此方向。 setPaperOrientation方法接受一個PaperOrientation值作為參數,讓您可以根據需要變更產生的 PDF 的紙張方向。
在上面的程式碼中, PaperOrientation設定為LANDSCAPE 。 PdfDocument類別用於使用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"));以上程式碼修改了上一節中建立的 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 for Java 的 Java 函式庫。使用 rotatePage 方法旋轉個別頁面,或使用 rotateAllPages 方法旋轉文件中的所有頁面。這些方法允許您指定旋轉角度,例如 90 度或 270 度。
使用 Java 旋轉 PDF 有哪些設定要求?
若要在 Java 中使用 IronPDF 旋轉 PDF,請確保您已安裝 JDK、Maven 和 IronPDF 函式庫。您還必須在專案的 pom.xml 檔案中包含 IronPDF 與 Slf4j 的相依性。
IronPDF 可以用 Java 將網頁轉換成 PDF 嗎?
是的,IronPDF 可將網頁轉換為 PDF,將 HTML 檔案渲染為像素完美的 PDF,保持準確的文字和圖像格式。
使用 IronPDF for Java 需要支付費用嗎?
IronPDF for Java 免費供開發使用。但是,部署需要商業授權,價格從基本級開始。
如何在 Java 中變更 PDF 的紙張方向?
要在 Java 中使用 IronPDF 更改 PDF 的紙張方向,請使用 ChromePdfRenderOptions 類,並在渲染 PDF 前將 PaperOrientation 屬性設為 portrait 或 landscape。
在哪裡可以取得 IronPDF for Java 函式庫?
您可以從 Maven Central 或 IronPDF 產品的官方網站下載 IronPDF Java 函式庫。
IronPDF 是否支持 PDF 中的表单字段?
是的,IronPDF 支援 PDF 內的表單欄位,允許開發人員以程式化的方式建立和操作表單元件。
IronPDF 為 PDF 操作提供了哪些額外功能?
IronPDF 提供各種 PDF 處理功能,包括將 PDF 呈現為影像、擷取文字和內容、呈現圖表,以及利用密碼和數位簽章強化文件安全性。







