views:

260

answers:

1

I did this:

Imports iTextSharp.text.rtf

and then this:

Dim grx As graphic = New graphic

and on the first "graphic" I am getting a "type expected"

graphic is a member of iTextSharp.text.rtf

Here's the surrounding code:

Public Sub New1()
    Console.WriteLine("Chapter 4 example 4: Simple Graphic")
    Dim document As Document = New Document
    Try
        PdfWriter.GetInstance(document, New FileStream("Chap0404.pdf", FileMode.Create))
        document.Open()
        Dim grx As graphic = New graphic
        grx.Rectangle(100, 700, 100, 100)
        grx.MoveTo(100, 700)
        grx.LineTo(200, 800)
        grx.Stroke()
        document.Add(grx)
    Catch de As DocumentException
        Console.Error.WriteLine(de.Message)
    Catch ioe As IOException
        Console.Error.WriteLine(ioe.Message)
    End Try
    document.Close()
End Sub

Here's the entire tutorial: (sorry its not a tutorial but thats what they call it)

Imports System
Imports System.IO
Imports iTextSharp.text
Imports iTextSharp.text.pdf
Namespace iTextSharp.tutorial.Chap04

Public Class Chap0404

 Public Sub New()
  Console.WriteLine("Chapter 4 example 4: Simple Graphic")
  Dim document As Document = New Document
  Try
   PdfWriter.GetInstance(document, New FileStream("Chap0404.pdf", FileMode.Create))
   document.Open
   Dim grx As Graphic = New Graphic
   grx.Rectangle(100, 700, 100, 100)
   grx.MoveTo(100, 700)
   grx.LineTo(200, 800)
   grx.Stroke
   document.Add(grx)
  Catch de As DocumentException
   Console.Error.WriteLine(de.Message)
  Catch ioe As IOException
   Console.Error.WriteLine(ioe.Message)
  End Try
  document.Close
 End Sub
End Class

End Namespace

+3  A: 

After playing with this for a while I think the conclusion is that the tutorial you're following applies to an out-of-date version of iText / iTextSharp.

Their sourceforge site links to a matching example from January of 2006, and your translation to VB.NET looks accurate--the problem is that the current version of iTextSharp doesn't contain a Graphic type, and after some searching it doesn't appear to have been just renamed--it's more likely that the full graphics API has been significantly altered.

The sourceforge page has a disclaimer (last line) that the linked examples might not work anymore,

Note that some of the example won't work with the most recent version of iTextSharp.

With the given evidence, and the use of Reflector, I found that the expected Graphic.Stroke() method only exists within the PdfContentByte class; however Document.Add() expects a class that implements IElement, which PdfContentByte doesn't do.

That change is the smallest I could make to get close to compiling, but it significantly alters the intent of the code and probably won't function as expected. Here's my updated version for your reference though:

Public Class Chap0404

 Public Sub New()
  Console.WriteLine("Chapter 4 example 4: Simple Graphic")
  Dim document As Document = New Document
  Try
   Dim writer As PdfWriter = PdfWriter.GetInstance(document, New FileStream("Chap0404.pdf", FileMode.Create))
   document.Open()
   Dim grx As PdfContentByte = New PdfContentByte(writer)
   grx.Rectangle(100, 700, 100, 100)
   grx.MoveTo(100, 700)
   grx.LineTo(200, 800)
   grx.Stroke()
   'document.Add(grx)
  Catch de As DocumentException
   Console.Error.WriteLine(de.Message)
  Catch ioe As IOException
   Console.Error.WriteLine(ioe.Message)
  End Try
  document.Close()
 End Sub
End Class
STW
tried this doesnt work
I__
Does the Imports statement look correct in VS (no squiggley?); are you sure you've got all necessary references?
STW
yes im sure i actually got it directly out of the tutorial
I__
...you're gonna make me go download the library then... so be it! :-)
STW
you're the man thanks so much for your help
I__
updated the answer, but it's still not what you're after. What's the URL of the tutorial you're looking at?
STW
cool thank you, so what shall i do?
I__
actually ive pasted the entire thing already
I__