C# 获取 PDF 文件宽高:处理页面旋转问题
这可能是因为 PDF 文件的页面旋转了 90 度。在 C# 中获取 PDF 文件的宽和高时,您可以使用以下代码来获取正确的值:
using (PdfDocument pdf = PdfDocument.Load(filePath))
{
PdfPage page = pdf.Pages[0]; //假设您要获取第一页的宽和高
int width = page.Width;
int height = page.Height;
if (page.Rotation == PdfPageRotation.Rotate90 || page.Rotation == PdfPageRotation.Rotate270)
{
//如果页面旋转了 90 度或 270 度,则交换宽和高的值
int temp = width;
width = height;
height = temp;
}
Console.WriteLine('Width: ' + width);
Console.WriteLine('Height: ' + height);
}
在上面的代码中,我们首先加载 PDF 文件并获取第一页。然后,我们获取页面的宽和高,并检查页面是否旋转了 90 度或 270 度。如果是这样,我们就交换宽和高的值,以便获取正确的值。最后,我们输出宽和高的值。
原文地址: https://www.cveoy.top/t/topic/oKPb 著作权归作者所有。请勿转载和采集!