I have tagged the C#
tag since almost all the examples found on the internet are in this language and "translate" to vb.net
is very simple.
My report based on iTextSharp is dynamic. It has a header with the base information and two tables dynamically generated.
One of my problems is that when the table goes to a new page it simply overwrites the header .
The other problem is that I need to repeat the header of the table if it goes to a new page, for both the first and second tables.
Although several items are unnecessary for the question, follow below code in full.
Dim IDCompromisso = CType(HttpContext.Current.Session("IDCompromisso"), Integer)
Dim CompromissoAtual = GetCompromisso().Where(Function(y) y.ID = IDCompromisso).FirstOrDefault()
Dim nome As String = "report" + Date.Now.ToString().Replace(".", "").Replace("/", "").Replace(" ", "").Replace(":", "") + ".pdf"
Dim pdf As New Document(PageSize.A4.Rotate())
Dim path = Request.PhysicalApplicationPath + "\reports\" + nome
Dim writer As PdfWriter = PdfWriter.GetInstance(pdf, New FileStream(path, FileMode.Create))
pdf.Open()
CreateHeader(pdf)
Dim evH As New HeaderF
writer.PageEvent = evH
pdf.Add(New Phrase("Produtos: "))
Dim CountLoop = 0
Dim ValidarItens As New List(Of CompromissoInfo)
ValidarItens.Clear()
For Each A In GetCompromissoInfo()
If (GetValidarDataCompromisso().Where(Function(b) b.Vencimento = A.IDVencimento And b.Compromisso = A.CompromissoID And b.Item = A.ItemID).Count() > 0) Then
ValidarItens.Add(A)
End If
Next
For Each A In ValidarItens.OrderByDescending(Function(b) b.DT_VENCIMENTO).GroupBy(Function(B) B.IDVencimento)
CountLoop = CountLoop + 1
Next
Dim pdfTable As New PdfPTable(CountLoop + 4)
pdfTable.TotalWidth = 100
CountLoop = 0
pdfTable.DefaultCell.Padding = 3
pdfTable.HorizontalAlignment = Element.ALIGN_LEFT
pdfTable.DefaultCell.BorderWidth = 1
pdfTable.AddCell("Produto")
pdfTable.AddCell("Código")
pdfTable.AddCell("Qtd. Embalagem")
For Each A In ValidarItens.OrderByDescending(Function(b) b.DT_VENCIMENTO).GroupBy(Function(B) B.IDVencimento)
CountLoop = CountLoop + 1
Dim cell As New PdfPCell(New Phrase(A.FirstOrDefault().DT_VENCIMENTO.ToString("dd/MM/yyyy")))
pdfTable.AddCell(cell)
Next
pdfTable.AddCell("Quantidade Total")
Dim ValidarItensLista As New List(Of CompromissoInfo)
ValidarItensLista.Clear()
For Each A In GetCompromissoInfo()
If (GetValidarDataCompromisso().Where(Function(b) b.Vencimento = A.IDVencimento And b.Compromisso = A.CompromissoID And b.Item = A.ItemID).Count() > 0) Then
ValidarItensLista.Add(A)
End If
Next
Dim QuantidadeGeral = 0
For Each C In ValidarItensLista.GroupBy(Function(EX) EX.ItemID)
pdfTable.AddCell(C.Select(Function(a) a.DESCRICAO).FirstOrDefault())
pdfTable.AddCell(C.Select(Function(a) a.Codigo).FirstOrDefault())
pdfTable.AddCell(C.Select(Function(a) a.EMBALAGEM).FirstOrDefault())
Dim QTDTotal = 0
For Each A In ValidarItensLista.OrderByDescending(Function(b) b.IDVencimento).GroupBy(Function(B) B.IDVencimento)
Dim Validar = 0
For Each X In A
If X.ItemID = C.FirstOrDefault().ItemID And C.Where(Function(h) h.ItemID = X.ItemID).Select(Function(t) t.IDVencimento).Contains(X.IDVencimento) Then
pdfTable.AddCell(ConverterQuantidade(X.QTD_ITEM))
QTDTotal = QTDTotal + X.QTD_ITEM
Validar = 1
End If
Next
If Validar = 0 Then
pdfTable.AddCell("0")
End If
Next
pdfTable.AddCell(ConverterQuantidade(QTDTotal))
QuantidadeGeral = QuantidadeGeral + QTDTotal
Next
pdf.Add(pdfTable)
pdf.Add(New Phrase("Resumo: "))
pdfTable = Nothing
pdfTable = New PdfPTable(4)
pdfTable.TotalWidth = 100
pdfTable.DefaultCell.Padding = 3
pdfTable.HorizontalAlignment = Element.ALIGN_LEFT
pdfTable.DefaultCell.BorderWidth = 1
pdfTable.AddCell("Nº Parcela")
pdfTable.AddCell("Data de Vencimento")
pdfTable.AddCell("Valor em " + CompromissoAtual.Moeda)
pdfTable.AddCell("% Desconto")
Dim ValorTotal = 0
Dim Parcela = 0
Dim CountFor = 0
ValidarItens.Clear()
For Each A In GetCompromissoInfo()
If (GetValidarDataCompromisso().Where(Function(b) b.Vencimento = A.IDVencimento And b.Compromisso = A.CompromissoID And b.Item = A.ItemID And A.DT_VENCIMENTO = b.Data).Count() > 0) Then
ValidarItens.Add(A)
End If
Next
Dim Valores As New List(Of Single)
Dim ValoresData As New List(Of Single)
Dim Quantidade As New List(Of Integer)
Dim Depurar As New List(Of String)
Dim ValoresArray As New List(Of Single)
Dim QuantidadeArray As New List(Of Integer)
For Each A In ValidarItens.OrderByDescending(Function(c) c.DT_VENCIMENTO).GroupBy(Function(B) B.IDVencimento)
For C As Integer = 0 To A.Count() - 1
Dim Valor = (A(C).VLR_ITEM.ToString())
Dim Desconto = ((A(C).VLR_ITEM.ToString() / 100) * A(C).DESCONTO)
Dim QuantidadeItem = (A(C).QTD_ITEM)
Dim Total = (Valor - Desconto) * QuantidadeItem
QuantidadeArray.Add((A(C).QTD_ITEM))
ValoresData.Add(Total)
Next
Depurar.Add("Quantidde = " + ValoresData.Count().ToString() + " Valores = " + ValoresData.Sum().ToString())
Valores.Add(ValoresData.Sum())
ValoresData.Clear()
CountFor = CountFor + 1
Quantidade.Add(QuantidadeArray.Sum())
Next
For Each Y In ValidarItens.OrderByDescending(Function(a) a.DT_VENCIMENTO).GroupBy(Function(Z) Z.IDVencimento)
Parcela = Parcela + 1
pdfTable.AddCell(Parcela)
pdfTable.AddCell(Y.FirstOrDefault().DT_VENCIMENTO.ToString("dd/MM/yyyy"))
Dim ValorAtual = Valores((Parcela - 1))
If Not ValorAtual.ToString().Contains(",") Then
ValorAtual = ValorAtual + ",00"
End If
pdfTable.AddCell(ConverterMoeda(ValorAtual))
pdfTable.AddCell(ConverterMoeda(Y.FirstOrDefault().DESCONTO))
Next
pdf.Add(pdfTable)
pdf.Close()
System.Diagnostics.Process.Start(path)
Classes responsible for header
Public Class HeaderF
Inherits PdfPageEventHelper
Public Overrides Sub OnStartPage(ByVal writer As iTextSharp.text.pdf.PdfWriter, ByVal document As iTextSharp.text.Document)
CreateHeader(document)
End Sub
End Class
Public Shared Function CreateHeader(ByVal document As iTextSharp.text.Document)
Dim CompromissoAtual = GetCompromisso().Where(Function(y) y.ID = CType(HttpContext.Current.Session("IDCompromisso"), Integer)).FirstOrDefault()
document.Add(New Paragraph("Número do Compromisso: " + CompromissoAtual.ID.ToString))
document.Add(New Paragraph("Agente: " + CompromissoAtual.Agente.ToString))
document.Add(New Paragraph("Safra: " + CompromissoAtual.Periodo.ToString))
document.Add(New Paragraph("Origem de Faturamento: " + CompromissoAtual.OrigemFaturamento.ToString))
document.Add(New Paragraph("Moeda: " + CompromissoAtual.Moeda.ToString))
document.Add(New Paragraph("Data de Emissão: " + CompromissoAtual.DataEmissao.ToString("dd/MM/yyyy")))
End Function
Result
Page 1:
Page2:
Forgive the pollution in the images but I am forced to do so.