通過(guò)C#實(shí)現(xiàn)獲取PDF頁(yè)面大小、方向和旋轉(zhuǎn)角度
免費(fèi)庫(kù) Free Spire.PDF for .NET 提供了接口來(lái)獲取PDF頁(yè)面信息,我們可以從官網(wǎng)下載產(chǎn)品包后手動(dòng)添加引用,或者直接通過(guò)NuGet安裝。
PM> Install-Package FreeSpire.PDF
輸入文檔如圖:
C# 讀取PDF頁(yè)面大?。▽挾?、高度)
免費(fèi)Spire.PDF提供了 PdfPageBase.Size.Width
和 PdfPageBase.Size.Height
屬性來(lái)獲取指定PDF頁(yè)面的寬度和高度。
獲取到的值默認(rèn)單位為磅(point),如果想要將其轉(zhuǎn)換為厘米、毫米等常見(jiàn)單位,可以通過(guò) PdfUnitConvertor
類的 ConvertUnits(float value, PdfGraphicsUnit from, PdfGraphicsUnit to)
方法進(jìn)行轉(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"); //獲取第一頁(yè) PdfPageBase page = pdf.Pages[0]; //獲取頁(yè)面寬度和高度(默認(rèn)單位為point) float pointWidth = page.Size.Width; float pointHeight = page.Size.Height; //創(chuàng)建PdfUnitConvertor對(duì)象用于轉(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頁(yè)面寬高度信息 Console.WriteLine("該P(yáng)DF頁(yè)面大小為(以磅為單位): 寬度 " + pointWidth + "pt, 高度 " + pointHeight + "pt"); Console.WriteLine("該P(yáng)DF頁(yè)面大小為(以厘米為單位): 寬度 " + centimeterWidth + "cm, 高度 " + centimeterHeight + "cm"); Console.WriteLine("該P(yáng)DF頁(yè)面大小為(以毫米為單位): 寬度 " + millimeterWidth + "mm, 高度 " + millimeterHeight + "mm"); } } }
輸出結(jié)果:
C# 判斷PDF頁(yè)面方向
頁(yè)面的方向通常以橫向或縱向表示。要判斷指定PDF頁(yè)面的方向:
- 先獲取頁(yè)面寬度和高度
- 再比較這兩個(gè)值。(如果寬度大于高度,則頁(yè)面方向?yàn)闄M向,反之則為縱向。)
示例代碼如下:
using Spire.Pdf; using System; namespace GetPDFPageOrientation { class Program { static void Main(string[] args) { //加載PDF文檔 PdfDocument pdf = new PdfDocument(); pdf.LoadFromFile("示例.pdf"); //獲取第一頁(yè) PdfPageBase page = pdf.Pages[0]; //獲取頁(yè)面寬度和高度 float width = page.Size.Width; float height = page.Size.Height; //通過(guò)比較頁(yè)面寬度和高度來(lái)判斷頁(yè)面方向 if (width > height) { Console.WriteLine("當(dāng)前頁(yè)面方向?yàn)闄M向。"); } else { Console.WriteLine("當(dāng)前頁(yè)面方向?yàn)榭v向。"); } } } }
輸出結(jié)果:
C# 檢測(cè)PDF頁(yè)面旋轉(zhuǎn)角度
使用 PdfPageBase.Rotation
可以獲取指定PDF頁(yè)面的旋轉(zhuǎn)角度。如果為 0,則表示頁(yè)面保持原來(lái)的方向。
示例代碼如下:
using Spire.Pdf; using System; namespace GetPDFPageOrientation { class Program { static void Main(string[] args) { //加載PDF文檔 PdfDocument pdf = new PdfDocument(); pdf.LoadFromFile("示例.pdf"); //獲取第一頁(yè) PdfPageBase page = pdf.Pages[0]; //獲取頁(yè)面的旋轉(zhuǎn)角度并輸出結(jié)果 PdfPageRotateAngle rotationAngle = page.Rotation; string rotation = rotationAngle.ToString(); Console.WriteLine("當(dāng)前頁(yè)面旋轉(zhuǎn)角度為: " + rotation); } } }
輸出結(jié)果:
以上就是通過(guò)C#實(shí)現(xiàn)獲取PDF頁(yè)面大小、方向和旋轉(zhuǎn)角度的詳細(xì)內(nèi)容,更多關(guān)于C#獲取PDF頁(yè)面屬性的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
C#?Sqlite數(shù)據(jù)庫(kù)的搭建及使用技巧
這篇文章主要介紹了C#?Sqlite數(shù)據(jù)庫(kù)的搭建及使用技巧,文章圍繞主題展開(kāi)詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的朋友可以參考一下2022-08-08C#使用Lazy實(shí)現(xiàn)延遲加載的方法示例
在C#中,Lazy< T> 類是一個(gè)非常有用的工具,它可以用于延遲加載值,在本文中,我們將詳細(xì)介紹 Lazy< T> 的實(shí)現(xiàn)機(jī)制和用法,并提供一些示例來(lái)展示它的優(yōu)勢(shì),文中通過(guò)代碼示例講解的非常詳細(xì),需要的朋友可以參考下2024-06-06C#WinFrom導(dǎo)出Excel過(guò)程解析
這篇文章主要介紹了C#WinFrom導(dǎo)出Excel過(guò)程解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-11-11C#以流方式讀socket超時(shí)設(shè)置的實(shí)例
這篇文章主要為大家詳細(xì)介紹了C#以流方式讀socket超時(shí)設(shè)置的實(shí)例,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-03-03基于C#?實(shí)現(xiàn)?OPC?DA?Server的問(wèn)題小結(jié)
這篇文章主要介紹了基于C#?實(shí)現(xiàn)?OPC?DA?Server的相關(guān)知識(shí),關(guān)于C#怎么編寫(xiě)一個(gè)進(jìn)程外的DCOM組件,這里先不做介紹了,這里主要介紹下OPC?DA?Server?的第一個(gè)接口,感興趣的朋友跟隨小編一起看看吧2024-04-04C#實(shí)現(xiàn)簡(jiǎn)單串口通訊實(shí)例
這篇文章主要為大家詳細(xì)介紹了C#實(shí)現(xiàn)簡(jiǎn)單串口通訊的方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-02-02