Spire.PDF for .NET【页面设置】演示:旋转 PDF 中的页面

在某些情况下,您可能需要旋转 PDF 页面。例如,当您收到包含混乱页面的 PDF 文档时,您可能希望旋转页面以便更轻松地阅读文档。在本文中,您将学习如何使用Spire.PDF for .NET在 C# 和 VB.NET 中旋转 PDF 中的页面。

Spire.PDF for .NET 是一款独立 PDF 控件,用于 .NET 程序中创建、编辑和操作 PDF 文档。使用 Spire.PDF 类库,开发人员可以新建一个 PDF 文档或者对现有的 PDF 文档进行处理,且无需安装 Adobe Acrobat。

E-iceblue功能类库Spire 系列文档处理组件均由中国本土团队研发,不依赖第三方软件,不受其他国家的技术或法律法规限制,同时适配国产操作系统如中科方德、中标麒麟等,兼容国产文档处理软件 WPS(如 .wps/.et/.dps 等格式

Spire.PDF for.net下载

安装 Spire.PDF for .NET

首先,您需要将 Spire.PDF for.NET 包中包含的 DLL 文件作为引用添加到您的 .NET 项目中。 可以从此链接下载 DLL 文件,也可以通过NuGet安装

复制代码
PM> Install-Package Spire.PDF
使用 C# 和 VB.NET 旋转 PDF 中的特定页面

旋转以 90 度为增量。您可以将 PDF 页面旋转 0/90/180/270 度。以下是旋转 PDF 页面的步骤:

  • 创建PdfDocument类的实例。
  • **使用PdfDocument.LoadFromFile()**方法加载 PDF 文档。
  • **通过PdfDocument.Pages[pageIndex]**属性根据其索引(从零开始)获取所需的页面。
  • 通过PdfPageBase.Rotation属性获取页面原始旋转角度。
  • 将原始旋转角度增加所需的度数。
  • 通过PdfPageBase.Rotation属性将新的旋转角度应用到页面。
  • **使用PdfDocument.SaveToFile()**方法保存结果文档。

[C#]

复制代码
using Spire.Pdf;

namespace RotatePdfPage
{
class Program
{
static void Main(string[] args)
{
//Create a PdfDocument instance
PdfDocument pdf = new PdfDocument();
//Load a PDF document
pdf.LoadFromFile("Sample.pdf");

//Get the first page
PdfPageBase page = pdf.Pages[0];

//Get the original rotation angle of the page
int rotation = (int)page.Rotation;

//Rotate the page 180 degrees clockwise based on the original rotation angle
rotation += (int)PdfPageRotateAngle.RotateAngle180;
page.Rotation = (PdfPageRotateAngle)rotation;

//Save the result document
pdf.SaveToFile("Rotate.pdf");
}
}
}

[VB.NET]

复制代码
Imports Spire.Pdf

Namespace RotatePdfPage
Friend Class Program
Private Shared Sub Main(ByVal args As String())
'Create a PdfDocument instance
Dim pdf As PdfDocument = New PdfDocument()
'Load a PDF document
pdf.LoadFromFile("Sample.pdf")

'Get the first page
Dim page As PdfPageBase = pdf.Pages(0)

'Get the original rotation angle of the page
Dim rotation = CInt(page.Rotation)

'Rotate the page 180 degrees clockwise based on the original rotation angle
rotation += CInt(PdfPageRotateAngle.RotateAngle180)
page.Rotation = CType(rotation, PdfPageRotateAngle)

'Save the result document
pdf.SaveToFile("Rotate.pdf")
End Sub
End Class
End Namespace
使用 C# 和 VB.NET 旋转 PDF 中的所有页面

以下是旋转 PDF 文档中所有页面的步骤:

  • 创建PdfDocument类的实例。
  • **使用PdfDocument.LoadFromFile()**方法加载 PDF 文档。
  • 循环遍历文档中的每一页。
  • 通过PdfPageBase.Rotation属性获取页面原始旋转角度。
  • 将原始旋转角度增加所需的度数。
  • 通过PdfPageBase.Rotation属性将新的旋转角度应用到页面。
  • **使用PdfDocument.SaveToFile()**方法保存结果文档。

[C#]

复制代码
using Spire.Pdf;

namespace RotateAllPdfPages
{
class Program
{
static void Main(string[] args)
{
//Create a PdfDocument instance
PdfDocument pdf = new PdfDocument();
//Load a PDF document
pdf.LoadFromFile("Sample.pdf");

foreach (PdfPageBase page in pdf.Pages)
{
//Get the original rotation angle of the page
int rotation = (int)page.Rotation;
//Rotate the page 180 degrees clockwise based on the original rotation angle
rotation += (int)PdfPageRotateAngle.RotateAngle180;
page.Rotation = (PdfPageRotateAngle)rotation;
}

//Save the result document
pdf.SaveToFile("RotateAll.pdf");
}
}
}

[VB.NET]

复制代码
Imports Spire.Pdf

Namespace RotateAllPdfPages
Friend Class Program
Private Shared Sub Main(ByVal args As String())
'Create a PdfDocument instance
Dim pdf As PdfDocument = New PdfDocument()
'Load a PDF document
pdf.LoadFromFile("Sample.pdf")

For Each page As PdfPageBase In pdf.Pages
'Get the original rotation angle of the page
Dim rotation = CInt(page.Rotation)
'Rotate the page 180 degrees clockwise based on the original rotation angle
rotation += CInt(PdfPageRotateAngle.RotateAngle180)
page.Rotation = CType(rotation, PdfPageRotateAngle)
Next

'Save the result document
pdf.SaveToFile("RotateAll.pdf")
End Sub
End Class
End Namespace
相关推荐
吃海鲜的骆驼4 分钟前
服务异步通讯与RabbitMQ
java·分布式·后端·rabbitmq
羱滒7 分钟前
sql调优之数据库开发规范
java·数据库·数据库开发
不想有bug的小菜鸟14 分钟前
vue3使用iframe全屏展示pdf效果
前端·pdf
Forget the Dream36 分钟前
设计模式之责任链模式
java·c++·设计模式·责任链模式
jonyleek39 分钟前
「JVS更新日志」低代码、企业会议、智能BI、智能排产2.26更新说明
java·大数据·低代码·数据分析·软件需求
计算机小白一个1 小时前
蓝桥杯 Java B 组之最短路径算法(Dijkstra、Floyd-Warshall)
java·数据结构·算法·蓝桥杯
曼岛_1 小时前
[密码学实战]Java实现SM4加解密(ecb,cbc)及工具验证
java·密码学
Forget the Dream1 小时前
设计模式之代理模式
java·c++·设计模式·代理模式
帅的飞起来2 小时前
设计模式--spring中用到的设计模式
java·spring·设计模式
吃汤圆的抹香鲸2 小时前
Rider 安装包 绿色版 Win/Mac/Linux 适合.NET和游戏开发者使用 2025全栈开发终极指南:从零配置到企业级实战
linux·运维·windows·sql·游戏·macos·.net