Programming

How to Draw Shape in PDF with C#

July 11, 2011

At present, more and more e-books and e-magazines are created with PDF because people can insert beautiful images, flash or some other attractive elements in PDF, which make the e-books or e-magazines be more appealed for readers.

Besides inserting images or flash, sometimes, people may draw shapes or tables in PDF document according to the document contents. For example, if one paragraph has shown much data information, the author may create a data table to make reader learn clearly about the data.

But, there is a question. How to draw in PDF? And now, I want to talk something about how to draw shape in PDF.

Generally speaking, if we want to edit one PDF document, we need to use some professional tools. The most frequently used tool is Adobe Acrobat. People can follow the guide about how to use these tools to meet their requirements.

The method I introduce is how to draw shape by using C#. This method is useful for developers who want to create PDF drawing tools. And I use one component, Spire.PDF in this method.

The shapes I draw include star and circle.

 

using System;

using System.Drawing;

using Spire.Pdf;

using Spire.Pdf.Graphics;

namespace DrawShape

{

    class Program

    {

        static void Main(string[] args)

        {

            //Create a pdf document.

            PdfDocument doc = new PdfDocument();

            // Create one page

            PdfPageBase page = doc.Pages.Add();

            DrawSpiro(page);

            DrawPath(page);

            //Save pdf file.

            doc.SaveToFile("DrawShape.pdf");

            doc.Close();

            //Launching the Pdf file.

            System.Diagnostics.Process.Start("DrawShape.pdf");

        }

        private static void DrawPath(PdfPageBase page)

        {

            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 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 graphics state

            PdfGraphicsState state = page.Canvas.Save();

            PdfPen pen = new PdfPen(Color.DeepSkyBlue, 0.02f);

            PdfBrush brush1 = new PdfSolidBrush(Color.CadetBlue);

            page.Canvas.ScaleTransform(50f, 50f);

            page.Canvas.TranslateTransform(5f, 1.2f);

            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, brush1, path);

            PdfLinearGradientBrush brush2

                = new PdfLinearGradientBrush(new PointF(-2, 0), new PointF(2, 0), Color.Red, Color.Blue);

            page.Canvas.TranslateTransform(-4f, 2f);

            path.FillMode = PdfFillMode.Alternate;

            page.Canvas.DrawPath(pen, brush2, path);

            PdfRadialGradientBrush brush3

                = new PdfRadialGradientBrush(new PointF(0f, 0f), 0f, new PointF(0f, 0f), 1f, Color.Red, Color.Blue);

            page.Canvas.TranslateTransform(2f, 0f);

            path.FillMode = PdfFillMode.Winding;

            page.Canvas.DrawPath(pen, brush3, path);

            PdfTilingBrush brush4 = new PdfTilingBrush(new RectangleF(0, 0, 4f, 4f));

            brush4.Graphics.DrawRectangle(brush2, 0, 0, 4f, 4f);

            page.Canvas.TranslateTransform(2f, 0f);

            path.FillMode = PdfFillMode.Winding;

            page.Canvas.DrawPath(pen, brush4, path);

            //restor graphics

            page.Canvas.Restore(state);

        }

        private static void DrawSpiro(PdfPageBase page)

        {

            //save graphics state

            PdfGraphicsState state = page.Canvas.Save();

            //Draw shap - spiro

            PdfPen pen = PdfPens.DeepSkyBlue;

            int nPoints = 1000;

            double r1 = 30;

            double r2 = 25;

            double p = 35;

            double x1 = r1 + r2 - p;

            double y1 = 0;

            double x2 = 0;

            double y2 = 0;

            page.Canvas.TranslateTransform(100, 100);

            for (int i = 0; i < nPoints; i++)

            {

                double t = i * Math.PI / 90;

                x2 = (r1 + r2) * Math.Cos(t) - p * Math.Cos((r1 + r2) * t / r2);

                y2 = (r1 + r2) * Math.Sin(t) - p * Math.Sin((r1 + r2) * t / r2);

                page.Canvas.DrawLine(pen, (float)x1, (float)y1, (float)x2, (float)y2);

                x1 = x2;

                y1 = y2;

            }

            //restor graphics

            page.Canvas.Restore(state);

        }

    }

}

 

How to Paginate PDF with C#

June 28, 2011

As is known, one book includes cover, back cover, category and contents. Therefore, if we want to create an electronic book, the above elements are needed to be contained. And firstly we should learn how to paginate.

Generally speaking, the electronic books are PDF books. So, in this article, I want to talk how to create PDF pagination.

It is common to use Adobe Acrobat to edit the PDF book. Open it and count pages. Then click advanced. Select document Operation and then choose Pagina...


Continue reading...
 

How to Protect Excel by Using MS and C#

June 15, 2011

Generally speaking, people may protect one important document or file by giving a password for it, especially some important or secret files. For office workers, they often record important data in Excel. Therefore, they need to know clearly about how to protect Excel file.

There are several methods to protect Excel file. We can lock the whole worksheet to prevent others from opening or editing it. Also, we can just protect specified Excel rangeor cells.

Firstly, I want to talk somethi...


Continue reading...
 

Add Word Header and Footer with C#

June 1, 2011

If a word document has many pages, people may add header or footer to show the page number. Sometimes, the header or footer may be symbols which are the topic of the document talk about or the owner of the document.

It is simple to insert add header and footer. Open a Word document. Then click Header and footer on File menu. Type the text or use the format Word provides.

Then, I want to show something about how to add Word header and footer with C#.  

Using the code:

#region add W...


Continue reading...
 

How to Convert Image to PDF and the Program with C#

May 19, 2011

In PDF, we can insert image to make the contents be colorful and attractive. And it is possible that one image is one PDF document, especially in PDF form. Therefore, we may need to convert image to PDF sometimes.

Except Adobe Acrobat, which is the professional tool to manipulate PDF, there are lots of converters which specialize in converting image to PDF online. The tools are easy to use and the developers will show the guide about how to use the converters. Choose the image you want to...


Continue reading...
 

How to Set Excel Text Alignment

March 11, 2011

Introduction

We often format an Excel worksheet to make it be convenient for reading. Usually, we complete Excel formatting through setting cells styles. For example, if the text in one cell is the title of one column or row, we may set its size to be larger than other cells. Also, we may give a color for text in one cell to show that i...


Continue reading...
 

How to Decrypt Word Document

March 11, 2011
In order to make sure that the important information can't be revealed, we may encrypt a Word document by giving it a complicated password. The Word protection function is very powerful that it is difficult to get the encrypted document without password. However, sometimes, we may forget the password if it is too complex or we can't use the protected Word document for a long time.

How to Decrypt Word Document by Using Spire.Doc

In Spire.Doc, you can simply use document.LoadFromFile method to de...

Continue reading...
 

How to Convert Word to PDF

February 15, 2011

What is PDF

PDF (Portable Document Format) is one kind of electronic document format. PDF is the ideal document format to publish electronic document and deliver digital information on internet for it has nothing to do with operation system. PDF file format can package font, format, color and image in a file. Also, it can include hyperl...


Continue reading...
 

How to Add a Formula for Excel

February 15, 2011

Introduction

In an Excel Worksheet, we may import a great deal of data. Sometimes, we need to calculate the data to get another numbers we need. Formula is the mail tool to calculate...


Continue reading...
 

How to Convert Word Document to XML

January 10, 2011

Basic Knowledge about XML

When talking about XML, we may think of HTML. Actually, XML is similar to HTML are tag-based languages. The difference between XML and HTML is that the tags which XML uses are not predefined. If we want to create own tags within XML, we need to follow a few rules.

Firstly, only one root element is contained in XML document. The root element is often taken as document element and appears after the prolog section. Besides, all the XML elements should contain end ...


Continue reading...
 

About Me


Recent Posts