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

输出结果:


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

相关推荐
每日出拳老爷子22 分钟前
[C#] 使用TextBox换行失败的原因与解决方案:换用RichTextBox的实战经验
开发语言·c#
程序猿多布9 小时前
C# 值拷贝、引用拷贝、浅拷贝、深拷贝
c#
阿蒙Amon10 小时前
C#随机数生成全面详解:从基础到高级应用
服务器·网络·c#
开开心心_Every10 小时前
便捷的电脑自动关机辅助工具
开发语言·人工智能·pdf·c#·电脑·音视频·sublime text
我要打打代码13 小时前
C#Winform窗体显示模糊的问题
c#
阿蒙Amon13 小时前
C#正则表达式全面详解:从基础到高级应用
开发语言·正则表达式·c#
水果里面有苹果15 小时前
19-C#静态方法与静态类
java·开发语言·c#
吃着火锅x唱着歌15 小时前
LeetCode 3306.元音辅音字符串计数2
算法·leetcode·c#
格林威20 小时前
Baumer工业相机堡盟工业相机如何通过DeepOCR模型识别判断数值和字符串的范围和相似度(C#)
开发语言·人工智能·python·数码相机·计算机视觉·c#·视觉检测
向宇it1 天前
Unity Universal Render Pipeline/Lit光照材质介绍
游戏·unity·c#·游戏引擎·材质