如何在 .NET C# 中設定頁面方向和旋轉 | IronPDF

How to Set Page Orientation and Rotation

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度。

快速入門:在C#中設置PDF頁面方向和旋轉

使用IronPDF在.NET C#中輕鬆設置PDF文件的頁面方向和旋轉。 首先加載您的PDF,然後使用簡單的方法調用應用所需的旋轉或方向。 快速保存您更新的文檔,確保佈局滿足任何特定要求。 本快速指南幫助您輕鬆上手。

Nuget IconGet started making PDFs with NuGet now:

  1. Install IronPDF with NuGet Package Manager

    PM > Install-Package IronPdf

  2. Copy and run this code snippet.

    IronPdf.PdfDocument.FromFile("file.pdf")
        .SetAllPageRotations(IronPdf.PdfDocument.PageRotation.Rotate90)
        .SaveAs("rotated.pdf");
  3. Deploy to test on your live environment

    Start using IronPDF in your project today with a free trial
    arrow pointer
class="hsg-featured-snippet">

簡化工作流程(5步驟)

  1. 下載IronPDF C#庫以設置PDF頁面方向和旋轉
  2. 使用PaperOrientation屬性在渲染之前設置頁面方向
  3. 探索IronPDF的所有頁面旋轉選項
  4. 學習旋轉單個或多個PDF頁面的方法
  5. 使用IronPDF檢索PDF頁面旋轉


頁面方向範例

僅在從其他格式生成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")
$vbLabelText   $csharpLabel

輸出PDF


頁面旋轉範例

IronPDF提供四種旋轉角度:

  • None: 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");
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")
$vbLabelText   $csharpLabel

輸出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)
$vbLabelText   $csharpLabel

常見問題解答

如何使用 C# 設定 PDF 的頁面方向?

您可以使用 IronPDF 中RenderingOptions類別的PaperOrientation屬性來設定頁面方向。在渲染 PDF 之前,可以將此屬性設定為縱向或橫向。

PDF頁面可能的旋轉角度有哪些?

IronPDF 讓您可以將 PDF 頁面旋轉到四個可能的角度:0 度(無)、90 度(順時針 90)、180 度(順時針 180)和 270 度(順時針 270)。

如何使用 C# 旋轉 PDF 文件中的所有頁面?

您可以使用 IronPDF 的SetAllPageRotations方法來旋轉 PDF 文件中的所有頁面,並指定所需的旋轉角度(例如, PdfPageRotation.Clockwise90 )。

是否可以使用 C# 旋轉 PDF 中的特定頁面?

是的,您可以使用 IronPDF 的SetPageRotation方法旋轉單一頁面,或透過提供頁面索引來使用SetPageRotations方法旋轉多個頁面。

如何確定PDF頁面的目前旋轉方向?

您可以使用 IronPDF 中的GetPageRotation方法,透過提供頁面索引來擷取特定頁面的旋轉角度。

建立新 PDF 時,預設頁面方向是什麼?

使用 IronPDF 建立新 PDF 時,預設頁面方向為縱向。

我是否需要任何額外的庫來操作 PDF 中的頁面方向和旋轉?

是的,您需要下載 IronPDF C# 庫來操作 PDF 中的頁面方向和旋轉。

在 C# 中使用 IronPDF 進行 PDF 處理有什麼好處?

IronPDF 提供了一套全面的工具,可在 C# 中建立、編輯和操作 PDF 文檔,包括設定頁面方向和旋轉,從而增強文檔呈現效果並符合特定的檢視偏好。

IronPDF 是否完全相容於 .NET 10,包括方向和旋轉功能?

是的-IronPDF 支援 .NET 10 以及早期版本(.NET 9、8 等),並且可以與所有頁面方向和旋轉功能(如RenderingOptions.PaperOrientationSetPageRotationSetAllPageRotationsGetPageRotation )無縫配合使用。

A PHP Error was encountered

Severity: Warning

Message: Illegal string offset 'name'

Filename: sections/author_component.php

Line Number: 18

Backtrace:

File: /var/www/ironpdf.com/application/views/main/sections/author_component.php
Line: 18
Function: _error_handler

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 63
Function: view

File: /var/www/ironpdf.com/application/views/products/sections/three_column_docs_page_structure.php
Line: 64
Function: main_view

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 88
Function: view

File: /var/www/ironpdf.com/application/views/products/how-to/index.php
Line: 2
Function: view

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 88
Function: view

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 552
Function: view

File: /var/www/ironpdf.com/application/controllers/Products/Howto.php
Line: 31
Function: render_products_view

File: /var/www/ironpdf.com/index.php
Line: 292
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: Illegal string offset 'title'

Filename: sections/author_component.php

Line Number: 38

Backtrace:

File: /var/www/ironpdf.com/application/views/main/sections/author_component.php
Line: 38
Function: _error_handler

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 63
Function: view

File: /var/www/ironpdf.com/application/views/products/sections/three_column_docs_page_structure.php
Line: 64
Function: main_view

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 88
Function: view

File: /var/www/ironpdf.com/application/views/products/how-to/index.php
Line: 2
Function: view

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 88
Function: view

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 552
Function: view

File: /var/www/ironpdf.com/application/controllers/Products/Howto.php
Line: 31
Function: render_products_view

File: /var/www/ironpdf.com/index.php
Line: 292
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: Illegal string offset 'comment'

Filename: sections/author_component.php

Line Number: 48

Backtrace:

File: /var/www/ironpdf.com/application/views/main/sections/author_component.php
Line: 48
Function: _error_handler

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 63
Function: view

File: /var/www/ironpdf.com/application/views/products/sections/three_column_docs_page_structure.php
Line: 64
Function: main_view

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 88
Function: view

File: /var/www/ironpdf.com/application/views/products/how-to/index.php
Line: 2
Function: view

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 88
Function: view

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 552
Function: view

File: /var/www/ironpdf.com/application/controllers/Products/Howto.php
Line: 31
Function: render_products_view

File: /var/www/ironpdf.com/index.php
Line: 292
Function: require_once

準備好開始了嗎?
Nuget 下載 16,154,058 | 版本: 2025.11 剛剛發布