如何在itextsharp pdf中打破pdf页面

时间:2016-11-11 14:09:51

标签: vb.net pdf itext

我编写了以下代码来生成pdf:

path = Server.MapPath("PDF-Files")
filename = path + "/mydata.pdf"

document = New iTextSharp.text.Document(iTextSharp.text.PageSize.A4, 5.0F, 5.0F, 5.0F, 5.0F)

Dim bfTimes As BaseFont
Dim times As iTextSharp.text.Font

bfTimes = BaseFont.CreateFont(BaseFont.TIMES_ROMAN, BaseFont.CP1252, False)
times = New iTextSharp.text.Font(bfTimes, 8, iTextSharp.text.Font.NORMAL)

Dim writer As PdfWriter
writer = PdfWriter.GetInstance(document, New FileStream(filename, FileMode.Create))
Dim ev As New itsEvents
writer.PageEvent = ev

If document.IsOpen Then

    document.Close()

End If

document.Open()

Dim spacing As Integer

spacing = 0

Dim curY, lineHeight As Double

curY = document.Top
lineHeight = 0

Const maxPerLine As Integer = 3

For k As Integer = 0 To ds.Tables(0).Rows.Count - 1

    Dim table As PdfPTable
    table = New PdfPTable(3)

    table.DefaultCell.Border = iTextSharp.text.Rectangle.NO_BORDER
    table.TotalWidth = 200.0F
    table.LockedWidth = True

    Dim cell As PdfPCell
    cell = New PdfPCell()
    cell.AddElement(New iTextSharp.text.Paragraph(ds.Tables(0).Rows(k)("id").ToString(), times))
    cell.AddElement(New iTextSharp.text.Paragraph(ds.Tables(0).Rows(k)("name").ToString() + " " + dsLabelTemp.Tables(0).Rows(k)("city").ToString() + "(" + dsLabelTemp.Tables(0).Rows(k)("post").ToString() + ")", times))
cell.AddElement(New iTextSharp.text.Paragraph(ds.Tables(0).Rows(k)("userpersonal").ToString(), times))
    cell.Colspan = 3
    cell.HorizontalAlignment = 0
    cell.Border = iTextSharp.text.Rectangle.NO_BORDER
    cell.Padding = 20.0F
    table.AddCell(cell)


    table.WriteSelectedRows(0, -1, document.Left + spacing, curY, writer.DirectContent)

    spacing = spacing + 200

    lineHeight = Math.Max(lineHeight, table.TotalHeight)

    If 0 = (k + 1) Mod maxPerLine Then

        curY = curY - lineHeight
        spacing = 0
        lineHeight = 0

    End If

    Next

    document.Close()
    ShowPdf(filename)

当上面的代码以完美的方式执行并给出输出但是它没有显示if page 1是否已完成。

enter image description here

在上图中,您可以检查它是否显示完整记录。

如果页面已满或者增加页面高度,我想打破页面。

如果我的第一页已满,如何将数据传输到第二页?

1 个答案:

答案 0 :(得分:0)

我得到了解决方案。

以下是我的新代码:

    Dim table As PdfPTable
        table = New PdfPTable(4)

        table.DefaultCell.Border = iTextSharp.text.Rectangle.NO_BORDER
        table.TotalWidth = 400.0F
        table.LockedWidth = True

    For k As Integer = 0 To ds.Tables(0).Rows.Count - 1

        Dim cell As PdfPCell
        cell = New PdfPCell()
        cell.AddElement(New iTextSharp.text.Paragraph(ds.Tables(0).Rows(k)("id").ToString(), times))
        cell.AddElement(New iTextSharp.text.Paragraph(ds.Tables(0).Rows(k)("name").ToString() + " " + dsLabelTemp.Tables(0).Rows(k)("city").ToString() + "(" + dsLabelTemp.Tables(0).Rows(k)("post").ToString() + ")", times))
        cell.AddElement(New iTextSharp.text.Paragraph(ds.Tables(0).Rows(k)("userpersonal").ToString(), times))
        cell.Colspan = 1
        cell.HorizontalAlignment = 0
        cell.Border = iTextSharp.text.Rectangle.NO_BORDER
        cell.Padding = 20.0F
        table.AddCell(cell)
    Next

 document.Add(table)
 document.Close()
 ShowPdf(filename)