C# 获取PDF页面大小、方向、旋转角度

在处理PDF文件时,了解页面的大小、方向和旋转角度等信息对于PDF的显示、打印和布局设计至关重要。本文将介绍如何使用免费.NET 库通过C#来读取PDF页面的这些属性。

  • C# 读取PDF页面大小(宽度、高度)
  • C# 判断PDF页面方向
  • C# 检测PDF页面旋转角度

免费库 Free Spire.PDF for .NET 提供了接口来获取PDF页面信息,我们可以从官网下载产品包后手动添加引用,或者直接通过NuGet安装。

PM> Install-Package FreeSpire.PDF

输入文档如图:

C# 读取PDF页面大小(宽度、高度)

免费Spire.PDF提供了 PdfPageBase.Size.WidthPdfPageBase.Size.Height 属性来获取指定PDF页面的宽度和高度。

获取到的值默认单位为磅(point),如果想要将其转换为厘米、毫米等常见单位,可以通过 PdfUnitConvertor 类的 ConvertUnits(float value, PdfGraphicsUnit from, PdfGraphicsUnit to) 方法进行转换。

示例代码如下:

复制代码
using System;
using System.Text;
using Spire.Pdf;
using Spire.Pdf.Graphics;

namespace GetPDFPageSize
{
    class Program
    {
        static void Main(string[] args)
        {
            //加载PDF文件
            PdfDocument pdf = new PdfDocument();
            pdf.LoadFromFile("示例.pdf");

            //获取第一页
            PdfPageBase page = pdf.Pages[0];

            //获取页面宽度和高度(默认单位为point)
            float pointWidth = page.Size.Width;
            float pointHeight = page.Size.Height;

            //创建PdfUnitConvertor对象用于转换单位
            PdfUnitConvertor unitCvtr = new PdfUnitConvertor();

            //将单位从磅(point)转换为厘米
            float centimeterWidth = unitCvtr.ConvertUnits(pointWidth, PdfGraphicsUnit.Point, PdfGraphicsUnit.Centimeter);
            float centimeterHeight = unitCvtr.ConvertUnits(pointHeight, PdfGraphicsUnit.Point, PdfGraphicsUnit.Centimeter);

            //将单位从磅(point)转换为毫米
            float millimeterWidth = unitCvtr.ConvertUnits(pointWidth, PdfGraphicsUnit.Point, PdfGraphicsUnit.Millimeter);
            float millimeterHeight = unitCvtr.ConvertUnits(pointHeight, PdfGraphicsUnit.Point, PdfGraphicsUnit.Millimeter);

            //输出PDF页面宽高度信息
            Console.WriteLine("该PDF页面大小为(以磅为单位): 宽度 " + pointWidth + "pt, 高度 " + pointHeight + "pt");
            Console.WriteLine("该PDF页面大小为(以厘米为单位): 宽度 " + centimeterWidth + "cm, 高度 " + centimeterHeight + "cm");
            Console.WriteLine("该PDF页面大小为(以毫米为单位): 宽度 " + millimeterWidth + "mm, 高度 " + millimeterHeight + "mm");

        }
    }
}

输出结果:

C# 判断PDF页面方向

页面的方向通常以横向或纵向表示。要判断指定PDF页面的方向:

  1. 先获取页面宽度和高度
  2. 再比较这两个值。(如果宽度大于高度,则页面方向为横向,反之则为纵向。)

示例代码如下:

复制代码
using Spire.Pdf;
using System;

namespace GetPDFPageOrientation
{
    class Program
    {
        static void Main(string[] args)
        {
            //加载PDF文档
            PdfDocument pdf = new PdfDocument();
            pdf.LoadFromFile("示例.pdf");

            //获取第一页
            PdfPageBase page = pdf.Pages[0];

            //获取页面宽度和高度
            float width = page.Size.Width;
            float height = page.Size.Height;

            //通过比较页面宽度和高度来判断页面方向
            if (width > height)
            {
                Console.WriteLine("当前页面方向为横向。");
            }

            else
            {
                Console.WriteLine("当前页面方向为纵向。");
            }
        }
    }
}

输出结果:

C# 检测PDF页面旋转角度

通过 PdfPageBase.Rotation 属性可以获取指定PDF页面的旋转角度。如果为 0,则表示页面保持原来的方向。

示例代码如下:

复制代码
using Spire.Pdf;
using System;

namespace GetPDFPageOrientation
{
    class Program
    {
        static void Main(string[] args)
        {
            //加载PDF文档
            PdfDocument pdf = new PdfDocument();
            pdf.LoadFromFile("示例.pdf");

            //获取第一页
            PdfPageBase page = pdf.Pages[0];

            //获取页面的旋转角度并输出结果
            PdfPageRotateAngle rotationAngle = page.Rotation;
            string rotation = rotationAngle.ToString();

            Console.WriteLine("当前页面旋转角度为: " + rotation);
        }
    }
}

输出结果:


-- 如有任何疑问,可前往论坛交流。

相关推荐
鲤籽鲲12 小时前
C# Lambda 表达式 详解
开发语言·c#·c# 知识捡漏
Whappy00114 小时前
第八节:基于Winform框架的串口助手小项目---完结优化《C#编程》
开发语言·c#
Big_潘大师15 小时前
C#程序加密与解密Demo程序示例
c#·加密·软件授权
xcLeigh16 小时前
WPF在特定领域的应用:打造一款专业的图像编辑工具
c#·wpf
PfCoder18 小时前
C# 程序结构
开发语言·c#·.net
PfCoder21 小时前
C# 常用数据类型
开发语言·c#·vs·winform
nuclear20111 天前
C# 在Excel中插入和操作切片器-详解
c#·excel切片器·添加excel切片器·修改excel切片器·删除excel切片器
yuanpan1 天前
23种设计模式之《观察者模式(Observer)》在c#中的应用及理解
观察者模式·设计模式·c#
计算机学姐1 天前
基于Asp.net的高校社交学习交流平台
vue.js·后端·学习·sqlserver·c#·asp.net·visual studio
yantuguiguziPGJ1 天前
Trae IDE新建C#工程
开发语言·c#