Portrait & Landscape Orientation

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.Paths;

// Use the setPaperOrientation method to set rendered PDFs in portrait or landscape orientation.  
// Note: This will only work for newly-created PDFs!  
ChromePdfRenderOptions renderOptions = new ChromePdfRenderOptions();  
renderOptions.setPaperOrientation(PaperOrientation.LANDSCAPE);  
PdfDocument newPdf = PdfDocument.renderUrlAsPdf("https://ironpdf.com", renderOptions);  
newPdf.saveAs(Paths.get("assets/LandscapePdf.pdf"));  
  
// Use the rotatePage/rotateAllPages methods to adjust the page orientation for existing PDFs  
PdfDocument existingPdf = PdfDocument.fromFile(Paths.get("assets/example.pdf"));  
  
// Get the orientation of the first page of the existing PDF document.  
PageRotation firstPageRotation = existingPdf.getPagesInfo().get(0).getPageRotation();  
System.out.println(firstPageRotation);  
  
// Rotate the first page of the document only 90 degrees clockwise.  
existingPdf.rotatePage(PageRotation.CLOCKWISE_90, PageSelection.firstPage());  
  
// Rotate all pages of the document clockwise.  
existingPdf.rotateAllPages(PageRotation.CLOCKWISE_270);  
  
existingPdf.saveAs(Paths.get("assets/ExistingPdfRotated.pdf"));
mvn install
Portrait & Landscape Orientation

Portrait & Landscape Orientation

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

使用IronPDF重新渲染的新PDF文件預設為縱向方向。 開發者可以在將內容(HTML、RTFs、URLs等)轉換為PDF時,透過ChromePdfRenderOptions實例覆蓋此行為。 PaperOrientation值,允許開發者根據需要更改生成的PDF的紙張方向。 特徵程式碼範例中的第21至23行建立了一個橫向方向的PDF文件。 在第21行,調用PaperOrientation.LANDSCAPE會觸發方向行為。 將枚舉值替換為PdfDocument.renderUrlAsPdf的呼叫將建立IronPDF首頁,呈現為縱向方向。

PdfDocument.fromFile方法載入到IronPDF中)。 對於這些PDF文件,可以使用基於旋轉的轉換來調整頁面方向。 為此,IronPDF提供了可用的rotateAllPages方法。

PageRotation枚舉型別,該型別指定了一組接受的旋轉值。 特徵程式碼範例中的第40行將工作中的PDF文件中的每個頁面順時鐘旋轉270度。 要旋轉PDF中的單頁(或多頁),請使用rotateAllPages。

Learn more about PaperOrientation in IronPDF for Java.
在 GitHub 查看

準備好開始了嗎?

版本:2026.10剛剛發布