如何在 vb.net 中使用 iTextSharp 创建虚线分隔符

how to create dashed line separator using iTextSharp in vb.net

我已完成自定义虚线分隔符示例,但无法完成
在 visual basic 中做在 vb

中有任何例子吗

我收到以下错误

1)'Dash' 已在此 class.

中声明为 'Protected Friend dash As Single'

2)'Phase' 已在此 class 中声明为 'Protected Friend phase As Single'。

3)'LineWidth' 不是 'iTextSharp.text.pdf.PdfContentByte' 的成员。

4) 变量'Dash' 在赋值之前被使用。一个空 引用异常可能会在运行时产生。

5) 变量'Phase' 在赋值之前被使用。空
引用异常可能会在运行时产生。

6) sub 'draw' 隐藏了基础 class 'DottedLineSeparator' 中的一个可覆盖方法。要覆盖基方法,必须声明此方法 'Overrides'。

 Option Strict On
 Option Explicit On

Imports iTextSharp.text
Imports iTextSharp.text.pdf
Imports iTextSharp.text.pdf.draw

Public Class Form1

Private Sub Form1_Load(sender As System.Object, e As System.EventArgs)     
Handles MyBase.Load

End Sub

Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) 
Handles Button1.Click
    Dim FileName As String = System.IO.Path.Combine 
(AppDomain.CurrentDomain.BaseDirectory, "Customers.pdf")
    'Dim PageSize As New iTextSharp.text.Rectangle 
(iTextSharp.text.PageSize.A4)
    Dim Document As Document = New Document(iTextSharp.text.PageSize.A4,  
50, 10, 10, 10)

    Try
        PdfWriter.GetInstance(Document, New System.IO.FileStream(FileName, 
  System.IO.FileMode.Create))

        Document.Open()

        Dim separator As New CustomDashedLineSeparator()
        separator.dash = 10
        separator.Gap = 7
        separator.LineWidth = 3
        Dim linebreak As New Chunk(separator)
        Document.Add(linebreak)


        Document.Close()

    Catch ex As Exception
        MsgBox("Pdf is Created")
    End Try

End Sub
End Class
Friend Class CustomDashedLineSeparator
Inherits pdf.draw.DottedLineSeparator

Protected Friend dash As Single = 5
Protected Friend phase As Single = 2.5F
Public Overridable Property Dash As Single
    Get
        Return Dash
    End Get
    Set(ByVal dash As Single)
        Me.dash = dash
    End Set
End Property
Public Overridable Property Phase As Single
    Get
        Return Phase
    End Get
    Set(ByVal phase As Single)
        Me.phase = phase
    End Set
End Property

Public Overridable Sub draw(ByVal canvas As PdfContentByte, ByVal llx As  
Single, ByVal lly As Single, ByVal urx As Single, ByVal ury As Single,  
ByVal y As Single)
    canvas.SaveState()
    canvas.LineWidth = LineWidth
    canvas.SetLineDash(dash, Gap, phase)
    DrawLine(canvas, llx, urx, y)
    canvas.RestoreState()
 End Sub
 End Class

与 C# 和 Java 不同,Visual Basic 通常不区分大小写。您有一个 属性 和名为 Dash 的字段,也有相同的 Phase。这是你的前两个错误以及你的第四个和第五个。您可以通过重命名字段来解决此问题。一种常见的方法是在它们前面加上下划线。

对于第三个错误,需要使用SetLineWidth()方法,而不是使用LineWidth 属性.

对于您的第六个错误,您试图在没有明确告诉 VB 您想要覆盖的情况下覆盖一个方法。为此,您需要使用 Overrides 而不是 Overridable.

您清理后的 class 应该如下所示:

Friend Class CustomDashedLineSeparator
    Inherits iTextSharp.text.pdf.draw.DottedLineSeparator

    Protected Friend _dash As Single = 5
    Protected Friend _phase As Single = 2.5F
    Public Overridable Property Dash As Single
        Get
            Return Dash
        End Get
        Set(ByVal dash As Single)
            Me._dash = dash
        End Set
    End Property
    Public Overridable Property Phase As Single
        Get
            Return _Phase
        End Get
        Set(ByVal phase As Single)
            Me._phase = phase
        End Set
    End Property

    Public Overrides Sub Draw(ByVal canvas As PdfContentByte, ByVal llx As Single, ByVal lly As Single, ByVal urx As Single, ByVal ury As Single, ByVal y As Single)
        canvas.SaveState()
        canvas.SetLineWidth(LineWidth)
        canvas.SetLineDash(Dash, Gap, phase)
        DrawLine(canvas, llx, urx, y)
        canvas.RestoreState()
    End Sub
End Class

但是,我敢打赌您一开始并不真的需要这些内部属性。如果没有,您可以使 class 更简单:

Friend Class CustomDashedLineSeparator
    Inherits iTextSharp.text.pdf.draw.DottedLineSeparator

    Public Property Dash As Single
    Public Property Phase As Single

    Public Overrides Sub Draw(ByVal canvas As PdfContentByte, ByVal llx As Single, ByVal lly As Single, ByVal urx As Single, ByVal ury As Single, ByVal y As Single)
        canvas.SaveState()
        canvas.SetLineWidth(LineWidth)
        canvas.SetLineDash(Dash, Gap, phase)
        DrawLine(canvas, llx, urx, y)
        canvas.RestoreState()
    End Sub
End Class

最后,请永远不要 try/catch 巨大的代码块。充其量,您的应用程序会礼貌地崩溃,但您不知道为什么,也永远无法修复它。虽然不那么漂亮 "Unable to open file XYZ.pdf for writing" 比 "Unable to create PDF" 更有帮助。