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

时间:2015-04-13 17:26:03

标签: itextsharp

我已经完成了自定义虚线分隔符示例但无法使用     在Visual Basic中做这个在vb中有任何例子

我收到以下错误

1)“Dash”已在此课程中声明为“受保护的朋友短划线”。

2)此阶段已将“阶段”声明为“受保护的朋友阶段为单身”。

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

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

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

6)子'draw'在基类'DottedLineSeparator'中隐藏一个可覆盖的方法。要覆盖基本方法,必须将此方法声明为“覆盖”。

 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

1 个答案:

答案 0 :(得分:1)

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

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

对于第六个错误,您尝试覆盖方法而不明确告诉VB您想要的方法。为此,您需要使用Overrides代替Overridable

您清理过的课程应如下所示:

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

然而,我敢打赌你一开始并不真正需要这些内部属性。如果没有,你可以让这个课更简单:

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巨大的代码块。充其量,你的应用程序会礼貌地崩溃,但你不知道为什么,你永远无法解决它。虽然不那么漂亮"无法打开文件XYZ.pdf进行写作"比#34更有帮助;无法创建PDF"。