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);
        }
    }
}

输出结果:


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

相关推荐
脚步的影子26 分钟前
Redis 入门 - C#|.NET Core客户端库六种选择
redis·c#·.netcore
Achanss29 分钟前
C#学习笔记 .NET Core使用注意事项
笔记·学习·c#
吾门6 小时前
.NET编程——利用C#调用海康机器人工业相机SDK实现回调取图与软触发取图【含免费源码】
c#·.net·相机
初级代码游戏7 小时前
C# winforms 窗口延迟初始化 splash 定时器
开发语言·c#
loosenivy7 小时前
身份证实名认证接口如何用C#实现
开发语言·c#·php
小故渊7 小时前
C#事件(Event)
开发语言·c#
王维志11 小时前
C# 双向链表介绍
unity·c#
GZM88888811 小时前
深入解析Flink SQL:基本概念与高级应用
sql·flink·c#
Crazy Struggle12 小时前
C# 开源教程带你轻松掌握数据结构与算法
c#·hello 算法
zls36536513 小时前
C# WPF上位机与西门子PLC通信实现实例解析
开发语言·c#·wpf