Я использую itextsharp для создания PDF. Я получаю проблемы при вставке верхнего и нижнего колонтитула с помощью обработчиков событий. Вот мой код.
Imports System.Data
Imports System.Data.SqlClient
Imports iTextSharp.text
Imports iTextSharp.text.pdf
Imports System.IO
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
cmd.Connection = con
If con.State = ConnectionState.Open Then con.Close()
con.Open()
Dim combo As SqlCommand
combo = New SqlCommand("select * from login2", con)
Dim da1 As SqlDataAdapter = New SqlDataAdapter(combo)
Dim ds1 As DataSet = New DataSet()
Dim dt1 As DataTable = New DataTable()
da1.Fill(ds1)
da1.Fill(dt1)
DataGridView1.DataSource = dt1
DataGridView1.Refresh()
con.Close()
da1.Dispose()
SaveFileDialog1.FileName = ""
SaveFileDialog1.Filter = "PDF (*.pdf)|*.pdf"
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
SaveFileDialog1.FileName = ""
If SaveFileDialog1.ShowDialog = DialogResult.OK Then
TextBox1.Text = SaveFileDialog1.FileName
End If
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Dim Paragraph As New Paragraph ' declaration for new paragraph
Dim PdfFile As New Document(PageSize.A4, 40, 40, 40, 20) ' set pdf page size
PdfFile.AddTitle("Text Book") ' set our pdf title
Dim Write As PdfWriter = PdfWriter.GetInstance(PdfFile, New FileStream(TextBox1.Text, FileMode.Create))
Dim ev As New itsEvents
Write.PageEvent = ev
'Dim ev1 As New itsEvents5
'Write.PageEvent = ev1
PdfFile.Open()
Dim pTitle As New Font(iTextSharp.text.Font.FontFamily.TIMES_ROMAN, 14, iTextSharp.text.Font.BOLD, BaseColor.BLACK)
Dim pTable As New Font(iTextSharp.text.Font.FontFamily.TIMES_ROMAN, 12, iTextSharp.text.Font.NORMAL, BaseColor.BLACK)
' insert title into pdf file
Paragraph.SpacingAfter = 10.0F
Paragraph = New Paragraph(New Chunk("Batch Audit Reports", pTitle))
Paragraph.Alignment = Element.ALIGN_CENTER
paragraph.SpacingAfter = 5.0F
pdffile.Add(paragraph)
' create data into table
Dim PdfTable As New PdfPTable(DataGridView1.Columns.Count)
' setting width of table
PdfTable.TotalWidth = 500.0F
PdfTable.LockedWidth = True
Dim widths(0 To DataGridView1.Columns.Count - 1) As Single
widths(0) = 50.0F
widths(1) = 65.0F
widths(2) = 50.0F
widths(3) = 70.0F
widths(4) = 70.0F
widths(5) = 110.0F
widths(6) = 85.0F
PdfTable.SetWidths(widths)
PdfTable.HorizontalAlignment = 0
PdfTable.SpacingBefore = 5.0F
' declaration pdf cells
Dim pdfcell As PdfPCell = New PdfPCell
' create pdf header
For i As Integer = 0 To DataGridView1.Columns.Count - 1
If i = 0 Then
pdfcell = New PdfPCell(New Phrase(New Chunk("ID", pTable)))
' alignment header table
pdfcell.BackgroundColor = BaseColor.GRAY
pdfcell.HorizontalAlignment = PdfPCell.ALIGN_CENTER
' add cells into pdf table
PdfTable.AddCell(pdfcell)
ElseIf i = 1 Then
pdfcell = New PdfPCell(New Phrase(New Chunk("Batch Number", pTable)))
' alignment header table
pdfcell.BackgroundColor = BaseColor.GRAY
pdfcell.HorizontalAlignment = PdfPCell.ALIGN_CENTER
' add cells into pdf table
PdfTable.AddCell(pdfcell)
ElseIf i = 2 Then
pdfcell = New PdfPCell(New Phrase(New Chunk("Lot Number", pTable)))
pdfcell.BackgroundColor = BaseColor.GRAY
' alignment header table
pdfcell.HorizontalAlignment = PdfPCell.ALIGN_CENTER
' add cells into pdf table
PdfTable.AddCell(pdfcell)
ElseIf i = 3 Then
pdfcell = New PdfPCell(New Phrase(New Chunk("Done By", pTable)))
' alignment header table
pdfcell.BackgroundColor = BaseColor.GRAY
pdfcell.HorizontalAlignment = PdfPCell.ALIGN_CENTER
' add cells into pdf table
PdfTable.AddCell(pdfcell)
ElseIf i = 4 Then
pdfcell = New PdfPCell(New Phrase(New Chunk("Role", pTable)))
pdfcell.BackgroundColor = BaseColor.GRAY
' alignment header table
pdfcell.HorizontalAlignment = PdfPCell.ALIGN_CENTER
' add cells into pdf table
PdfTable.AddCell(pdfcell)
ElseIf i = 5 Then
pdfcell = New PdfPCell(New Phrase(New Chunk("Done On", pTable)))
pdfcell.BackgroundColor = BaseColor.GRAY
' alignment header table
pdfcell.HorizontalAlignment = PdfPCell.ALIGN_CENTER
' add cells into pdf table
PdfTable.AddCell(pdfcell)
Else
pdfcell = New PdfPCell(New Phrase(New Chunk(DataGridView1.Columns(i).HeaderText, pTable)))
pdfcell.BackgroundColor = BaseColor.GRAY
' alignment header table
pdfcell.HorizontalAlignment = PdfPCell.ALIGN_CENTER
' add cells into pdf table
PdfTable.AddCell(pdfcell)
End If
Next
' add data into pdf table
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
' add pdf table into pdf document
PdfFile.Add(PdfTable)
PdfFile.Close() ' close all sessions
' show message if hasben exported
MessageBox.Show("PDF format success exported !", "Informations", MessageBoxButtons.OK, MessageBoxIcon.Information)
End Sub
End Class
Module Module1
Sub Main()
Dim pdfDoc As New Document()
Dim pdfWrite As PdfWriter = PdfWriter.GetInstance(pdfDoc, New FileStream("tryme2.pdf", FileMode.Create))
Dim ev As New itsEvents
pdfWrite.PageEvent = ev
pdfDoc.Open()
pdfDoc.Add(New Paragraph("Hello World"))
pdfDoc.NewPage()
pdfDoc.Add(New Paragraph("Hello World Again"))
pdfDoc.Close()
End Sub
End Module
Public Class itsEvents
Inherits PdfPageEventHelper
Public Overrides Sub OnStartPage(ByVal writer As iTextSharp.text.pdf.PdfWriter, ByVal document As iTextSharp.text.Document)
Dim Paragraph As New Paragraph
Dim image = iTextSharp.text.Image.GetInstance("C:\Users\hp\source\repos\WindowsApp2\WindowsApp2\Images\Aurobindo-Logo.png")
image.ScalePercent(24.0F)
image.SetAbsolutePosition(90, 770)
image.Alignment = Image.TEXTWRAP Or Element.ALIGN_LEFT
document.Add(image)
Dim bfntHead = BaseFont.CreateFont(BaseFont.TIMES_ROMAN, BaseFont.CP1252, BaseFont.NOT_EMBEDDED)
Dim pTitle As New Font(iTextSharp.text.Font.FontFamily.TIMES_ROMAN, 14, iTextSharp.text.Font.BOLD, BaseColor.BLACK)
Dim pTable As New Font(iTextSharp.text.Font.FontFamily.TIMES_ROMAN, 12, iTextSharp.text.Font.NORMAL, BaseColor.BLACK)
Dim prgHeading = New Paragraph
prgHeading.Alignment = Element.ALIGN_CENTER
Dim prgHeading1 = New Paragraph
prgHeading1.Alignment = Element.ALIGN_CENTER
prgHeading.Add(New Chunk("AUROBINDO PHARMA LIMITED", pTitle))
prgHeading1.Add(New Chunk("Unit III Batch Reports", pTitle))
document.Add(prgHeading)
document.Add(prgHeading1)
Paragraph.SpacingAfter = 10.0F
Paragraph = New Paragraph(New Chunk("Batch Audit Reports", pTitle))
Paragraph.Alignment = Element.ALIGN_CENTER
Paragraph.SpacingAfter = 10.0F
document.Add(Paragraph)
Dim ch As New Chunk("This is my Stack Overflow Header on page " & writer.PageNumber)
End Sub
End Class
Public Class itsEvents5
Inherits PdfPageEventHelper
Public Overrides Sub OnEndPage(ByVal writer As iTextSharp.text.pdf.PdfWriter, ByVal document As iTextSharp.text.Document)
End Sub
End Class
вот файл PDF, который я получаю. Вывод PDF
Проблема заключается в том, что верхний и нижний колонтитулы получают только сверху. Если я сохранил верхний и нижний колонтитулы на событии startofthepage, он отображает всю информацию только сверху. Если я сохранил информацию о событии endofpage, это не работает.