首页 > 解决方案 > 如何使用VB和itextsharp在标题页面后在固定点添加表格

问题描述

我有以下代码创建两个标题表和一个要遵循的表。我无法在标题之后立即插入下表。它们都从页面顶部开始,第二个表格应该在灰色标题网格之后立即开始。每一页都必须重复

Public Class Form1

    Public Doc As Document
    Public wri As PdfWriter

    Public fntNormal12 As iTextSharp.text.Font = FontFactory.GetFont("Arial", 12, iTextSharp.text.Font.NORMAL, BaseColor.BLACK)
    Public TableGrid As PdfPTable '= New PdfPTable(5)
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

        Doc = New Document(PageSize.A4, 20, 20, 20, 20)

        Dim ev As _events = New _events() ' ora

        wri = PdfWriter.GetInstance(Doc, New FileStream("simple2.pdf", FileMode.Create))
        ' evento per nuova pagina

        wri.PageEvent = ev  ' ora

        Doc.Open()

        TableGrid = New PdfPTable(5) ' diventata pubblica

        TableGrid.WidthPercentage = 100 ' Table size is set to 100% of the page
        TableGrid.HorizontalAlignment = 0 '0=left, 1=centre , 2 = right
        TableGrid.SpacingAfter = 10   ' mette uno spazio di 10 ,  dopo

        Dim TableGridWidths(4) As Single
        TableGridWidths(0) = 150 ' se li metto tutti e 3 uguali è come se faccio il 33%
        TableGridWidths(1) = 200
        TableGridWidths(2) = 500
        TableGridWidths(3) = 50
        TableGridWidths(4) = 100

        TableGrid.SetWidths(TableGridWidths) ' Set the column widths on table creation. Unlike HTML cells cannot be sized.

        Try
            For i = 0 To 100

                '2° riga verde
                Dim GrillCell1 As New PdfPCell(New Phrase("Pos." & i, fntNormal12))
                'CellOne.Rotation = -90 ' inclina il testo
                GrillCell1.HorizontalAlignment = 1 '0=left, 1=centre , 2 = right
                TableGrid.AddCell(GrillCell1)

                Dim GrillCell2 As New PdfPCell(New Phrase("PS" & i, fntNormal12))
                'CellTwo.Border = 0
                GrillCell2.HorizontalAlignment = 1 '0=left, 1=centre , 2 = right
                TableGrid.AddCell(GrillCell2)

                Dim GrillCell3 As New PdfPCell(New Phrase("DDDDDDDDDDDD" & i, fntNormal12))
                GrillCell3.HorizontalAlignment = 1 '0=left, 1=centre , 2 = right
                TableGrid.AddCell(GrillCell3)

                Dim GrillCell4 As New PdfPCell(New Phrase(i, fntNormal12))
                GrillCell4.HorizontalAlignment = 1 '0=left, 1=centre , 2 = right
                TableGrid.AddCell(GrillCell4)

                Dim GrillCell5 As New PdfPCell(New Phrase(i, fntNormal12))
                GrillCell5.HorizontalAlignment = 1 '0=left, 1=centre , 2 = right
                TableGrid.AddCell(GrillCell5)

            Next i

            Doc.Add(TableGrid)

        Catch ex As Exception
            MsgBox(ex.ToString)

            Doc.Close()

        End Try

        Doc.Close()

        Process.Start("simple2.pdf")

    End Sub

End Class


Class _events
    Inherits PdfPageEventHelper

    Public Vvv = "VVV11111"
    Public Ppp = "PPP 11111"
    Public Vv = "VV 11111"
    Public Ggg = "GGG 11111"
    Public Pppp = "3333"
    Public Ttt = "tttttttttt"
    Public myTable As PdfPTable
    Public myTableBC As PdfPTable
    Public TableGridInt As PdfPTable '= New PdfPTable(5)

    Public Logo = "tttttttttt"
    'Public logo = iTextSharp.text.Image.GetInstance(Application.StartupPath & "\" & "MioLogo.jpg")
    ' settaggio intestazione
    Public fntBOLD18 As iTextSharp.text.Font = FontFactory.GetFont("Arial", 18, iTextSharp.text.Font.BOLD, BaseColor.BLACK)
    ' altro settaggio verde
    Public fntBOLD12 As iTextSharp.text.Font = FontFactory.GetFont("Arial", 12, iTextSharp.text.Font.BOLD, BaseColor.BLACK)
    Public fntBOLD8 As iTextSharp.text.Font = FontFactory.GetFont("Arial", 8, iTextSharp.text.Font.BOLD, BaseColor.BLACK)

    ' altro settaggio verde
    Public fntNormal18 As iTextSharp.text.Font = FontFactory.GetFont("Arial", 18, iTextSharp.text.Font.NORMAL, BaseColor.BLACK)
    Public fntNormal12 As iTextSharp.text.Font = FontFactory.GetFont("Arial", 12, iTextSharp.text.Font.NORMAL, BaseColor.BLACK)
    Public fntNormal8 As iTextSharp.text.Font = FontFactory.GetFont("Arial", 8, iTextSharp.text.Font.NORMAL, BaseColor.BLACK)

    Public Overrides Sub OnEndPage(ByVal writer As PdfWriter, ByVal document As Document)

        myTable = New PdfPTable(4)

        myTable.WidthPercentage = 100 ' Table size is set to 100% of the page
        myTable.HorizontalAlignment = 0 '0=left, 1=centre , 2 = right
        myTable.SpacingAfter = 10   ' mette uno spazio di 10 ,  dopo

        Dim sglTblHdWidths(3) As Single
        sglTblHdWidths(0) = 150 ' se li metto tutti e 3 uguali è come se faccio il 33%
        sglTblHdWidths(1) = 580
        sglTblHdWidths(2) = 140
        sglTblHdWidths(3) = 130

        myTable.SetWidths(sglTblHdWidths) ' Set the column widths on table creation. Unlike HTML cells cannot be sized.

        myTable.AddCell(logo)

        Dim CellTwoHdr As New PdfPCell(New Phrase("Mia Stampa", fntBOLD18)) ' font intestazione rosso
        CellTwoHdr.HorizontalAlignment = 1
        myTable.AddCell(CellTwoHdr)

        Dim CellTreeHdr As New PdfPCell(New Phrase(("38 38     "), fntBOLD12)) ' font intestazione rosso
        myTable.AddCell(CellTreeHdr)

        Dim Cell4Hdr As New PdfPCell(New Phrase(("Mio canale: " & " 34"), fntNormal8)) ' font intestazione rosso
        Cell4Hdr.HorizontalAlignment = 2
        Cell4Hdr.VerticalAlignment = 2 ' non funziona
        myTable.AddCell(Cell4Hdr)


        '2° riga verde
        Dim CellOne As New PdfPCell(New Phrase("PROVA", fntNormal12))
        'CellOne.Rotation = -90 ' inclina il testo
        myTable.AddCell(CellOne)

        Dim CellTwo As New PdfPCell(New Phrase("", fntNormal8))
        'CellTwo.Border = 0
        myTable.AddCell(CellTwo)

        Dim CellTree As New PdfPCell(New Phrase(Now, fntNormal8))
        myTable.AddCell(CellTree)

        Dim Cell4 As New PdfPCell(New Phrase("p  di  p ", fntNormal8))
        Cell4.HorizontalAlignment = 2
        myTable.AddCell(Cell4)

        document.Add(myTable)

        TableGridInt = New PdfPTable(5) ' diventata pubblica

        TableGridInt.WidthPercentage = 100 ' Table size is set to 100% of the page
        TableGridInt.HorizontalAlignment = 0 '0=left, 1=centre , 2 = right
        TableGridInt.SpacingAfter = 10   ' mette uno spazio di 10 ,  dopo

        Dim TableGridWidths(4) As Single
        TableGridWidths(0) = 150 ' se li metto tutti e 3 uguali è come se faccio il 33%
        TableGridWidths(1) = 200
        TableGridWidths(2) = 500
        TableGridWidths(3) = 50
        TableGridWidths(4) = 100

        TableGridInt.SetWidths(TableGridWidths) ' Set the column widths on table creation. Unlike HTML cells cannot be sized.

        '2° riga verde
        Dim IntCol1 As New PdfPCell(New Phrase("Point1", fntBOLD8))
        'CellOne.Rotation = -90 ' inclina il testo
        IntCol1.HorizontalAlignment = 1 '0=left, 1=centre , 2 = right
        IntCol1.BackgroundColor = BaseColor.LIGHT_GRAY
        TableGridInt.AddCell(IntCol1)

        Dim IntCol2 As New PdfPCell(New Phrase("Pt2", fntBOLD8))
        'CellTwo.Border = 0
        IntCol2.HorizontalAlignment = 1 '0=left, 1=centre , 2 = right
        IntCol2.BackgroundColor = BaseColor.LIGHT_GRAY
        TableGridInt.AddCell(IntCol2)

        Dim IntCol3 As New PdfPCell(New Phrase("Des", fntBOLD8))
        IntCol3.HorizontalAlignment = 1 '0=left, 1=centre , 2 = right
        IntCol3.BackgroundColor = BaseColor.LIGHT_GRAY
        TableGridInt.AddCell(IntCol3)

        Dim IntCol4 As New PdfPCell(New Phrase("Q.", fntBOLD8))
        IntCol4.HorizontalAlignment = 1 '0=left, 1=centre , 2 = right
        IntCol4.BackgroundColor = BaseColor.LIGHT_GRAY
        TableGridInt.AddCell(IntCol4)

        Dim IntCol5 As New PdfPCell(New Phrase("Point5", fntBOLD8))
        IntCol5.HorizontalAlignment = 1 '0=left, 1=centre , 2 = right
        IntCol5.BackgroundColor = BaseColor.LIGHT_GRAY
        TableGridInt.AddCell(IntCol5)

        document.Add(TableGridInt)

    End Sub

我已经尝试过使用 WriteSelectedRows 方法,但结果是它永远不会进入 OnEndPage 事件,因此我最多有大约 28 或 30 个表行,而不是其他行。

那可能是什么问题?

标签: vb.netpdfitext

解决方案


推荐阅读