Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

GDI+ Path to PDF

0.00/5 (No votes)
24 Jun 2008 1  
Convert a GDI+ graphics path to PDF content with iTextSharp.

Introduction

Microsoft GDI+ and Adobe PDF graphical concepts are two similar and convergent graphical concepts, but enormously different in implementation details on the syntax level and in object handling. It is not simple to use GDI+ objects, methods, and code snippets to generate PDF files directly without changes. The central concept for graphical object construction in both technologies is a path.

This article should show you that it is possible to convert paths from one (GDI+) to the other (PDF) technology and to make a bridge between GDI+ and PDF.

Requirements

You will require:

  1. The iTextSharp v.4.1.2 DLL from the SourceForge project page to test the compiled example.
  2. SharpDevelop v2.2.1 Build 2648 from the SharpDevelop home page to compile the source code.

After downloading iTextSharp binaries, you will need to copy iTextSharp.dll:

  1. to the Exe directory before starting the example,
  2. to the Src directory before compiling the project.

Description

Conversion from a GDI+ to PDF path is realised inside the static Append method of the PathToPDF class:

public class PathToPDF
{
    public static void Append(GraphicsPath path, ByteBuffer content)
    {
       int len = 0;
       for (int i = 0; i<path.PathData.Points.Length; i++)
       {
          content.Append(path.PathData.Points[i + 0].X).Append(' ')
                 .Append(path.PathData.Points[i + 0].Y).Append(' ');
 
          if (path.PathData.Types[i] == 
             (byte)PathPointType.Start)
             content.Append("m\n");
          else 
          { 
             if ((path.PathData.Types[i] & 
                 (byte)PathPointType.Bezier) == 
                 (byte)PathPointType.Bezier)
             {
                len++;
                if (len == 3)
                {
                   len = 0;
                   content.Append("c\n");
                }
             }
             else if ((path.PathData.Types[i] & 
                      (byte)PathPointType.Line) == 
                      (byte)PathPointType.Line)
                     content.Append("l\n");
 
             if ((path.PathData.Types[i] & 
                 (byte)PathPointType.CloseSubpath) == 
                 (byte)PathPointType.CloseSubpath)
                content.Append("h\n");
          }
       }
    }
}

This Append method is similar to:

Private Function _createSVG(ByVal _path As GraphicsPath) As String

from Alexander Seel's CodeProject article Text on Path with VB.NET, but adapted to PDF content syntax and optimized.

Using the Code

To create PDF files with iTextSharp, you need to do five simple steps:

  1. Create a document-object
  2. Create a writer that directs a PDF-stream to a file
  3. Open the document
  4. Grab the ContentByte and do something with it
  5. Close the document

After grabbing ContentByte from the DirectContent of the PDF-stream writer in step 4 with:

ContentByte cb = writer.DirectContent;

you can append a prepared GDI+ path with:

iTextSharp.Utility.PathToPDF.Append(path, cb.InternalBuffer);

directly to the content of the PDF document. It is possible to append more different paths before the document is closed in step 5. As shown in the attributive example, you can use PathToPDF to fit a text exactly to any rectangle on a PDF page. Take a note that the fitted text is converted to various numbers of paths which are described into the character definition of the used font and is not saved as a string as usual.

History

  • 19.06.2008 - Version 1.0 released.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here