2

I have heard that ITextSharp does not have support for the JAVA2D class, does this mean that i cant import vector points from a clients databas to "print" to a ITextSharp application?

I would relly like to find answer to this before going further with this suggestion. Anyone have real experiences of this?

1 Answers1

2

While it is true that you can't use JAVA2D with iTextSharp you can still draw vector graphics in a PDF-native way by writing directly to the PdfWriter.DirectContent object. It supports all of the standard MoveTo(), LineTo(), CurveTo(), etc methods that you'd expect from a vector drawing program. Below is a full-working VB.Net WinForms app targeting iTextSharp 5.1.1.0 the shows off some simple uses.

Option Explicit On
Option Strict On

Imports iTextSharp.text
Imports iTextSharp.text.pdf
Imports System.IO

Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim OutputFile As String = Path.Combine(My.Computer.FileSystem.SpecialDirectories.Desktop, "VectorTest.pdf")

        Using FS As New FileStream(OutputFile, FileMode.Create, FileAccess.Write, FileShare.None)
            Using Doc As New Document(PageSize.LETTER)
                Using writer = PdfWriter.GetInstance(Doc, FS)
                    ''//Open the PDF for writing
                    Doc.Open()

                    Dim cb As PdfContentByte = writer.DirectContent

                    ''//Save the current state so that we can restore it later. This is not required but it makes it easier to undo things later
                    cb.SaveState()

                    ''//Draw a line with a bunch of options set
                    cb.MoveTo(100, 100)
                    cb.LineTo(500, 500)
                    cb.SetRGBColorStroke(255, 0, 0)
                    cb.SetLineWidth(5)
                    cb.SetLineDash(10, 10, 20)
                    cb.SetLineCap(PdfContentByte.LINE_CAP_ROUND)
                    cb.Stroke()

                    ''//This undoes any of the colors, widths, etc that we did since the last SaveState
                    cb.RestoreState()

                    ''//Draw a circle
                    cb.SaveState()
                    cb.Circle(200, 500, 50)
                    cb.SetRGBColorStroke(0, 255, 0)
                    cb.Stroke()

                    ''//Draw a bezier curve
                    cb.RestoreState()
                    cb.MoveTo(100, 300)
                    cb.CurveTo(140, 160, 300, 300)
                    cb.SetRGBColorStroke(0, 0, 255)
                    cb.Stroke()

                    ''//Close the PDF
                    Doc.Close()
                End Using
            End Using
        End Using
    End Sub
End Class

EDIT

Incidentally, although you can't use JAVA2D (which is obviously Java and wouldn't work with .Net) you can create iTextSharp images using the standard System.Drawing.Image class and passing it to iTextSharp.text.Image.GetInstance() static method. Unfortunately System.Drawing.Image is a raster/bitmap object so it won't help you in this case.

Chris Haas
  • 53,986
  • 12
  • 141
  • 274
  • Thank you very much for making such a nice example and answering my question. It will make my job easier! I think i will use ITextSharp now for an upcoming project. / R – Rasmus Lundberg Sep 12 '11 at 07:35