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
相关推荐
极客先躯1 小时前
说说高级java每日一道面试题-2025年2月13日-数据库篇-请说说 MySQL 数据库的锁 ?
java·数据库·mysql·数据库的锁·模式分·粒度分·属性分
程序员侠客行1 小时前
Spring事务原理 二
java·后端·spring
小猫猫猫◍˃ᵕ˂◍1 小时前
备忘录模式:快速恢复原始数据
android·java·备忘录模式
liuyuzhongcc1 小时前
List 接口中的 sort 和 forEach 方法
java·数据结构·python·list
五月茶2 小时前
Spring MVC
java·spring·mvc
sjsjsbbsbsn2 小时前
Spring Boot定时任务原理
java·spring boot·后端
yqcoder2 小时前
Express + MongoDB 实现在筛选时间段中用户名的模糊查询
java·前端·javascript
五VV2 小时前
Note25021902_TIA Portal V18 WinCC BCA Ed 需要.NET 3.5 SP1
.net
菜鸟单飞2 小时前
介绍一款非常实用的PDF阅读软件!
windows·pdf·电脑
菜鸟蹦迪2 小时前
八股文实战之JUC:ArrayList不安全性
java