Click here to Skip to main content
Click here to Skip to main content

GDI+ Path to PDF

, 24 Jun 2008
Rate this:
Please Sign up or sign in to vote.
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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Tefik Becirovic
Systems Engineer
Germany Germany
No Biography provided

Comments and Discussions

 
GeneralMy vote of 5 Pinmemberscosta_FST13-Mar-12 21:47 
GeneralRe: My vote of 5 [modified] PinmemberTefik Becirovic30-Mar-12 8:42 
GeneralRe: My vote of 5 Pinmemberscosta_FST1-Apr-12 20:35 
GeneralMy vote of 5 Pinmembermanoj kumar choubey18-Feb-12 0:08 
GeneralRe: My vote of 5 PinmemberTefik Becirovic30-Mar-12 8:44 
JokeCoooooool!!!! PinmemberMember 27936861-Nov-08 3:15 
GeneralRe: Coooooool!!!! PinmemberTefik Becirovic1-Nov-08 3:59 
GeneralDOUBT PinmemberSoumyaRanjan23-Aug-08 0:52 
GeneralRe: DOUBT PinmemberTefik Becirovic23-Aug-08 4:55 

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

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

| Advertise | Privacy | Mobile
Web01 | 2.8.140821.2 | Last Updated 24 Jun 2008
Article Copyright 2008 by Tefik Becirovic
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid