溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

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

發(fā)布時(shí)間:2020-06-23 05:38:58 來源:網(wǎng)絡(luò) 閱讀:843 作者:E_iceblue 欄目:編程語言

引言

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

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

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

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

【示例1】繪制基本圖形

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

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

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

步驟2:繪制圓形、矩形、線段、三角形

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

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

            //繪入線段
            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:保存文檔

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

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

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

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

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

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

            //保存并打開文檔
            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類,在頂點(diǎ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]);

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

            //實(shí)例化畫筆和畫刷1、畫刷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í)例化畫刷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í)例化畫刷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í)例化畫刷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);

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

自定義圖形添加效果:

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

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

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

            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)建畫刷

            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)注明出處!
(本文完)

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

pdf
AI