Click here to Skip to main content
Licence CPOL
First Posted 24 Jun 2008
Views 20,210
Downloads 489
Bookmarked 22 times

GDI+ Path to PDF

By | 24 Jun 2008 | Article
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)

About the Author

Tefik Becirovic

Systems Engineer
Freelancer
Germany Germany

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralMy vote of 5 Pinmemberscosta_FST21:47 13 Mar '12  
GeneralRe: My vote of 5 [modified] PinmemberTefik Becirovic8:42 30 Mar '12  
GeneralRe: My vote of 5 Pinmemberscosta_FST20:35 1 Apr '12  
GeneralMy vote of 5 Pinmembermanoj kumar choubey0:08 18 Feb '12  
GeneralRe: My vote of 5 PinmemberTefik Becirovic8:44 30 Mar '12  
JokeCoooooool!!!! PinmemberMember 27936863:15 1 Nov '08  
GeneralRe: Coooooool!!!! PinmemberTefik Becirovic3:59 1 Nov '08  
GeneralDOUBT PinmemberSoumyaRanjan0:52 23 Aug '08  
GeneralRe: DOUBT PinmemberTefik Becirovic4:55 23 Aug '08  

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.

Permalink | Advertise | Privacy | Mobile
Web04 | 2.5.120517.1 | Last Updated 24 Jun 2008
Article Copyright 2008 by Tefik Becirovic
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid