C#代码实现获取PDF页面尺寸,方向和旋转角度
在某些情况下,检查 PDF 页面的尺寸、方向和旋转角度是文档质量控制的一部分。例如,在发布或分发文档之前,可能需要验证这些信息,以确保文档中的所有页面都能正确显示。本文将介绍如何在 C# 中获取 PDF 页面尺寸、检测页面方向以及读取页面旋转角度。
环境准备
开始之前,需要在 .NET 项目中添加 PDF 处理库的 DLL 引用。可以通过下载安装包手动添加,也可以直接使用 NuGet 安装相关组件。
PM> Install-Package Spire.PDF
在 C# 中获取 PDF 页面尺寸
常见的 PDF 处理库通常提供页面宽度和高度属性,可用于获取 PDF 页面的尺寸信息,默认单位一般为 point(磅)。如果需要将默认单位转换为其他单位(如厘米、毫米或英寸),可以借助单位转换工具类完成。下面是具体步骤:
- 创建
PdfDocument实例。 - 使用
PdfDocument.LoadFromFile()方法加载 PDF 文件。 - 通过
PdfDocument.Pages[]获取指定页面。 - 使用页面对象的宽度和高度属性获取 PDF 页面的尺寸信息。
- 创建单位转换对象,并通过
ConvertUnits()方法将 point 单位转换为其他计量单位。 - 将页面尺寸信息写入
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)。具体步骤如下:
- 创建
PdfDocument实例。 - 使用
PdfDocument.LoadFromFile()方法加载 PDF 文件。 - 通过
PdfDocument.Pages[]获取指定页面。 - 使用页面对象的宽度和高度属性获取 PDF 页面的尺寸信息。
- 比较页面宽度和高度的值,以判断页面方向。
- 使用
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 属性获取。具体步骤如下:
- 创建
PdfDocument实例。 - 使用
PdfDocument.LoadFromFile()方法加载 PDF 文件。 - 通过
PdfDocument.Pages[]获取指定页面。 - 使用
PdfPageBase.Rotation属性获取页面的旋转角度,并将结果转换为字符串。 - 使用
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页面信息的资料请关注脚本之家其它相关文章!
相关文章
C#实现Json转DataTable并导出Excel的方法示例
这篇文章主要介绍了C#实现Json转DataTable并导出Excel的方法,结合实例形式总结分析了Json转换DataTable,以及DataTable导出Excel相关操作技巧,需要的朋友可以参考下2019-02-02


最新评论