I did an 'imports' - error TYPE EXPECTED

时间秒杀一切 提交于 2020-01-24 23:28:03

问题


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


回答1:


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


来源:https://stackoverflow.com/questions/1331253/i-did-an-imports-error-type-expected

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!