縱向和橫向方向

IronPDF for Java 可以修改新的和現有的 PDF 文件的頁面方向。

使用 IronPDF 渲染的新PDF文件默認採用豎向格式。 開發人員可以使用ChromePdfRenderOptions實例在將內容(HTML、RTFs、URLs 等)轉換為PDF時覆蓋此行為。 setPaperOrientation 方法接受 PaperOrientation 值,允許開發人員根據需要更改結果 PDF 的紙張方向。 範例代碼的第21至23行創建了一個設置為橫向的PDF文件。 在第 21 行使用 PaperOrientation.LANDSCAPE 調用 setPaperOrientation 會觸發方向行為。 將列舉值替換為PaperOrientation.PORTRAIT會使隨後調用PdfDocument.renderUrlAsPdf以縱向方向創建IronPDF首頁

ChromePdfRenderOptions 物件無法用於更改現有 PDF 的頁面方向(這些可以是從先前對任何 PDF 繪製方法的調用中產生的 PdfDocument,或使用 PdfDocument.fromFile 方法加載到 IronPDF 中的檔案)。 對於這些PDF文件,可以通過基於旋轉的變換調整頁面方向。 為此,IronPDF 提供了 rotateAllPages 方法以供使用。

rotateAllPages 接受一個 PageRotation 列舉型別,其指定一組可接受的旋轉值。 第40行的示例代碼將工作PDF文檔中的每個頁面順時針旋轉270度。 若要僅旋轉PDF中的一頁(或部分頁面),請選擇使用rotatePage方法,而非rotateAllPages

existingPdf.rotatePage(PageRotation.CLOCKWISE_270, PageSelection.firstPage());  
existingPdf.rotatePage(PageRotation.CLOCKWISE_180, PageSelection.lastPage());  
existingPdf.rotatePage(PageRotation.CLOCKWISE_90, PageSelection.singlePage(8)); // Rotate page 9  
existingPdf.rotatePage(PageRotation.CLOCKWISE_270, PageSelection.pageRange(9, 14)); // Rotate pages 10 - 15
existingPdf.rotatePage(PageRotation.CLOCKWISE_270, PageSelection.firstPage());  
existingPdf.rotatePage(PageRotation.CLOCKWISE_180, PageSelection.lastPage());  
existingPdf.rotatePage(PageRotation.CLOCKWISE_90, PageSelection.singlePage(8)); // Rotate page 9  
existingPdf.rotatePage(PageRotation.CLOCKWISE_270, PageSelection.pageRange(9, 14)); // Rotate pages 10 - 15
JAVA