如何设置页面方向和旋转

Chaknith related to 如何设置页面方向和旋转
查克尼特·宾
2023年九月25日
更新 2024年十月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 票据,还支持产品开发、文档编写和市场营销,从而提升客户的整体体验。当他不在办公室时,他可能会在学习机器学习、编程或徒步旅行。