C#繪制PDF圖形——基本圖形、自定義圖形、色彩透明度

引言

在PDF中我們可以通過(guò)C#程序代碼來(lái)添加非常豐富的元素來(lái)呈現(xiàn)我們想要表達(dá)的內(nèi)容,如繪制表格、文字,添加圖形、圖像等等。在本篇文章中,我將介紹如何在PDF中繪制圖形,并設(shè)置圖形屬性的操作。

專(zhuān)注于為中小企業(yè)提供成都網(wǎng)站設(shè)計(jì)、成都網(wǎng)站建設(shè)、外貿(mào)網(wǎng)站建設(shè)服務(wù),電腦端+手機(jī)端+微信端的三站合一,更高效的管理,為中小企業(yè)北碚免費(fèi)做網(wǎng)站提供優(yōu)質(zhì)的服務(wù)。我們立足成都,凝聚了一批互聯(lián)網(wǎng)行業(yè)人才,有力地推動(dòng)了1000+企業(yè)的穩(wěn)健成長(zhǎng),幫助中小企業(yè)通過(guò)網(wǎng)站建設(shè)實(shí)現(xiàn)規(guī)模擴(kuò)充和轉(zhuǎn)變。

文章中將分以下要點(diǎn)進(jìn)行介紹:

  1. 繪制基本圖形(線(xiàn)條、橢圓、圓形、矩形、三角形)
  2. 繪制自定義圖形
  3. 繪制圖形并設(shè)置圖形透明度
    所需工具:Spire.PDF for .NET 4.0

提示:安裝后,直接引用安裝路徑下Bin文件夾中的dll文件到項(xiàng)目程序中即可。

【示例1】繪制基本圖形

步驟1:新建一個(gè)PDF文檔,添加頁(yè),添加畫(huà)筆、畫(huà)刷

           //新建一個(gè)PDF文檔,添加頁(yè)
            PdfDocument doc = new PdfDocument();
            PdfPageBase page = doc.Pages.Add();

            //設(shè)置畫(huà)筆和畫(huà)刷
            PdfPen pen = new PdfPen(PdfBrushes.Black, 1f);
            PdfBrush brush2 = PdfBrushes.RosyBrown;
            PdfBrush brush3 = PdfBrushes.Goldenrod;

步驟2:繪制圓形、矩形、線(xiàn)段、三角形

            //繪入矩形(此處通過(guò)設(shè)置值來(lái)繪制成正方形)
            page.Canvas.DrawRectangle(pen, brush2, new Rectangle(new Point(50, 50), new Size(60, 60)));

            //繪入橢圓(此處通過(guò)設(shè)置值來(lái)繪制成圓形)
            page.Canvas.DrawEllipse(pen, brush3, 210, 50, 60, 60);

            //繪入線(xiàn)段
            page.Canvas.DrawLine(pen, new PointF(50, 115), new PointF(270, 115));

            //繪入多邊形(此處繪制成三角形)
            PointF p1 = new PointF(130, 172);
            PointF p2 = new PointF(160, 120);
            PointF p3 = new PointF(190, 172);
            PointF[] points = new PointF[] { p1, p2, p3 };
            page.Canvas.DrawPolygon(pen, points);

步驟3:保存文檔

            //保存并打開(kāi)文檔
            doc.SaveToFile("基本圖形.pdf");
            System.Diagnostics.Process.Start("基本圖形.pdf");

圖形添加效果:
C# 繪制PDF圖形——基本圖形、自定義圖形、色彩透明度

【示例2】繪制自定義圖形

步驟1:創(chuàng)建pdf文檔,調(diào)用方法DrawStar()繪制自定義圖形,并保存

            //新建一個(gè)PDF文檔,添加頁(yè)
            PdfDocument doc = new PdfDocument();
            PdfPageBase page = doc.Pages.Add();

            //調(diào)用DrawStar()方法繪入五角星圖形
            DrawStar(page);

            //保存并打開(kāi)文檔
            doc.SaveToFile("自定義圖形.pdf");
            System.Diagnostics.Process.Start("自定義圖形.pdf");

步驟2:自定義DrawStar()方法繪制幾個(gè)不同樣式的五角星

private static void DrawStar(PdfPageBase page)
        {
            //設(shè)置五角星的5個(gè)頂點(diǎn)坐標(biāo)
            PointF[] points = new PointF[5];
            for (int i = 0; i < points.Length; i++)
            {
                float x = (float)Math.Cos(i * 2 * Math.PI / 5);
                float y = (float)Math.Sin(i * 2 * Math.PI / 5);
                points[i] = new PointF(x, y);
            }

            //創(chuàng)建PdfPath類(lèi),在頂點(diǎn)之間添加線(xiàn)段組成五角星
            PdfPath path = new PdfPath();
            path.AddLine(points[2], points[0]);
            path.AddLine(points[0], points[3]);
            path.AddLine(points[3], points[1]);
            path.AddLine(points[1], points[4]);
            path.AddLine(points[4], points[2]);

            //保存畫(huà)布狀態(tài)
            PdfGraphicsState state = page.Canvas.Save();

            //實(shí)例化畫(huà)筆和畫(huà)刷1、畫(huà)刷2
            PdfPen pen = new PdfPen(Color.DeepSkyBlue, 0.02f);
            PdfBrush brush2 = new PdfSolidBrush(Color.PaleGreen);
            PdfBrush brush3 = new PdfSolidBrush(Color.Bisque);
            //將坐標(biāo)放大40倍
            page.Canvas.ScaleTransform(40f, 40f);

            //平移坐標(biāo)
            page.Canvas.TranslateTransform(1f, 1.5f);

            //繪入第一個(gè)五角星
            page.Canvas.DrawPath(pen, path);

            //平移坐標(biāo)并在新的位置繪入第二個(gè)五角星
            page.Canvas.TranslateTransform(2f, 0f);
            path.FillMode = PdfFillMode.Alternate;
            page.Canvas.DrawPath(pen, brush2, path);

            //平移坐標(biāo)并在新的位置繪入第三個(gè)五角星
            page.Canvas.TranslateTransform(2f, 0f);
            path.FillMode = PdfFillMode.Winding;
            page.Canvas.DrawPath(pen, brush3, path);

            //實(shí)例化畫(huà)刷3,平移坐標(biāo)并在新的位置繪入第四個(gè)五角星
            PdfLinearGradientBrush brush4
                = new PdfLinearGradientBrush(new PointF(-2, 0), new PointF(2, 0), Color.OrangeRed, Color.Yellow);
            page.Canvas.TranslateTransform(-4f, 2f);
            path.FillMode = PdfFillMode.Alternate;
            page.Canvas.DrawPath(pen, brush4, path);

            //實(shí)例化畫(huà)刷4,平移坐標(biāo)并在新的位置繪入第五個(gè)五角星
            PdfRadialGradientBrush brush5
                = new PdfRadialGradientBrush(new PointF(0f, 0f), 0f, new PointF(0f, 0f), 1f, Color.Orchid, Color.LightBlue);
            page.Canvas.TranslateTransform(2f, 0f);
            path.FillMode = PdfFillMode.Winding;
            page.Canvas.DrawPath(pen, brush5, path);

            //實(shí)例化畫(huà)刷5,平移坐標(biāo)并在新的位置繪入第六個(gè)五角星
            PdfTilingBrush brush6 = new PdfTilingBrush(new RectangleF(0, 0, 4f, 4f));
            brush6.Graphics.DrawRectangle(brush4, 0, 0, 4f, 4f);
            page.Canvas.TranslateTransform(2f, 0f);
            path.FillMode = PdfFillMode.Winding;
            page.Canvas.DrawPath(pen, brush6, path);

            //再次保存畫(huà)布狀態(tài)
            page.Canvas.Restore(state);
        }

自定義圖形添加效果:

C# 繪制PDF圖形——基本圖形、自定義圖形、色彩透明度

【示例3】設(shè)置色彩透明度

步驟1:新建一個(gè)PDF文檔,添加頁(yè)

            PdfDocument doc = new PdfDocument();
            PdfPageBase page = doc.Pages.Add();

步驟2:初始化一個(gè)PdfSeparationColorSpace的對(duì)象,用于創(chuàng)建基本色,并將基本色透明度設(shè)置為1

             PdfSeparationColorSpace cs1 = new PdfSeparationColorSpace("MySpotColor", Color.DarkGreen);
            PdfSeparationColorSpace cs2 = new PdfSeparationColorSpace("MySpotColor", Color.Yellow);
            PdfSeparationColorSpace cs3 = new PdfSeparationColorSpace("MySpotColor", Color.DeepPink);
            PdfSeparationColor color1 = new PdfSeparationColor(cs1, 1f);
            PdfSeparationColor color2 = new PdfSeparationColor(cs2, 1f);
            PdfSeparationColor color3 = new PdfSeparationColor(cs3, 1f);

步驟3:根據(jù)顏色創(chuàng)建畫(huà)刷

            PdfSolidBrush brush2 = new PdfSolidBrush(color1);
            PdfSolidBrush brush3 = new PdfSolidBrush(color2);
            PdfSolidBrush brush4 = new PdfSolidBrush(color3);

步驟4:繪入圖形及文字,應(yīng)用色彩透明度到圖形

            //繪入圖形及文字并著色
            page.Canvas.DrawPie(brush2, 10, 30, 60, 60, 360, 360);
            page.Canvas.DrawPie(brush3, 10, 120, 60, 60, 360, 360);
            page.Canvas.DrawPie(brush4, 10, 210, 60, 60, 360, 360);
            page.Canvas.DrawString("透明度=1.0", new PdfTrueTypeFont(new Font("Arial Unicode MS", 10f), true), brush2, new PointF(16, 100));

            //將基本色透明度設(shè)置為0.5,并繪入圖片及文字
            color1 = new PdfSeparationColor(cs1, 0.5f);
            brush2 = new PdfSolidBrush(color1);
            page.Canvas.DrawPie(brush2, 80, 30, 60, 60, 360, 360);
            color2 = new PdfSeparationColor(cs2, 0.5f);
            brush3 = new PdfSolidBrush(color2);
            page.Canvas.DrawPie(brush3, 80, 120, 60, 60, 360, 360);
            color3 = new PdfSeparationColor(cs3, 0.5f);
            brush4 = new PdfSolidBrush(color3);
            page.Canvas.DrawPie(brush4, 80, 210, 60, 60, 360, 360);

            page.Canvas.DrawString("透明度=0.5", new PdfTrueTypeFont(new Font("Arial Unicode MS", 10f), true), brush2, new PointF(86, 100));

            //將基本色透明度設(shè)置為0.25,并繪入圖片及文字
            color1 = new PdfSeparationColor(cs1, 0.25f);
            brush2 = new PdfSolidBrush(color1);
            page.Canvas.DrawPie(brush2, 150, 30, 60, 60, 360, 360);
            color2 = new PdfSeparationColor(cs2, 0.25f);
            brush3 = new PdfSolidBrush(color2);
            page.Canvas.DrawPie(brush3, 150, 120, 60, 60, 360, 360);
            color3 = new PdfSeparationColor(cs3, 0.25f);
            brush4 = new PdfSolidBrush(color3);
            page.Canvas.DrawPie(brush4, 150, 210, 60, 60, 360, 360);
            page.Canvas.DrawString("透明度=0.25", new PdfTrueTypeFont(new Font("Arial Unicode MS", 10f), true), brush2, new PointF(156, 100));

步驟5:保存文檔

            doc.SaveToFile("設(shè)置透明度.pdf");
            System.Diagnostics.Process.Start("設(shè)置透明度.pdf");

色彩透明度實(shí)現(xiàn)效果:
C# 繪制PDF圖形——基本圖形、自定義圖形、色彩透明度

以上是關(guān)于C#繪制PDF圖形的全部?jī)?nèi)容,如需轉(zhuǎn)載,請(qǐng)注明出處!
(本文完)

網(wǎng)站題目:C#繪制PDF圖形——基本圖形、自定義圖形、色彩透明度
網(wǎng)站網(wǎng)址:http://muchs.cn/article20/jpgojo.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站設(shè)計(jì)公司面包屑導(dǎo)航、自適應(yīng)網(wǎng)站云服務(wù)器、品牌網(wǎng)站建設(shè)、電子商務(wù)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話(huà):028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

手機(jī)網(wǎng)站建設(shè)