Click here to Skip to main content
15,397,950 members
Articles / Desktop Programming / Windows Forms
Posted 24 Jun 2008


29 bookmarked

GDI+ Path to PDF

Rate me:
Please Sign up or sign in to vote.
4.20/5 (11 votes)
24 Jun 2008CPOL2 min read
Convert a GDI+ graphics path to PDF content with iTextSharp.

Image 1


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.


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.


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] == 
             if ((path.PathData.Types[i] & 
                 (byte)PathPointType.Bezier) == 
                if (len == 3)
                   len = 0;
             else if ((path.PathData.Types[i] & 
                      (byte)PathPointType.Line) == 
             if ((path.PathData.Types[i] & 
                 (byte)PathPointType.CloseSubpath) == 

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.


  • 19.06.2008 - Version 1.0 released.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Tefik Becirovic
Systems Engineer
Germany Germany
No Biography provided

Comments and Discussions

GeneralMy vote of 5 Pin
scosta_FST13-Mar-12 21:47
Memberscosta_FST13-Mar-12 21:47 
Very good.
Simple, concise and working piece of code.

Any hint on how to extract vector graphics (not text or images) from a pdf?

GeneralRe: My vote of 5 Pin
Tefik Becirovic30-Mar-12 8:42
MemberTefik Becirovic30-Mar-12 8:42 
GeneralRe: My vote of 5 Pin
scosta_FST1-Apr-12 20:35
Memberscosta_FST1-Apr-12 20:35 
GeneralMy vote of 5 Pin
Manoj Kumar Choubey18-Feb-12 0:08
professionalManoj Kumar Choubey18-Feb-12 0:08 
GeneralRe: My vote of 5 Pin
Tefik Becirovic30-Mar-12 8:44
MemberTefik Becirovic30-Mar-12 8:44 
JokeCoooooool!!!! Pin
Alexander Kholodovitch1-Nov-08 3:15
MemberAlexander Kholodovitch1-Nov-08 3:15 
GeneralRe: Coooooool!!!! Pin
Tefik Becirovic1-Nov-08 3:59
MemberTefik Becirovic1-Nov-08 3:59 
GeneralDOUBT Pin
Cruel Handsome23-Aug-08 0:52
MemberCruel Handsome23-Aug-08 0:52 
GeneralRe: DOUBT Pin
Tefik Becirovic23-Aug-08 4:55
MemberTefik Becirovic23-Aug-08 4:55 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.