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


引言

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

 

文章中將分以下要點進行介紹:

1. 繪制基本圖形(線條、橢圓、圓形、矩形、三角形)

2. 繪制自定義圖形

3. 繪制圖形並設置圖形透明度

 

所需工具Spire.PDF for .NET 4.0

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

【示例1】繪制基本圖形

C#

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

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

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

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

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

            //繪入橢圓(此處通過設置值來繪制成圓形)
            page.Canvas.DrawEllipse(pen, brush2, 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");

 

全部代碼

using Spire.Pdf;
using Spire.Pdf.Graphics;
using System.Drawing;

namespace DrawRectangle_PDF
{
    class Program
    {
        static void Main(string[] args)
        {        
            //新建一個PDF文檔,添加頁
            PdfDocument doc = new PdfDocument();
            PdfPageBase page = doc.Pages.Add();

            //設置畫筆和畫刷
            PdfPen pen = new PdfPen(PdfBrushes.Black, 1f);
            PdfBrush brush1 = PdfBrushes.RosyBrown;
            PdfBrush brush2 = PdfBrushes.Goldenrod;
            //繪入矩形(此處通過設置值來繪制成正方形)
            page.Canvas.DrawRectangle(pen, brush1, new Rectangle(new Point(50, 50), new Size(60, 60)));

            //繪入橢圓(此處通過設置值來繪制成圓形)
            page.Canvas.DrawEllipse(pen, brush2, 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);

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

 

效果圖:

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

步驟1:創建pdf文檔,調用方法DrawStar()繪制自定義圖形,並保存

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

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

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

 

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

 private static void DrawStar(PdfPageBase page)
        {
            //設置五角星的5個頂點坐標
            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);
            }

            //創建PdfPath類,在頂點之間添加線段組成五角星
            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]);

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

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

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

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

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

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

            //實例化畫刷3,平移坐標並在新的位置繪入第四個五角星
            PdfLinearGradientBrush brush3
                = 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, brush3, path);

            //實例化畫刷4,平移坐標並在新的位置繪入第五個五角星
            PdfRadialGradientBrush brush4
                = 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, brush4, path);

            //實例化畫刷5,平移坐標並在新的位置繪入第六個五角星
            PdfTilingBrush brush5 = new PdfTilingBrush(new RectangleF(0, 0, 4f, 4f));
            brush5.Graphics.DrawRectangle(brush3, 0, 0, 4f, 4f);
            page.Canvas.TranslateTransform(2f, 0f);
            path.FillMode = PdfFillMode.Winding;
            page.Canvas.DrawPath(pen, brush5, path);

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

全部代碼:

using Spire.Pdf;
using Spire.Pdf.Graphics;
using System;
using System.Drawing;


namespace DrawCustomGraphics_PDF
{
    class Program
    {
        static void Main(string[] args)
        {
            //新建一個PDF文檔,添加頁
            PdfDocument doc = new PdfDocument();
            PdfPageBase page = doc.Pages.Add();

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

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

        }

        //自定義DrawStar方法繪制幾個不同樣式的五角星
        private static void DrawStar(PdfPageBase page)
        {
            //設置五角星的5個頂點坐標
            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);
            }

            //創建PdfPath類,在頂點之間添加線段組成五角星
            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]);

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

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

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

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

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

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

            //實例化畫刷3,平移坐標並在新的位置繪入第四個五角星
            PdfLinearGradientBrush brush3
                = 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, brush3, path);

            //實例化畫刷4,平移坐標並在新的位置繪入第五個五角星
            PdfRadialGradientBrush brush4
                = 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, brush4, path);

            //實例化畫刷5,平移坐標並在新的位置繪入第六個五角星
            PdfTilingBrush brush5 = new PdfTilingBrush(new RectangleF(0, 0, 4f, 4f));
            brush5.Graphics.DrawRectangle(brush3, 0, 0, 4f, 4f);
            page.Canvas.TranslateTransform(2f, 0f);
            path.FillMode = PdfFillMode.Winding;
            page.Canvas.DrawPath(pen, brush5, path);

            //再次保存畫布狀態
            page.Canvas.Restore(state);
        }
    }
}
View Code

 

效果圖:

【示例3】設置色彩透明度

步驟1:新建一個PDF文檔,添加頁

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

步驟2:初始化一個PdfSeparationColorSpace的對象,用於創建基本色,並將基本色透明度設置為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:根據顏色創建畫刷

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

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

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

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

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

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

步驟5:保存文檔

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

全部代碼:

using Spire.Pdf;
using Spire.Pdf.ColorSpace;
using Spire.Pdf.Graphics;
using System.Drawing;

namespace CrearteSpotColor_PDF
{
    class Program
    {
        static void Main(string[] args)
        {
            //新建一個PDF文檔,添加頁
            PdfDocument doc = new PdfDocument();
            PdfPageBase page = doc.Pages.Add();

            //初始化一個PdfSeparationColorSpace的對象,用於創建基本色
            PdfSeparationColorSpace cs1 = new PdfSeparationColorSpace("MySpotColor", Color.DarkGreen);
            PdfSeparationColorSpace cs2 = new PdfSeparationColorSpace("MySpotColor", Color.Yellow);
            PdfSeparationColorSpace cs3 = new PdfSeparationColorSpace("MySpotColor", Color.DeepPink);

            //將基本色透明度設置為1
            PdfSeparationColor color1 = new PdfSeparationColor(cs1, 1f);
            PdfSeparationColor color2 = new PdfSeparationColor(cs2, 1f);
            PdfSeparationColor color3 = new PdfSeparationColor(cs3, 1f);
            //根據顏色創建畫刷
            PdfSolidBrush brush1 = new PdfSolidBrush(color1);
            PdfSolidBrush brush2 = new PdfSolidBrush(color2);
            PdfSolidBrush brush3 = new PdfSolidBrush(color3);

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

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

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

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

            //保存並打開文檔
            doc.SaveToFile("設置透明度.pdf");
            System.Diagnostics.Process.Start("設置透明度.pdf");
        }
    }
}
View Code

 

效果圖:

以上是關於C#繪制PDF圖形的全部內容,如需轉載,請注明出處!

(本文完)


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM