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

 

引言

 

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

 

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

 

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

 

2. 繪製自定義圖形

 

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

 

所需工具:Spire.PDF for .NET 4.0

 

【示例1】繪製基本圖形

【C#】

 

 

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");
        }
    }
}

 效果圖:

 

 


 

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

【C#】

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);
        }
    }
}

 效果圖:

 



 

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

 

【C#】

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");
        }
    }
}

 效果圖:



 

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

 

(本文完)

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章