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
相关推荐
朝新_2 小时前
【多线程初阶】阻塞队列 & 生产者消费者模型
java·开发语言·javaee
立莹Sir2 小时前
Calendar类日期设置进位问题
java·开发语言
季鸢4 小时前
Java设计模式之状态模式详解
java·设计模式·状态模式
@yanyu6664 小时前
springboot实现查询学生
java·spring boot·后端
ascarl20104 小时前
准确--k8s cgroup问题排查
java·开发语言
magic 2454 小时前
Lombok 的 @Data 注解失效,未生成 getter/setter 方法引发的HTTP 406 错误
java
爱敲代码的憨仔5 小时前
分布式协同自动化办公系统-工作流引擎-流程设计
java·flowable·oa
纪元A梦5 小时前
分布式拜占庭容错算法——PBFT算法深度解析
java·分布式·算法
卿着飞翔5 小时前
RabbitMQ入门4.1.0版本(基于java、SpringBoot操作)
java·rabbitmq·java-rabbitmq
陈阿土i5 小时前
SpringAI 1.0.0 正式版——利用Redis存储会话(ChatMemory)
java·redis·ai·springai