纵向和横向方向
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,在加载的 PdfDocument 上使用 SetPageRotation 或 SetAllPageRotations 旋转单独页面或整个文档。

理解代码

新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刚刚发布