我做了'进口' - 错误类型预期

时间:2009-08-25 21:56:44

标签: vb.net winforms pdf itextsharp

我这样做了:

Imports iTextSharp.text.rtf

然后这个:

Dim grx As graphic = New graphic

在第一个“图形”上我得到了“预期的类型”

graphic是iTextSharp.text.rtf

的成员

这是周围的代码:

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

以下是整个教程:(抱歉,这不是教程,但这就是他们所说的)

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 

结束命名空间

1 个答案:

答案 0 :(得分:3)

在玩了一段时间后,我认为结论是你所遵循的教程适用于过时的iText / iTextSharp版本。

他们的sourceforge网站从2006年1月开始链接到匹配的example,您对VB.NET的翻译看起来很准确 - 问题是当前版本的iTextSharp不包含{ {1}}类型,经过一些搜索后,似乎只是重命名 - 更有可能是完整图形API发生了重大变化。

sourceforge页面有一个免责声明(最后一行),链接的示例可能不再起作用,

  

请注意,某些示例不适用于最新版本的iTextSharp。

根据给定的证据和Reflector的使用,我发现预期的Graphic方法仅存在于Graphic.Stroke()类中;但是PdfContentByte需要一个实现Document.Add()的类,IElement不会这样做。

这个变化是我可以使 关闭 进行编译的最小变化,但它会显着改变代码的意图,并且可能无法按预期运行。这是我的更新版本供您参考:

PdfContentByte
相关问题