【问题标题】:How do I add a footer in a PDF using iTextSharp VB.net?如何使用 iTextSharp VB.net 在 PDF 中添加页脚?
【发布时间】:2018-05-25 23:07:49
【问题描述】:

我有一段代码可以生成我的 PDF。 我试图弄清楚如何为每个页面添加页脚,但我不知道如何。我在 C# 中看到了很多示例,但我似乎无法找到如何使用 VB.net 准确添加页脚(C# 技能不是那么好)。

我相信正确的方法是使用 OnEndPage 功能,但我现在不知道如何在我的代码中添加它。 我是新手,所以任何帮助都会很棒。

下面的代码是我现在使用的。

Private Sub GeneratePDF()


    Dim pdfDoc As New Document()
    Dim pdfWrite As PdfWriter = PdfWriter.GetInstance(pdfDoc, New FileStream("\\testserver\verkoop\offerte v2\Offerte " & offertenummer2 & "-" & offertenummer & " " & TextBox2.Text & ".pdf", FileMode.Create))

    Dim FontColour As BaseColor = New BaseColor(35, 31, 32)
    Dim Calibri8 As Font = FontFactory.GetFont("Calibri", 10, FontColour)

    Dim img As Image = Image.GetInstance("Resources\Offerte-NL.png")

    pdfDoc.Open()

    'witte lijn

    Dim whiteline As New PdfPCell(New Phrase(" "))
    whiteline.Colspan = 2
    whiteline.HorizontalAlignment = 0
    whiteline.Border = Rectangle.NO_BORDER

    'foto inladen
    If RadioButton5.Checked = True Then

        img = Image.GetInstance("Resources\Offerte-NL.png")
        img.ScalePercent(18.0F, 18.0F)
        img.Alignment = 0
        pdfDoc.Add(img)

    ElseIf RadioButton4.Checked = True Then

        img = Image.GetInstance("Resources\Offerte-BE.png")
        img.ScalePercent(18.0F, 18.0F)
        img.Alignment = 0
        pdfDoc.Add(img)

    End If


    'Eerste table in PDF voor klantgegevens

    Dim klanttable As New PdfPTable(2)
    klanttable.TotalWidth = 350.0F
    klanttable.LockedWidth = True
    klanttable.HorizontalAlignment = 0
    'klanttable.DefaultCell.Border = Rectangle.NO_BORDER

    Dim sglTblHdWidths(1) As Single
    sglTblHdWidths(0) = 120
    sglTblHdWidths(1) = 230
    klanttable.SetWidths(sglTblHdWidths)

    Dim kollommatenproduct(5) As Single
    kollommatenproduct(0) = 45
    kollommatenproduct(1) = 45
    kollommatenproduct(2) = 200
    kollommatenproduct(3) = 65
    kollommatenproduct(4) = 60
    kollommatenproduct(5) = 55


    klanttable.SetWidths(sglTblHdWidths)




    Dim cell As New PdfPCell(New Phrase("Debiteur gegevens"))

    cell.Colspan = 2
    cell.HorizontalAlignment = 0
    cell.Border = Rectangle.NO_BORDER

    Dim debnr As String = TextBox1.Text
    Dim bn As String = TextBox2.Text
    Dim adr As String = TextBox3.Text
    Dim pcwp As String = TextBox4.Text
    Dim cp As String = TextBox5.Text
    Dim km As String = TextBox6.Text

    klanttable.AddCell(cell)
    klanttable.AddCell(whiteline)
    klanttable.AddCell(New Phrase("Debiteur nr.: ", Calibri8))
    klanttable.AddCell(New Phrase(debnr, Calibri8))
    klanttable.AddCell(New Phrase("(Bedrijfs)naam:", Calibri8))
    klanttable.AddCell(New Phrase(bn, Calibri8))
    klanttable.AddCell(New Phrase("Adres:", Calibri8))
    klanttable.AddCell(New Phrase(adr, Calibri8))
    klanttable.AddCell(New Phrase("Postcode & woonplaats:", Calibri8))
    klanttable.AddCell(New Phrase(pcwp, Calibri8))
    klanttable.AddCell(New Phrase("Contactpersoon", Calibri8))
    klanttable.AddCell(New Phrase(cp, Calibri8))
    klanttable.AddCell(New Phrase("Kenmerk:", Calibri8))
    klanttable.AddCell(New Phrase(km, Calibri8))
    klanttable.SpacingBefore = 50.0F
    klanttable.SpacingAfter = 100.0F


    pdfDoc.Add(klanttable)

    'Offerte nummer tabel

    Dim offerteinfotable As New PdfPTable(2)
    offerteinfotable.TotalWidth = 350.0F
    offerteinfotable.LockedWidth = True
    offerteinfotable.HorizontalAlignment = 0
    'offerteinfotable.DefaultCell.Border = Rectangle.NO_BORDER

    offerteinfotable.SetWidths(sglTblHdWidths)


    Dim cell2 As New PdfPCell(New Phrase("Offerte informatie"))
    cell2.Colspan = 2
    cell2.HorizontalAlignment = 0
    cell2.Border = Rectangle.NO_BORDER

    offerteinfotable.AddCell(cell2)
    offerteinfotable.AddCell(whiteline)
    offerteinfotable.AddCell(New Phrase("Offertenummer:", Calibri8))
    offerteinfotable.AddCell(New Phrase(offertenummer2.ToString + "-" + offertenummer.ToString, Calibri8))
    offerteinfotable.AddCell(New Phrase("Datum:", Calibri8))
    offerteinfotable.AddCell(New Phrase(DateTime.Now.Date.ToShortDateString, Calibri8))
    offerteinfotable.AddCell(New Phrase("Contactpersoon:", Calibri8))
    offerteinfotable.AddCell(New Phrase(Login.TextBox3.Text.ToString, Calibri8))
    offerteinfotable.SpacingAfter = 100.0F

    pdfDoc.Add(offerteinfotable)


    'Extra informatie offerte

    Dim offerteextrainfotable As New PdfPTable(2)
    offerteextrainfotable.TotalWidth = 350.0F
    offerteextrainfotable.LockedWidth = True
    offerteextrainfotable.HorizontalAlignment = 0
    'offerteinfotable.DefaultCell.Border = Rectangle.NO_BORDER

    offerteextrainfotable.SetWidths(sglTblHdWidths)


    Dim cell3 As New PdfPCell(New Phrase("Leverings informatie"))
    cell3.Colspan = 2
    cell3.HorizontalAlignment = 0
    cell3.Border = Rectangle.NO_BORDER

    offerteextrainfotable.AddCell(cell3)
    offerteextrainfotable.AddCell(whiteline)
    offerteextrainfotable.AddCell(New Phrase("Betalingscondities:", Calibri8))
    offerteextrainfotable.AddCell(New Phrase(ComboBox3.Text.ToString, Calibri8))
    offerteextrainfotable.AddCell(New Phrase("Levertermijn:", Calibri8))
    offerteextrainfotable.AddCell(New Phrase(ComboBox4.Text.ToString, Calibri8))
    offerteextrainfotable.AddCell(New Phrase("Extra informatie:", Calibri8))
    offerteextrainfotable.AddCell(New Phrase(TextBox7.Text.ToString, Calibri8))

    pdfDoc.Add(offerteextrainfotable)


    'pagina 2
    pdfDoc.NewPage()

    'Header invoegen
    pdfDoc.Add(img)

    'producten invoegen

    Dim productentable As New PdfPTable(6)
    productentable.TotalWidth = 530.0F
    productentable.LockedWidth = True
    productentable.HorizontalAlignment = 0
    productentable.DefaultCell.PaddingBottom = 5
    productentable.DefaultCell.PaddingTop = 5


    productentable.SetWidths(kollommatenproduct)

    productentable.AddCell(New Phrase("Aantal", Calibri8))
    productentable.AddCell(New Phrase("Art. Nr.", Calibri8))
    productentable.AddCell(New Phrase("Omschrijving", Calibri8))
    productentable.AddCell(New Phrase("Capaciteit", Calibri8))
    productentable.AddCell(New Phrase("Prijs per stuk", Calibri8))
    productentable.AddCell(New Phrase("Prijs totaal", Calibri8))

    Dim totaalincbtw As Decimal = 0.00
    Dim totaalexbtw As Decimal = 0.00


    For Each Row In DataGridView1.Rows

        If Not Row.IsNewRow Then

            Dim aantal As String = Row.Cells(0).Value.ToString
            Dim artikelnummer As String = Row.Cells(1).Value.ToString
            Dim omschrijving As String = Row.Cells(5).Value.ToString
            Dim capaciteit As String = Row.Cells(2).Value.ToString + Row.Cells(3).Value.ToString
            Dim prijsperstukinc As Decimal = Row.Cells(4).Value.ToString
            Dim prijsperstuk As Decimal = Math.Round(prijsperstukinc / 1.21, 2)
            Dim tot1 As Decimal = Row.Cells(0).Value.ToString
            Dim tot2 As Decimal = Row.Cells(4).Value.ToString
            Dim totaalprijsex As Decimal = Math.Round(tot1 * tot2 / 1.21, 2)
            Dim totaalprijsinc As Decimal = Math.Round(tot1 * tot2, 2)

            totaalincbtw = totaalincbtw + totaalprijsinc
            totaalexbtw = totaalexbtw + totaalprijsex

            productentable.AddCell(New Phrase(aantal, Calibri8))
            productentable.AddCell(New Phrase(artikelnummer, Calibri8))
            productentable.AddCell(New Phrase(omschrijving, Calibri8))
            productentable.AddCell(New Phrase(capaciteit, Calibri8))
            productentable.AddCell(New Phrase("€ " + prijsperstuk.ToString, Calibri8))
            productentable.AddCell(New Phrase("€ " + totaalprijsex.ToString, Calibri8))



        End If

    Next


    'totaal bedragen toevoegen

    Dim btw As Decimal = totaalincbtw - totaalexbtw

    Dim blankcell As New PdfPCell(New Phrase(" "))
    blankcell.Colspan = 3
    blankcell.HorizontalAlignment = 0
    blankcell.Border = Rectangle.NO_BORDER

    Dim subtotaalcell As New PdfPCell(New Phrase("Subtotaal:", Calibri8))
    subtotaalcell.UseVariableBorders = True
    subtotaalcell.Border = Rectangle.BOTTOM_BORDER + Rectangle.TOP_BORDER + Rectangle.LEFT_BORDER


    Dim btw21cell As New PdfPCell(New Phrase("BTW 21%", Calibri8))
    btw21cell.UseVariableBorders = True
    btw21cell.Border = Rectangle.BOTTOM_BORDER + Rectangle.TOP_BORDER + Rectangle.LEFT_BORDER



    Dim totaalbedragcell As New PdfPCell(New Phrase("Totaalbedrag:", Calibri8))
    totaalbedragcell.UseVariableBorders = True
    totaalbedragcell.Border = Rectangle.BOTTOM_BORDER + Rectangle.TOP_BORDER + Rectangle.LEFT_BORDER



    Dim tussencel As New PdfPCell(New Phrase(" ", Calibri8))
    tussencel.UseVariableBorders = True
    tussencel.Border = Rectangle.BOTTOM_BORDER + Rectangle.TOP_BORDER
    tussencel.PaddingBottom = 5



    Dim subtotaalcell2 As New PdfPCell(New Phrase("€ " + totaalexbtw.ToString, Calibri8))
    subtotaalcell2.UseVariableBorders = True
    subtotaalcell2.Border = Rectangle.BOTTOM_BORDER + Rectangle.RIGHT_BORDER + Rectangle.TOP_BORDER



    Dim btw21cell2 As New PdfPCell(New Phrase("€ " + btw.ToString, Calibri8))
    btw21cell2.UseVariableBorders = True
    btw21cell2.Border = Rectangle.BOTTOM_BORDER + Rectangle.RIGHT_BORDER + Rectangle.TOP_BORDER



    Dim totaalbedragcell2 As New PdfPCell(New Phrase("€ " + totaalincbtw.ToString, Calibri8))
    totaalbedragcell2.UseVariableBorders = True
    totaalbedragcell2.Border = Rectangle.BOTTOM_BORDER + Rectangle.RIGHT_BORDER + Rectangle.TOP_BORDER



    productentable.AddCell(blankcell)
    productentable.AddCell(blankcell)

    productentable.AddCell(blankcell)
    productentable.AddCell(subtotaalcell)
    productentable.AddCell(tussencel)
    productentable.AddCell(subtotaalcell2)


    productentable.AddCell(blankcell)
    productentable.AddCell(btw21cell)
    productentable.AddCell(tussencel)
    productentable.AddCell(btw21cell2)

    productentable.AddCell(blankcell)
    productentable.AddCell(totaalbedragcell)
    productentable.AddCell(tussencel)
    productentable.AddCell(totaalbedragcell2)



    pdfDoc.Add(productentable)


    pdfDoc.Close()


End Sub

编辑:

这是答案:)! 将下一段代码添加到我的代码中。

Public Class itsEvents
    Inherits PdfPageEventHelper

    Public Overrides Sub OnEndPage(ByVal writer As iTextSharp.text.pdf.PdfWriter, ByVal document As iTextSharp.text.Document)

        Dim FontColour As BaseColor = New BaseColor(0, 0, 0)
        Dim Calibri6 As Font = FontFactory.GetFont("Calibri", 6, FontColour)



        Dim algemenevoorwaarden As New PdfPTable(1)
        algemenevoorwaarden.HorizontalAlignment = 1

        Dim cell3 As New PdfPCell(New Phrase(" "))
        cell3.HorizontalAlignment = 1
        cell3.Border = Rectangle.NO_BORDER
        Dim cell4 As New PdfPCell(New Phrase("Textline 1", Calibri6))
        cell4.HorizontalAlignment = 1
        cell4.Border = Rectangle.NO_BORDER
        Dim cell5 As New PdfPCell(New Phrase("Textline 2", Calibri6))
        cell5.HorizontalAlignment = 1
        cell5.Border = Rectangle.NO_BORDER
        Dim cell6 As New PdfPCell(New Phrase("Textline 3", Calibri6))
        cell6.HorizontalAlignment = 1
        cell6.Border = Rectangle.NO_BORDER

        algemenevoorwaarden.AddCell(cell3)
        algemenevoorwaarden.AddCell(cell4)
        algemenevoorwaarden.AddCell(cell5)
        algemenevoorwaarden.AddCell(cell6)

        document.Add(algemenevoorwaarden)




    End Sub

End Class

【问题讨论】:

  • Dag Jeffrey, waarom gebruik je een oude versie van iText? Waarom gebruik je iText 7 niet? (嗨 Jeffrey,你为什么使用旧版本的 iText?你为什么不使用 iText 7?)
  • 我在网上找到了 iTextSharp,并按照 Youtube 上的基本教程进行操作,我就在这里。如果我切换到 iText 7,我需要重写我的代码吗?
  • 是的,iText 7 是对 iText 的完全重写。你需要重写你的代码,但这是值得的。对 AGPL 用户的自愿支持已停止。不使用 AGPL 下的 iText(例如,因为他们不想开源自己的商业项目)并因此拥有商业许可证的人,如果他们续签支持合同,仍然可以获得支持。
  • 这花了我 2 天的时间来完成我想要的工作,除了页脚。你能帮我在 iTextSharp 5 中让它工作,这样我就可以展示一些东西,然后在 iTextSharp 7 中重写。我也不知道我使用的是旧版本,我在完成所有工作后才知道。
  • 我上一次写 VB 代码是在 1996 年(是的,那是 21 年前)。我没有打算再回到 VB,所以你得问问别人。

标签: vb.net itext pdf-generation


【解决方案1】:

这就是答案,刚刚添加到代码中,现在页面事件被触发了。

Public Class itsEvents
Inherits PdfPageEventHelper

Public Overrides Sub OnEndPage(ByVal writer As iTextSharp.text.pdf.PdfWriter, ByVal document As iTextSharp.text.Document)

    Dim FontColour As BaseColor = New BaseColor(0, 0, 0)
    Dim Calibri6 As Font = FontFactory.GetFont("Calibri", 6, FontColour)



    Dim algemenevoorwaarden As New PdfPTable(1)
    algemenevoorwaarden.HorizontalAlignment = 1

    Dim cell3 As New PdfPCell(New Phrase(" "))
    cell3.HorizontalAlignment = 1
    cell3.Border = Rectangle.NO_BORDER
    Dim cell4 As New PdfPCell(New Phrase("Textline 1", Calibri6))
    cell4.HorizontalAlignment = 1
    cell4.Border = Rectangle.NO_BORDER
    Dim cell5 As New PdfPCell(New Phrase("Textline 2", Calibri6))
    cell5.HorizontalAlignment = 1
    cell5.Border = Rectangle.NO_BORDER
    Dim cell6 As New PdfPCell(New Phrase("Textline 3", Calibri6))
    cell6.HorizontalAlignment = 1
    cell6.Border = Rectangle.NO_BORDER

    algemenevoorwaarden.AddCell(cell3)
    algemenevoorwaarden.AddCell(cell4)
    algemenevoorwaarden.AddCell(cell5)
    algemenevoorwaarden.AddCell(cell6)

    document.Add(algemenevoorwaarden)




End Sub

End Class

【讨论】:

    猜你喜欢
    • 2013-09-30
    • 2020-07-23
    • 2012-05-17
    • 2013-04-15
    • 2011-01-31
    • 1970-01-01
    • 2012-04-28
    • 2015-09-15
    • 1970-01-01
    相关资源
    最近更新 更多