最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

通過C#實(shí)現(xiàn)獲取PDF頁面大小、方向和旋轉(zhuǎn)角度

 更新時間:2024年08月25日 10:41:18   作者:Eiceblue  
在處理PDF文件時,了解頁面的大小、方向和旋轉(zhuǎn)角度等信息對于PDF的顯示、打印和布局設(shè)計至關(guān)重要,本文將介紹如何使用免費(fèi).NET?庫通過C#來讀取PDF頁面的這些屬性,需要的朋友可以參考下

免費(fèi)庫 Free Spire.PDF for .NET 提供了接口來獲取PDF頁面信息,我們可以從官網(wǎng)下載產(chǎn)品包后手動添加引用,或者直接通過NuGet安裝。

PM> Install-Package FreeSpire.PDF

輸入文檔如圖:

PDF頁面

C# 讀取PDF頁面大?。▽挾取⒏叨龋?/h2>

免費(fèi)Spire.PDF提供了 PdfPageBase.Size.WidthPdfPageBase.Size.Height 屬性來獲取指定PDF頁面的寬度和高度。

獲取到的值默認(rèn)單位為磅(point),如果想要將其轉(zhuǎn)換為厘米、毫米等常見單位,可以通過 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");

            //獲取第一頁
            PdfPageBase page = pdf.Pages[0];

            //獲取頁面寬度和高度(默認(rèn)單位為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("該P(yáng)DF頁面大小為(以磅為單位): 寬度 " + pointWidth + "pt, 高度 " + pointHeight + "pt");
            Console.WriteLine("該P(yáng)DF頁面大小為(以厘米為單位): 寬度 " + centimeterWidth + "cm, 高度 " + centimeterHeight + "cm");
            Console.WriteLine("該P(yáng)DF頁面大小為(以毫米為單位): 寬度 " + millimeterWidth + "mm, 高度 " + millimeterHeight + "mm");

        }
    }
}

輸出結(jié)果:

讀取PDF頁面寬、高

C# 判斷PDF頁面方向

頁面的方向通常以橫向或縱向表示。要判斷指定PDF頁面的方向:

  • 先獲取頁面寬度和高度
  • 再比較這兩個值。(如果寬度大于高度,則頁面方向?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");

            //獲取第一頁
            PdfPageBase page = pdf.Pages[0];

            //獲取頁面寬度和高度
            float width = page.Size.Width;
            float height = page.Size.Height;

            //通過比較頁面寬度和高度來判斷頁面方向
            if (width > height)
            {
                Console.WriteLine("當(dāng)前頁面方向?yàn)闄M向。");
            }

            else
            {
                Console.WriteLine("當(dāng)前頁面方向?yàn)榭v向。");
            }
        }
    }
}

輸出結(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("當(dāng)前頁面旋轉(zhuǎn)角度為: " + rotation);
        }
    }
}

輸出結(jié)果:

檢測頁面旋轉(zhuǎn)

以上就是通過C#實(shí)現(xiàn)獲取PDF頁面大小、方向和旋轉(zhuǎn)角度的詳細(xì)內(nèi)容,更多關(guān)于C#獲取PDF頁面屬性的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

最新評論

托克托县| 贞丰县| 东乡族自治县| 河北省| 盘锦市| 河曲县| 河曲县| 沧州市| 达州市| 新安县| 昆明市| 乡城县| 包头市| 罗甸县| 湖口县| 闵行区| 榕江县| 泰来县| 都江堰市| 比如县| 汝南县| 友谊县| 开远市| 环江| 株洲市| 津南区| 霸州市| 南昌县| 来宾市| 海门市| 虎林市| 农安县| 罗城| 木里| 永吉县| 奎屯市| 顺义区| 霍山县| 南溪县| 巴东县| 衡山县|