通过C# 读取PDF页面大小、方向、旋转角度

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

文章目录

    • [C# 读取PDF页面大小(宽度、高度)](# 读取PDF页面大小(宽度、高度))
    • [C# 判断PDF页面方向](# 判断PDF页面方向)
    • [C# 检测PDF页面旋转角度](# 检测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) 方法进行转换。

示例代码如下:

csharp 复制代码
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. 再比较这两个值。(如果宽度大于高度,则页面方向为横向,反之则为纵向。)

示例代码如下:

csharp 复制代码
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,则表示页面保持原来的方向。

示例代码如下:

csharp 复制代码
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 分钟前
HarmonyOS应用开发( Beta5.0)HOS-用户认证服务:面部识别
服务器·前端·华为·移动开发·嵌入式·harmonyos·鸿蒙
小小李程序员14 分钟前
Redis地理数据类型GEO
前端·redis·bootstrap
黑客大佬20 分钟前
安全工具 | 使用Burp Suite的10个小tips
运维·前端·网络·学习·安全·云计算·腾讯云
Leslie_Lei31 分钟前
pdf 转 jpg
pdf
SteveMiller33 分钟前
使用Azure+C#+visual studio开发图像目标检测系统
c#·azure·visual studio
fmc1211041 小时前
Excel文档的读取(3)
java·前端·python
生椰拿铁You1 小时前
CSS——盒子模型
前端·css
雾岛LYC听风1 小时前
3. 轴指令(omron 机器自动化控制器)——>MC_Power&MC_MoveJog
运维·c#·自动化
不知名的小Q1 小时前
如何动态获取路由上的参数
前端·javascript·vue.js
大鸡腿最好吃1 小时前
react&webpack老项目开发环境增加vite
前端·react.js·webpack