C # drawing PDF Graphics-Basic graphics, custom graphics, color transparency

Source: Internet
Author: User
Tags custom graphics pdfpen

Introduction

In the PDF we can add very rich elements to the content we want to express through C # program code, such as drawing tables, text, adding graphics, images, and so on. In this article, I'll show you how to draw a graphic in a PDF and set the operation of a graphic property.

The article will be described in the following points:

    1. Draw Basic shapes (lines, ellipses, circles, rectangles, triangles)
    2. Drawing a custom graphic
    3. Draw graphics and set graphic transparency
      Tools Required : Spire.pdf for. NET 4.0

tip : After installation, directly refer to the DLL file in the Bin folder under the installation path to the project program.

"Example 1" draws a basic graphic

Step 1: create a new PDF document, add a page, add a brush, paint a brush

           //新建一个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;

Step 2: draw a circle, rectangle, line segment, triangle

            //绘入矩形(此处通过设置值来绘制成正方形)            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);

Step 3: Save the document

            //保存并打开文档            doc.SaveToFile("基本图形.pdf");            System.Diagnostics.Process.Start("基本图形.pdf");

Graphics Add Effect:

"Example 2" drawing a custom graphic

Step 1: Create a PDF document, call Method Drawstar () to draw a custom graphic, and save

            //新建一个PDF文档,添加页            PdfDocument doc = new PdfDocument();            PdfPageBase page = doc.Pages.Add();            //调用DrawStar()方法绘入五角星图形            DrawStar(page);            //保存并打开文档            doc.SaveToFile("自定义图形.pdf");            System.Diagnostics.Process.Start("自定义图形.pdf");

Step 2: Customize the Drawstar () method to draw several different styles of pentagram

private static void Drawstar (Pdfpagebase page) {//Set 5 vertex coordinates of a pentagram 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);            }//Create the Pdfpath class to add segments between vertices to form the pentagram 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]); Save Canvas status Pdfgraphicsstate state = page.            Canvas.save ();            Instantiate brushes and brushes 1, brush 2 pdfpen pen = new PDFpen (Color.deepskyblue, 0.02f);            Pdfbrush brush1 = new Pdfsolidbrush (color.palegreen);            Pdfbrush brush2 = new Pdfsolidbrush (color.bisque); The coordinatesZoom in 40 times times page.            Canvas.scaletransform (40f, 40f); The translation coordinates page.            Canvas.translatetransform (1f, 1.5f); Draw in the first Pentagram page.            Canvas.drawpath (pen, path); Translates the coordinates and draws the second Pentagram page at the new location.            Canvas.translatetransform (2f, 0f); Path.            Fillmode = pdffillmode.alternate; Page.            Canvas.drawpath (pen, brush1, PATH); Translates the coordinates and draws a third pentagram page in the new location.            Canvas.translatetransform (2f, 0f); Path.            Fillmode = pdffillmode.winding; Page.            Canvas.drawpath (pen, BRUSH2, PATH); Instantiate a brush 3, translate the coordinates and draw the fourth pentagram in the new position pdflineargradientbrush BRUSH3 = new Pdflineargradientbrush (new point            F ( -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);              Instantiate a brush 4, translate the coordinates and draw the fifth pentagram in the new position 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);            Instantiate the brush 5, translate the coordinates and draw in the new position the sixth pentagram 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); Save the Canvas state page again.        Canvas.restore (state); }

Custom Graphics Add Effects:

"Example 3" Set color transparency

Step 1: Create a new PDF document, add a page

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

Step 2: Initialize an Pdfseparationcolorspace object to create the base color and set the base color transparency to 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);

Step 3: Create a brush based on color

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

Step 4: Draw in graphics and text, apply color transparency to graphics

            Draws graphics and text and coloring 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 ("Transparency =1.0", New Pdftruetypefont ("Arial Unicode MS", 10f), true), Brush1, New PointF (16, 100));            Set the basic color transparency to 0.5 and draw the picture and text 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 ("Transparency =0.5", New Pdftruetypefont ("Arial Unicode MS", 10f), True), Brush1, New PointF (86, 100));            Set the basic color transparency to 0.25 and draw the picture and text 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 ("Transparency =0.25", new Pdftruetypefont (New Font ("Arial Unicode MS", 10f), true), Brush1, New PointF (156, 100)) ;

Step 5: Save the document

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

Color transparency achieves the effect:

The above is about C # to draw the entire content of PDF graphics, if you want to reprint, please specify the source!
(End of this article)

C # drawing PDF Graphics-Basic graphics, custom graphics, color transparency

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.