It is necessary to have some images in one document to show information more intuitively. Also, the beautiful images with words are more appealed to readers than text only.

Generally speaking, we can insert images in documents directly. And then adjust images’ size or direction according to the requirements. But sometimes, there are not appropriate images for contents so that we may need to draw one. For example, if there must be a chart in a document, we should draw one based on the data information shown in document.

So, how to draw a image in our electronic document? Because PDF is frequently used by people so I intend to introduce the method about how to draw image in PDF.

For editing PDF document, people often use Adobe Acrobat. And there are lots of guides about how to use Adobe Acrobat. Beginners just follow the guide and then can edit their document.

The method I will share is not to use tools but draw programmatically. The image drawn in this method is a chart. It is based on a development component named Spire.PDF. If you want to use this method, you need to download and install it firstly.

The following code shows how to draw image in PDF with C#.

 

using System;

using System.Drawing;

using Spire.Pdf;

using Spire.Pdf.Graphics;

 

namespace DrawImage

{

    class Program

    {

        static void Main(string[] args)

        {

            //Create a pdf document.

            PdfDocument doc = new PdfDocument();

 

            // Create one page

            PdfPageBase page = doc.Pages.Add();

 

            TransformText(page);

            DrawImage(page);

            TransformImage(page);

 

            //Save pdf file.

            doc.SaveToFile("DrawImage.pdf");

            doc.Close();

 

            //Launching the Pdf file.

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

        }

 

        private static void TransformImage(PdfPageBase page)

        {

            //save graphics state

            PdfGraphicsState state = page.Canvas.Save();

 

            //Draw the text - transform          

            PdfFont font = new PdfFont(PdfFontFamily.Helvetica, 18f);

            PdfSolidBrush brush1 = new PdfSolidBrush(Color.Blue);

            PdfSolidBrush brush2 = new PdfSolidBrush(Color.CadetBlue);

            PdfStringFormat format = new PdfStringFormat(PdfTextAlignment.Center);

 

            page.Canvas.TranslateTransform(page.Canvas.ClientSize.Width / 2, 20);

            page.Canvas.DrawString("Sales Report Chart", font, brush1, 0, 0, format);

 

            page.Canvas.ScaleTransform(1f, -0.8f);

            page.Canvas.DrawString("Sales Report Chart", font, brush2, 0, -2 * 18 * 1.2f, format);

            //restor graphics

            page.Canvas.Restore(state);

        }

 

        private static void DrawImage(PdfPageBase page)

        {

            PdfImage image = PdfImage.FromFile(@"SalesReportChart.png");

            float width = image.Width * 0.75f;

            float height = image.Height * 0.75f;

            float x = (page.Canvas.ClientSize.Width - width) / 2;

 

            page.Canvas.DrawImage(image, x, 60, width, height);

        }

 

        private static void TransformText(PdfPageBase page)

        {

            PdfImage image = PdfImage.FromFile(@"SalesReportChart.png");

            int skewX = 20;

            int skewY = 20;

            float scaleX = 0.2f;

            float scaleY = 0.6f;

            int width = (int)((image.Width + image.Height * Math.Tan(Math.PI * skewX / 180)) * scaleX);

            int height = (int)((image.Height + image.Width * Math.Tan(Math.PI * skewY / 180)) * scaleY);

            PdfTemplate template = new PdfTemplate(width, height);

            template.Graphics.ScaleTransform(scaleX, scaleY);

            template.Graphics.SkewTransform(skewX, skewY);

            template.Graphics.DrawImage(image, 0, 0);

 

            //save graphics state

            PdfGraphicsState state = page.Canvas.Save();

            page.Canvas.TranslateTransform(page.Canvas.ClientSize.Width - 50, 260);

            float offset = (page.Canvas.ClientSize.Width - 100) / 12;

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

            {

                page.Canvas.TranslateTransform(-offset, 0);

                page.Canvas.SetTransparency(i / 12.0f);

                page.Canvas.DrawTemplate(template, new PointF(0, 0));

            }

 

            //restor graphics

            page.Canvas.Restore(state);

        }

    }

}