通過C#實現(xiàn)獲取PDF頁面大小、方向和旋轉(zhuǎn)角度
免費庫 Free Spire.PDF for .NET 提供了接口來獲取PDF頁面信息,我們可以從官網(wǎng)下載產(chǎn)品包后手動添加引用,或者直接通過NuGet安裝。
PM> Install-Package FreeSpire.PDF
輸入文檔如圖:
C# 讀取PDF頁面大小(寬度、高度)
免費Spire.PDF提供了 PdfPageBase.Size.Width
和 PdfPageBase.Size.Height
屬性來獲取指定PDF頁面的寬度和高度。
獲取到的值默認單位為磅(point),如果想要將其轉(zhuǎn)換為厘米、毫米等常見單位,可以通過 PdfUnitConvertor
類的 ConvertUnits(float value, PdfGraphicsUnit from, PdfGraphicsUnit to)
方法進行轉(zhuǎn)換。
示例代碼如下:
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; //創(chuàng)建PdfUnitConvertor對象用于轉(zhuǎn)換單位 PdfUnitConvertor unitCvtr = new PdfUnitConvertor(); //將單位從磅(point)轉(zhuǎn)換為厘米 float centimeterWidth = unitCvtr.ConvertUnits(pointWidth, PdfGraphicsUnit.Point, PdfGraphicsUnit.Centimeter); float centimeterHeight = unitCvtr.ConvertUnits(pointHeight, PdfGraphicsUnit.Point, PdfGraphicsUnit.Centimeter); //將單位從磅(point)轉(zhuǎn)換為毫米 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"); } } }
輸出結(jié)果:
C# 判斷PDF頁面方向
頁面的方向通常以橫向或縱向表示。要判斷指定PDF頁面的方向:
- 先獲取頁面寬度和高度
- 再比較這兩個值。(如果寬度大于高度,則頁面方向為橫向,反之則為縱向。)
示例代碼如下:
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("當前頁面方向為縱向。"); } } } }
輸出結(jié)果:
C# 檢測PDF頁面旋轉(zhuǎn)角度
使用 PdfPageBase.Rotation
可以獲取指定PDF頁面的旋轉(zhuǎn)角度。如果為 0,則表示頁面保持原來的方向。
示例代碼如下:
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]; //獲取頁面的旋轉(zhuǎn)角度并輸出結(jié)果 PdfPageRotateAngle rotationAngle = page.Rotation; string rotation = rotationAngle.ToString(); Console.WriteLine("當前頁面旋轉(zhuǎn)角度為: " + rotation); } } }
輸出結(jié)果:
以上就是通過C#實現(xiàn)獲取PDF頁面大小、方向和旋轉(zhuǎn)角度的詳細內(nèi)容,更多關(guān)于C#獲取PDF頁面屬性的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
基于C#?實現(xiàn)?OPC?DA?Server的問題小結(jié)
這篇文章主要介紹了基于C#?實現(xiàn)?OPC?DA?Server的相關(guān)知識,關(guān)于C#怎么編寫一個進程外的DCOM組件,這里先不做介紹了,這里主要介紹下OPC?DA?Server?的第一個接口,感興趣的朋友跟隨小編一起看看吧2024-04-04