如何使用iTextSharp VB.net在PDF中添加页脚? [英] How do I add a footer in a PDF using iTextSharp VB.net?

查看:109
本文介绍了如何使用iTextSharp VB.net在PDF中添加页脚?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a piece of code that generates my PDF. I am trying to figure out how to add a footer to every page but I don't know how. I have seen a lot of examples in C# but I can't seem to find out how to exactly add the footer using VB.net (C# skills aren't that great).

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

I believe that the correct way is to use the OnEndPage feature but I have no idea on how to add that in my code at this moment. I am new to this so any help would be great.

下面的代码是我现在正在使用的代码.

The code below is what I am using now.

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

这是答案:)! 在我的代码中添加了下一段代码.

Here is the answer :) ! Added the next piece of code to my code.

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

推荐答案

这个答案刚刚添加到代码中,现在触发了page事件.

This the answer, just added this to the code and now the page event is triggered.

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

这篇关于如何使用iTextSharp VB.net在PDF中添加页脚?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆