在某些情况下,检查 PDF 页面的尺寸、方向和旋转角度是文档质量控制的一部分。例如,在发布或分发文档之前,可能需要验证这些信息,以确保文档中的所有页面都能正确显示。本文将介绍如何在 C# 中获取 PDF 页面尺寸、检测页面方向以及读取页面旋转角度。
环境准备
开始之前,需要在 .NET 项目中添加 PDF 处理库的 DLL 引用。可以通过下载安装包手动添加,也可以直接使用 NuGet 安装相关组件。
cs
PM> Install-Package Spire.PDF
在 C# 中获取 PDF 页面尺寸
常见的 PDF 处理库通常提供页面宽度和高度属性,可用于获取 PDF 页面的尺寸信息,默认单位一般为 point(磅)。如果需要将默认单位转换为其他单位(如厘米、毫米或英寸),可以借助单位转换工具类完成。下面是具体步骤:
- 创建
PdfDocument实例。 - 使用
PdfDocument.LoadFromFile()方法加载 PDF 文件。 - 通过
PdfDocument.Pages[]获取指定页面。 - 使用页面对象的宽度和高度属性获取 PDF 页面的尺寸信息。
- 创建单位转换对象,并通过
ConvertUnits()方法将 point 单位转换为其他计量单位。 - 将页面尺寸信息写入
StringBuilder,最后保存为 TXT 文件。
示例代码如下:
cs
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()方法输出结果。
示例代码如下:
cs
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()方法输出结果。
示例代码如下:
cs
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 页面的尺寸、方向以及旋转角度。通过读取页面的宽度、高度和旋转属性,开发者可以快速分析 PDF 页面布局,并根据实际需求进行文档检查、页面处理或自动化管理。
此外,文章还演示了如何将页面尺寸从默认的 point(磅)单位转换为像素、英寸和厘米等常用单位,以及如何通过简单的宽高比较判断页面是横向还是纵向。借助这些方法,可以更方便地完成 PDF 文档的质量检测与页面信息提取工作。