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 分钟前
【源码+文档+调试讲解】党员之家服务系统小程序1
java·开发语言·spring boot·后端·小程序·旅游
冼紫菜11 分钟前
Java开发中使用 RabbitMQ 入门到进阶详解(含注解方式、JSON配置)
java·spring boot·后端·rabbitmq·springcloud
Kakikori11 分钟前
JSP链接MySQL8.0(Eclipse+Tomcat9.0+MySQL8.0)
java·开发语言
Dr.92723 分钟前
1-10 目录树
java·数据结构·算法
冬日枝丫32 分钟前
【spring】spring学习系列之六:spring的启动流程(下)
java·学习·spring
圈圈编码39 分钟前
LeetCode Hot100刷题——轮转数组
java·算法·leetcode·职场和发展
〆、风神40 分钟前
面试真题 - 高并发场景下Nginx如何优化
java·nginx·面试
應呈1 小时前
FreeRTOS的学习记录(任务创建,任务挂起)
java·linux·学习
钢铁男儿1 小时前
C# 深入理解类(静态函数成员)
java·开发语言·c#
bbsh20991 小时前
动易.NET系列产品:Safari浏览器登录后台提示登录信息过期的问题
java·.net·safari