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

输出结果:


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

相关推荐
人工智能训练师1 小时前
Ubuntu22.04如何安装新版本的Node.js和npm
linux·运维·前端·人工智能·ubuntu·npm·node.js
Seveny072 小时前
pnpm相对于npm,yarn的优势
前端·npm·node.js
yddddddy3 小时前
css的基本知识
前端·css
昔人'3 小时前
css `lh`单位
前端·css
未来之窗软件服务4 小时前
幽冥大陆(二)RDIFSDK 接口文档:布草洗涤厂高效运营的技术桥梁C#—东方仙盟
开发语言·c#·rdif·仙盟创梦ide·东方仙盟
1uther4 小时前
Unity核心概念⑨:Screen
开发语言·游戏·unity·c#·游戏引擎
Nan_Shu_6144 小时前
Web前端面试题(2)
前端
知识分享小能手4 小时前
React学习教程,从入门到精通,React 组件核心语法知识点详解(类组件体系)(19)
前端·javascript·vue.js·学习·react.js·react·anti-design-vue
阿幸软件杂货间5 小时前
Office转PDF转换器v1.0.py
开发语言·pdf·c#
蚂蚁RichLab前端团队5 小时前
🚀🚀🚀 RichLab - 花呗前端团队招贤纳士 - 【转岗/内推/社招】
前端·javascript·人工智能