在 .NET 中旋转 PDF 文本和页面
旋转 PDF 文本或页面是指改变 PDF 文档中整个页面或特定文本元素的方向。 此旋转可以按度数进行(通常为90度、180度或270度),以顺时针或逆时针方向重新定位内容。
开始使用IronPDF
立即在您的项目中开始使用IronPDF,并享受免费试用。
如何用 C# 旋转 PDF 文件
- 下载 IronPDF C# PDF 库 以旋转 PDF
- 使用提供的方法设置页面旋转
- 按程序旋转 PDF 页面
- 使用 CSS3 旋转 PDF 文本
- 查看 PDF 文档
旋转PDF页面
使用SetPageRotation
、SetPageRotations
和SetAllPageRotations
方法分别设置单页、多页和所有页的旋转。 这些方法将用指定的度数(顺时针测量)完全覆盖当前页面的旋转。如果原始页面旋转已设置为所需的度数,则这些方法不会影响输出文件。
:path=/static-assets/pdf/content-code-examples/how-to/rotating-text-set-page-rotation.cs
using IronPdf;
using IronPdf.Rendering;
using System.Linq;
// Import PDF
PdfDocument pdf = PdfDocument.FromFile("multi-page.pdf");
// Set rotation for a single page
pdf.SetPageRotation(0, PdfPageRotation.Clockwise90);
// Set rotation for multiple pages
pdf.SetPageRotations(Enumerable.Range(1,3), PdfPageRotation.Clockwise270);
// Set rotation for the entire document
pdf.SetAllPageRotations(PdfPageRotation.Clockwise180);
pdf.SaveAs("rotated.pdf");
Imports IronPdf
Imports IronPdf.Rendering
Imports System.Linq
' Import PDF
Private pdf As PdfDocument = PdfDocument.FromFile("multi-page.pdf")
' Set rotation for a single page
pdf.SetPageRotation(0, PdfPageRotation.Clockwise90)
' Set rotation for multiple pages
pdf.SetPageRotations(Enumerable.Range(1,3), PdfPageRotation.Clockwise270)
' Set rotation for the entire document
pdf.SetAllPageRotations(PdfPageRotation.Clockwise180)
pdf.SaveAs("rotated.pdf")
使用 CSS3 来旋转文本
在 .NET 中将 HTML 转换为 PDF 后,可能需要以编程方式旋转文本或整个页面。 经常需要在使用 HTML5 和 CSS3 的 PDF 中渲染垂直对齐的文本。以下是您可以实现此目的的方法。
CSS3 允许在使用之前安装的 IronPDF .NET Library 将 PDF 转换为 HTML 后,将文本旋转到任何角度。 这是通过使用-webkit-transform: rotate
CSS3 样式实现的,该样式可以将任何 HTML 元素旋转到任意角度。
-webkit-transform 允许 HTML 元素进行多种 3D 和 3D 旋转变换和效果。 一个简短的C# HTML转PDF,并将文本旋转180度的示例是:
:path=/static-assets/pdf/content-code-examples/how-to/rotating-text-css.cs
using IronPdf;
var renderer = new IronPdf.ChromePdfRenderer();
var pdf = renderer.RenderHtmlAsPdf(@"
<html>
<head>
<style>
.rotated{
-webkit-transform: rotate(-180deg);
width:400;
height:400;
}
</style>
</head>
<body>
<p class='rotated'>Rotated Text</p>
</body>
</html>
");
pdf.SaveAs("rotated.pdf");
Imports IronPdf
Private renderer = New IronPdf.ChromePdfRenderer()
Private pdf = renderer.RenderHtmlAsPdf("
<html>
<head>
<style>
.rotated{
-webkit-transform: rotate(-180deg);
width:400;
height:400;
}
</style>
</head>
<body>
<p class='rotated'>Rotated Text</p>
</body>
</html>
")
pdf.SaveAs("rotated.pdf")