如何設定頁面方向和旋轉
頁面方向指的是頁面的佈局方式,可以是垂直(縱向)或水平(橫向).
頁面旋轉是調整頁面角度的過程,允許您更改其方向,這對於糾正對齊或滿足特定的觀看偏好非常有用。 頁面角度可以設定為 90、180 和 270 度。
IronPDF允許您在渲染過程中指定方向為肖像或風景。 此外,您可以根據需要將新渲染或現有的PDF頁面分別旋轉0、90、180或270度。
如何設定頁面方向和旋轉
立即在您的專案中使用IronPDF,並享受免費試用。
頁面方向示例
僅在從其他格式生成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")
輸出 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")
輸出 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)