C#/VB.NET 如何旋转PDF页面

C#/VB.NET 如何旋转PDF页面

在日常工作中有时候会碰到PDF文档页面方向倒置的情况,这样文档阅读起来很不方便,因此我们需要对PDF文档的页面方向进行调整,也就是旋转页面。本文将介绍如何使用C#和免费PDF组件Free Spire.PDF实现旋转PDF页面的功能。

首先下载Free Spire.PDF并安装,然后创建C#/VB应用程序,再通过下面的路径把Bin文件夹里的Spire.PDF.dll添加为引用。路径大致如下:"...\Spire.pdf-fe\Bin\NET4.0\ Spire.PDF.dll"


方法/步骤:

步骤1:创建PdfDocument实例并载入PDF文档。

PdfDocument pdf = new PdfDocument();
pdf.LoadFromFile("Sample.pdf");
步骤2:获取需要被旋转的页面,此处为第一页。如果要旋转所有页面,只需遍历pdf.Pages。

PdfPageBase page =pdf.Pages[0];

步骤3:获取页面当前的旋转角度,然后在当前旋转角度的基础上旋转页面,角度可选0/90/180/270。

int rotation = (int)page.Rotation;
rotation += (int)PdfPageRotateAngle.RotateAngle270;
page.Rotation = (PdfPageRotateAngle)rotation;

步骤4:保存文档。

pdf.SaveToFile("Output.pdf");

旋转前:


旋转后:



完整代码:

C#:

using Spire.Pdf;
 
namespace RotatePDFPage
{
    class Program
    {
        static void Main(string[] args)
        {
            //载入PDF文档
            PdfDocument pdf = newPdfDocument();
            pdf.LoadFromFile("Sample.pdf");
 
            //获取需要旋转的页面,此处为第一页
            PdfPageBase page =pdf.Pages[0];
 
            //获取页面当前的旋转角度,然后在当前角度的基础上旋转页面
            int rotation = (int)page.Rotation;
            rotation += (int)PdfPageRotateAngle.RotateAngle270;
            page.Rotation = (PdfPageRotateAngle)rotation;
 
            //保存文档
            pdf.SaveToFile("Output.pdf");           
        }
    }
}
VB.NET:

Imports Spire.Pdf
 
Namespace RotatePDFPage
       ClassProgram
              PrivateShared Sub Main(args As String())
                     '载入PDF文档
                     Dimpdf As New PdfDocument()
                     pdf.LoadFromFile("Sample.pdf")
 
                     '获取需要旋转的页面,此处为第一页
                     Dimpage As PdfPageBase = pdf.Pages(0)
 
                     '获取页面当前的旋转角度,然后在当前角度的基础上旋转页面
                     Dimrotation As Integer = CInt(page.Rotation)
                     rotation+= CInt(PdfPageRotateAngle.RotateAngle270)
                     page.Rotation= DirectCast(rotation, PdfPageRotateAngle)
 
                     '保存文档
                     pdf.SaveToFile("Output.pdf")
              EndSub
       EndClass
End Namespace


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值