Click here to Skip to main content
15,867,568 members
Articles / Desktop Programming / Windows Forms

GDI+ Path to PDF

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

Image 1

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:

C#
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:

VB
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:

C#
ContentByte cb = writer.DirectContent;

you can append a prepared GDI+ path with:

C#
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)


Written By
Systems Engineer
Germany Germany
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMy vote of 5 Pin
scosta_FST13-Mar-12 21:47
scosta_FST13-Mar-12 21:47 
GeneralRe: My vote of 5 Pin
Tefik Becirovic30-Mar-12 8:42
Tefik Becirovic30-Mar-12 8:42 
GeneralRe: My vote of 5 Pin
scosta_FST1-Apr-12 20:35
scosta_FST1-Apr-12 20:35 
Thank you very much for your hints.
What I was searching for, is a method to extract by code vector graphics from a pdf.
I was thinking that, maybe, with itextsharp there is some way to do that.
But thank you again.
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
Tefik Becirovic30-Mar-12 8:44 
JokeCoooooool!!!! Pin
Alexander Kholodovitch1-Nov-08 3:15
Alexander Kholodovitch1-Nov-08 3:15 
GeneralRe: Coooooool!!!! Pin
Tefik Becirovic1-Nov-08 3:59
Tefik Becirovic1-Nov-08 3:59 
GeneralDOUBT Pin
Cruel Handsome23-Aug-08 0:52
Cruel Handsome23-Aug-08 0:52 
GeneralRe: DOUBT Pin
Tefik Becirovic23-Aug-08 4:55
Tefik 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.