在 .NET 中旋轉 PDF 文字和頁面

This article was translated from English: Does it need improvement?
Translated
View the article in English

旋轉 PDF 文字或頁面是指改變 PDF 文件中整個頁面或特定文字元素的方向。 此旋轉可以用度數來表示。(通常為90度、180度或270度)將內容順時針或逆時針重新定位。

開始使用 IronPDF

立即在您的專案中使用IronPDF,並享受免費試用。

第一步:
green arrow pointer



旋轉 PDF 頁面

使用 SetPageRotationSetPageRotationsSetAllPageRotations 方法分別設定單頁、多頁和所有頁面的旋轉。 這些方法會完全以指定的度數順時針覆蓋當前頁面旋轉。如果原始頁面旋轉已設定為所需的度數,則這些方法不會影響輸出文件。

: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")
VB   C#

使用 CSS3 旋轉文字

在 .NET 中將 HTML 轉換成 PDF 之後,可能需要以程式化的方式旋轉文字或整個頁面。 經常的需求是使用 HTML5 和 CSS3 在 PDF 中渲染垂直對齊的文字。以下是您可以達成此目的的方法。

CSS3 允許在使用 IronPDF 將 PDF 轉換為 HTML 後將文本旋轉到任何角度。IronPDF .NET 庫您之前安裝的。 這是透過使用 -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")
VB   C#