我正在尝试使用iTextSharp
创建一个PDF,它从表单中读取值,这些值是从DataGridView
插入的。
因此,在DataGridView
的特定行数之后,我想创建一个包含PdfFile.NewPage()
的新页面
此代码将DataGridView
写入PDF:
For i As Integer = 0 To DataGridView1.Rows.Count - 2
For j As Integer = 0 To DataGridView1.Columns.Count - 1
pdfcell = New PdfPCell(New Phrase(DataGridView1(j, i).Value.ToString(), pTable))
PdfTable.HorizontalAlignment = PdfPCell.ALIGN_LEFT
PdfTable.AddCell(pdfcell)
Next
Next
PdfFile.Add(PdfTable)
这样做的原因是DataGridView
会覆盖页脚。
在DataGridView
行之后,应跟随PdfFile.NewPage()
行。在第二页上,它应该继续使用21 DataGridView
行。
例如,50行之后是一个新页面。
这是页脚的代码:
Inherits PdfPageEventHelper
Public Overrides Sub OnendPage(ByVal writer As iTextSharp.text.pdf.PdfWriter, ByVal document As iTextSharp.text.Document)
Dim bf As BaseFont = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED)
Dim cb As PdfContentByte = writer.DirectContent
cb.BeginText()
cb.SetFontAndSize(bf, 12)
cb.SetTextMatrix(50, 30)
cb.ShowText("TEXTTEXT " & writer.PageNumber)
cb.EndText()
我已经尝试过了。它只是创建下一页但仍覆盖页脚:
For i As Integer = 0 To DataGridView1.Rows.Count - 2
For j As Integer = 0 To DataGridView1.Columns.Count - 1
pdfcell = New PdfPCell(New Phrase(DataGridView1(j, i).Value.ToString(), pTable))
PdfTable.HorizontalAlignment = PdfPCell.ALIGN_LEFT
PdfTable.AddCell(pdfcell)
Next
Select Case i
Case 20, 30, 60, 99
PdfFile.NewPage()
End Select
Next
PdfFile.Add(PdfTable)