1

我这样做了:

Imports iTextSharp.text.rtf

然后这个:

Dim grx As graphic = New 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 

结束命名空间

4

1 回答 1

3

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

他们的sourceforge站点链接到 2006 年 1 月的一个匹配示例,您对 VB.NET 的翻译看起来很准确——问题是当前版本的 iTextSharp 不包含Graphic类型,经过一番搜索后,它似乎没有刚刚被重命名——更可能是完整的图形 API 已被显着改变。

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

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

有了给定的证据,加上反射器的使用,我发现预期的Graphic.Stroke()方法只存在于PdfContentByte类中;但是Document.Add()需要一个实现 的类IElement,但它PdfContentByte不会。

该更改是我为接近编译所做的最小更改,但它显着改变了代码的意图,并且可能无法按预期运行。这是我的更新版本供您参考:

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
于 2009-08-25T21:59:37.177 回答