当前位置: 代码网 > it编程>编程语言>Asp.net > C#代码实现获取PDF页面尺寸,方向和旋转角度

C#代码实现获取PDF页面尺寸,方向和旋转角度

2026年05月12日 Asp.net 我要评论
在某些情况下,检查 pdf 页面的尺寸、方向和旋转角度是文档质量控制的一部分。例如,在发布或分发文档之前,可能需要验证这些信息,以确保文档中的所有页面都能正确显示。本文将介绍如何在 c# 中获取 pd

在某些情况下,检查 pdf 页面的尺寸、方向和旋转角度是文档质量控制的一部分。例如,在发布或分发文档之前,可能需要验证这些信息,以确保文档中的所有页面都能正确显示。本文将介绍如何在 c# 中获取 pdf 页面尺寸、检测页面方向以及读取页面旋转角度。

环境准备

开始之前,需要在 .net 项目中添加 pdf 处理库的 dll 引用。可以通过下载安装包手动添加,也可以直接使用 nuget 安装相关组件。

pm> install-package spire.pdf 

在 c# 中获取 pdf 页面尺寸

常见的 pdf 处理库通常提供页面宽度和高度属性,可用于获取 pdf 页面的尺寸信息,默认单位一般为 point(磅)。如果需要将默认单位转换为其他单位(如厘米、毫米或英寸),可以借助单位转换工具类完成。下面是具体步骤:

  1. 创建 pdfdocument 实例。
  2. 使用 pdfdocument.loadfromfile() 方法加载 pdf 文件。
  3. 通过 pdfdocument.pages[] 获取指定页面。
  4. 使用页面对象的宽度和高度属性获取 pdf 页面的尺寸信息。
  5. 创建单位转换对象,并通过 convertunits() 方法将 point 单位转换为其他计量单位。
  6. 将页面尺寸信息写入 stringbuilder,最后保存为 txt 文件。

示例代码如下:

using system.text;
using spire.pdf;
using spire.pdf.graphics;

namespace 获取pdf页面尺寸
{
    class program
    {
        static void main(string[] args)
        {
            // 创建 pdfdocument 对象
            pdfdocument pdf = new pdfdocument();

            // 从磁盘加载 pdf 文件
            pdf.loadfromfile("samplepdf.pdf");

            // 获取第一页
            pdfpagebase page = pdf.pages[0];

            // 获取页面宽度和高度(单位:point/磅)
            float pointwidth = page.size.width;
            float pointheight = page.size.height;

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

            // 将单位从 point 转换为 pixel(像素)
            float pixelwidth = unitcvtr.convertunits(pointwidth, pdfgraphicsunit.point, pdfgraphicsunit.pixel);
            float pixelheight = unitcvtr.convertunits(pointheight, pdfgraphicsunit.point, pdfgraphicsunit.pixel);

            // 将单位从 point 转换为 inch(英寸)
            float inchwidth = unitcvtr.convertunits(pointwidth, pdfgraphicsunit.point, pdfgraphicsunit.inch);
            float inchheight = unitcvtr.convertunits(pointheight, pdfgraphicsunit.point, pdfgraphicsunit.inch);

            // 将单位从 point 转换为 centimeter(厘米)
            float centimeterwidth = unitcvtr.convertunits(pointwidth, pdfgraphicsunit.point, pdfgraphicsunit.centimeter);
            float centimeterheight = unitcvtr.convertunits(pointheight, pdfgraphicsunit.point, pdfgraphicsunit.centimeter);

            // 创建 stringbuilder 实例
            stringbuilder content = new stringbuilder();

            // 添加页面尺寸信息
            content.appendline("文件页面尺寸(单位:point)为(宽:" + pointwidth + "pt,高:" + pointheight + "pt)。");
            content.appendline("文件页面尺寸(单位:pixel)为(宽:" + pixelwidth + "pixel,高:" + pixelheight + "pixel)。");
            content.appendline("文件页面尺寸(单位:inch)为(宽:" + inchwidth + "inch,高:" + inchheight + "inch)。");
            content.appendline("文件页面尺寸(单位:centimeter)为(宽:" + centimeterwidth + "cm,高:" + centimeterheight + "cm)。");

            // 保存为 txt 文件
            file.writealltext("getpagesize.txt", content.tostring());
        }
    }
}

在 c# 中检测 pdf 页面方向

检测 pdf 页面方向时,可以通过比较页面的宽度和高度来判断。如果页面宽度大于高度,则页面方向为横向(landscape);否则为纵向(portrait)。具体步骤如下:

  1. 创建 pdfdocument 实例。
  2. 使用 pdfdocument.loadfromfile() 方法加载 pdf 文件。
  3. 通过 pdfdocument.pages[] 获取指定页面。
  4. 使用页面对象的宽度和高度属性获取 pdf 页面的尺寸信息。
  5. 比较页面宽度和高度的值,以判断页面方向。
  6. 使用 console.writeline() 方法输出结果。

示例代码如下:

using spire.pdf;

namespace 检测pdf页面方向
{
    class program
    {
        static void main(string[] args)
        {
            // 创建 pdfdocument 对象
            pdfdocument pdf = new pdfdocument();

            // 从磁盘加载 pdf 文件
            pdf.loadfromfile("samplepdf.pdf");

            // 获取第一页
            pdfpagebase page = pdf.pages[0];

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

            // 比较页面宽度和高度的值
            if (width > height)
            {
                console.writeline("页面方向为横向(landscape)。");
            }
            else
            {
                console.writeline("页面方向为纵向(portrait)。");
            }
        }
    }
}

在 c# 中检测 pdf 页面旋转角度

pdf 页面的旋转角度可以通过 pdfpagebase.rotation 属性获取。具体步骤如下:

  1. 创建 pdfdocument 实例。
  2. 使用 pdfdocument.loadfromfile() 方法加载 pdf 文件。
  3. 通过 pdfdocument.pages[] 获取指定页面。
  4. 使用 pdfpagebase.rotation 属性获取页面的旋转角度,并将结果转换为字符串。
  5. 使用 console.writeline() 方法输出结果。

示例代码如下:

using spire.pdf;

namespace 获取pdf页面旋转角度
{
    class program
    {
        static void main(string[] args)
        {
            // 创建 pdfdocument 对象
            pdfdocument pdf = new pdfdocument();

            // 从磁盘加载 pdf 文件
            pdf.loadfromfile("e:\\pythonpdf\\sample.pdf");

            // 获取第一页
            pdfpagebase page = pdf.pages[0];

            // 获取当前页面的旋转角度
            pdfpagerotateangle rotationangle = page.rotation;
            string rotation = rotationangle.tostring();

            // 输出页面旋转角度信息
            console.writeline("当前页面的旋转角度为:" + rotation);
        }
    }
}

知识扩展

在c#中获取pdf页面的尺寸、方向和旋转角度,可以使用免费开源的 pdfpig 库,它无需安装adobe reader,跨平台且易于使用。以下代码演示了如何读取pdf文件,提取每个页面的宽度、高度、计算方向(横向/纵向)以及读取旋转角度。

1. 安装 pdfpig

在visual studio中打开“包管理器控制台”或使用 .net cli:

dotnet add package pdfpig

install-package pdfpig

2. 代码示例

using system;
using system.io;
using uglytoad.pdfpig;
using uglytoad.pdfpig.content;
class program
{
    static void main(string[] args)
    {
        string filepath = @"c:\test\example.pdf";
        getpdfpageinfo(filepath);
    }
    static void getpdfpageinfo(string pdfpath)
    {
        if (!file.exists(pdfpath))
        {
            console.writeline("文件不存在!");
            return;
        }
        using (pdfdocument document = pdfdocument.open(pdfpath))
        {
            for (int i = 0; i < document.numberofpages; i++)
            {
                page page = document.getpage(i + 1); // 页码从1开始
                // 获取页面尺寸(单位:点,1点 = 1/72英寸)
                double width = page.width;
                double height = page.height;
                // 获取旋转角度(0, 90, 180, 270)
                int rotation = page.rotation.value;  // rotation 是可空类型,一般为90度的整数倍
                // 计算方向(基于原始宽高,未考虑旋转)
                string orientation = (width > height) ? "横向 (landscape)" : "纵向 (portrait)";
                // 输出信息
                console.writeline($"--- 第 {i + 1} 页 ---");
                console.writeline($"原始尺寸: {width} x {height} 点 (约 {width/72:f2} x {height/72:f2} 英寸)");
                console.writeline($"方向: {orientation}");
                console.writeline($"旋转角度: {rotation} 度");
                console.writeline();
                // 如果需要考虑旋转后的实际显示方向,可以结合旋转角度判断
                bool isrotated = (rotation % 180 != 0);
                string effectiveorientation = (isrotated ? (height > width ? "横向" : "纵向") : orientation);
                console.writeline($"考虑旋转后的实际显示方向: {effectiveorientation}\n");
            }
        }
    }
}

3. 输出示例

--- 第 1 页 ---
原始尺寸: 595.0 x 842.0 点 (约 8.26 x 11.69 英寸)
方向: 纵向 (portrait)
旋转角度: 0 度

--- 第 2 页 ---
原始尺寸: 842.0 x 595.0 点 (约 11.69 x 8.26 英寸)
方向: 横向 (landscape)
旋转角度: 0 度

--- 第 3 页 ---
原始尺寸: 595.0 x 842.0 点 (约 8.26 x 11.69 英寸)
方向: 纵向 (portrait)
旋转角度: 90 度
考虑旋转后的实际显示方向: 横向

4. 关键说明

  • 尺寸单位:pdfpig 返回的宽度和高度以“点”(point)为单位(1点 = 1/72英寸)。
  • 旋转角度:通过 page.rotation.value 获取,值为 0、90、180 或 270。rotation 是可空类型,若不存在则默认 0。
  • 方向判断:原始方向由宽度 > 高度判定,但旋转 90° 或 270° 后实际显示的方向会互换。示例中给出了计算有效方向的方法。
  • 其他页面框:pdf 除了 /mediabox 还有 /cropbox/trimbox 等,page.width 和 page.height 默认返回的是 /cropbox(如果存在),否则返回 /mediabox。如需获取特定框,可通过 page.getmediabox() 等方法。

总结

本文介绍了如何在 c# 中获取 pdf 页面的尺寸、方向以及旋转角度。通过读取页面的宽度、高度和旋转属性,开发者可以快速分析 pdf 页面布局,并根据实际需求进行文档检查、页面处理或自动化管理。

此外,文章还演示了如何将页面尺寸从默认的 point(磅)单位转换为像素、英寸和厘米等常用单位,以及如何通过简单的宽高比较判断页面是横向还是纵向。借助这些方法,可以更方便地完成 pdf 文档的质量检测与页面信息提取工作。

以上就是c#代码实现获取pdf页面尺寸,方向和旋转角度的详细内容,更多关于c#获取pdf页面信息的资料请关注代码网其它相关文章!

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2026  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com