如何設定頁面方向和旋轉

Chaknith related to 如何設定頁面方向和旋轉
查克尼思·賓
2023年9月25日
已更新 2024年10月20日
分享:
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");

輸出 PDF


頁面旋轉範例

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

  • :0 度或未旋轉的文件。
  • Clockwise90:順時針旋轉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");

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