如何設定頁面方向和旋轉

This article was translated from English: Does it need improvement?
Translated
View the article in English

查克尼思·賓

頁面方向指的是頁面的佈局方式,可以是垂直(縱向)或水平(橫向).

頁面旋轉是調整頁面角度的過程,允許您更改其方向,這對於糾正對齊或滿足特定的觀看偏好非常有用。 頁面角度可以設定為 90、180 和 270 度。

IronPDF允許您在渲染過程中指定方向為肖像或風景。 此外,您可以根據需要將新渲染或現有的PDF頁面分別旋轉0、90、180或270度。


立即在您的專案中使用IronPDF,並享受免費試用。

第一步:
green arrow pointer

頁面方向示例

僅在從其他格式生成PDF文檔時才能設置方向。 您可以從RenderingOptions類別訪問PaperOrientation屬性。 此屬性可以設置為直式或橫式。 預設的頁面方向設置是直式。

代碼

:path=/static-assets/pdf/content-code-examples/how-to/page-orientation-rotation-orientation.cs
using IronPdf;
using IronPdf.Rendering;

ChromePdfRenderer renderer = new ChromePdfRenderer();

// Change paper orientation
renderer.RenderingOptions.PaperOrientation = PdfPaperOrientation.Landscape;

PdfDocument pdf = renderer.RenderUrlAsPdf("https://en.wikipedia.org/wiki/Main_Page");

pdf.SaveAs("landscape.pdf");
Imports IronPdf
Imports IronPdf.Rendering

Private renderer As New ChromePdfRenderer()

' Change paper orientation
renderer.RenderingOptions.PaperOrientation = PdfPaperOrientation.Landscape

Dim pdf As PdfDocument = renderer.RenderUrlAsPdf("https://en.wikipedia.org/wiki/Main_Page")

pdf.SaveAs("landscape.pdf")
VB   C#

輸出 PDF


頁面旋轉範例

IronPDF 提供四種可能的旋轉度數:

  • 無旋轉:0 度或未旋轉文件。
  • 順時針90度: 旋轉90度順時針。
  • Clockwise180:順時針旋轉180度。
  • Clockwise270:順時針旋轉270度。

    請注意
    以下提到的所有頁面索引位置均遵循從零開始的索引。

設定頁面旋轉

使用以下方法設置單個頁面、多個頁面或所有頁面的旋轉。

  • SetAllPageRotations:設定所有頁面的旋轉度。
  • SetPageRotation:設置單頁的旋轉度。
  • SetPageRotations:為選定頁面列表設定旋轉度。
:path=/static-assets/pdf/content-code-examples/how-to/page-orientation-rotation-set-rotation.cs
using IronPdf;
using IronPdf.Rendering;
using System.Collections.Generic;

PdfDocument pdf = PdfDocument.FromFile("landscape.pdf");

// Set all pages
pdf.SetAllPageRotations(PdfPageRotation.Clockwise90);

// Set a single page
pdf.SetPageRotation(1, PdfPageRotation.Clockwise180);

// Set multiple pages
List<int> selectedPages = new List<int>() { 0, 3 };
pdf.SetPageRotations(selectedPages, PdfPageRotation.Clockwise270);

pdf.SaveAs("rotatedLandscape.pdf");
Imports IronPdf
Imports IronPdf.Rendering
Imports System.Collections.Generic

Private pdf As PdfDocument = PdfDocument.FromFile("landscape.pdf")

' Set all pages
pdf.SetAllPageRotations(PdfPageRotation.Clockwise90)

' Set a single page
pdf.SetPageRotation(1, PdfPageRotation.Clockwise180)

' Set multiple pages
Dim selectedPages As New List(Of Integer)() From {0, 3}
pdf.SetPageRotations(selectedPages, PdfPageRotation.Clockwise270)

pdf.SaveAs("rotatedLandscape.pdf")
VB   C#

輸出 PDF

獲取頁面旋轉

使用 GetPageRotation 方法來檢索 PDF 文件中任何特定頁面的旋轉。 只需向方法提供頁面索引。

:path=/static-assets/pdf/content-code-examples/how-to/page-orientation-rotation-get-rotation.cs
using IronPdf;
using IronPdf.Rendering;

PdfDocument pdf = PdfDocument.FromFile("rotatedLandscape.pdf");

PdfPageRotation rotation = pdf.GetPageRotation(1);
Imports IronPdf
Imports IronPdf.Rendering

Private pdf As PdfDocument = PdfDocument.FromFile("rotatedLandscape.pdf")

Private rotation As PdfPageRotation = pdf.GetPageRotation(1)
VB   C#
Chaknith related to 獲取頁面旋轉

查克尼思·賓

軟體工程師

Chaknith 是開發者界的夏洛克福爾摩斯。他第一次意識到自己可能有個軟體工程的未來,是在他為了娛樂而參加程式挑戰的時候。他的重點是 IronXL 和 IronBarcode,但他也引以為豪的是,他幫助客戶解決所有產品的問題。Chaknith 利用他與客戶直接對話中獲得的知識,以進一步改進產品。他的實際反饋超越了 Jira 工單,並支持產品開發、文件撰寫和行銷,以提升客戶的整體體驗。不在公司時,他通常在學習機器學習、寫程式和徒步旅行。