直式與橫式排版
from ironpdf import *
 
# For NEW PDF Documents use PdfPaperOrientation
# PdfPaperOrientation: To choose Landscape or Portrait Orientation in a new PDF document
renderer = ChromePdfRenderer()
renderer.RenderingOptions.PaperOrientation = PdfPaperOrientation.Landscape

new_pdf_from_html = renderer.RenderHtmlAsPdf("<h1> Hello World! </h1>")
new_pdf_from_html_file = renderer.RenderHtmlFileAsPdf("example.html")
new_pdf_from_url = renderer.RenderUrlAsPdf("https://ironpdf.com")

# For EXISTING PDFs use PageRotation
# PageRotation: To rotate a PDF page and all of its contents
existing_pdf = PdfDocument.FromFile("old_report.pdf")

# Get rotation value
get_rotation_first_page = existing_pdf.Pages[0].PageRotation

# Rotate specified page
existing_pdf.SetPageRotation(PageIndex=0, Rotation=PdfPageRotation.Clockwise90)

# Rotate ALL pages
existing_pdf.SetAllPageRotations(PdfPageRotation.Clockwise270)
pip install ironpdf
直式與橫式排版

直式與橫式排版

IronPDF for Python 支援新增PDF的橫向和縱向方向,並提供現有文件的每頁旋轉控制。

入門指南

對於新增PDF,在呼叫任何渲染方法之前設置 RenderingOptions.PaperOrientation。 對於現有PDF,使用 SetPageRotation 或 SetAllPageRotations 在載入的 PdfDocument 上旋轉個別頁面或整份文件。

理解程式碼

新增PDF

  • PaperOrientation = PdfPaperOrientation.Landscape:以橫向方向渲染PDF。 使用 Portrait 回到預設的正向排版。
  • 可與 RenderHtmlFileAsPdf 及 RenderUrlAsPdf 搭配使用。

現有PDF

  • PdfDocument.FromFile(path):載入現有PDF文件。
  • Pages[0].PageRotation:讀取第一頁的當前旋轉角度(從零開始的索引)。
  • SetPageRotation(PageIndex, Rotation):將單一頁面旋轉至指定角度。
  • SetAllPageRotations(Rotation):對文件中的每一頁應用相同的旋轉。

旋轉值

PdfPageRotation 列舉提供四個選項:

值度數
None0°
Clockwise9090°
Clockwise180180°
Clockwise270270°
學習如何用 IronPDF for Python 自訂PDF版面!

準備開始了嗎?

版本:2026.11剛剛發布