Text overwriting the header

3

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.

    
asked by anonymous 13.07.2015 / 21:29

1 answer

1

Inspiring with multiple answers to this en.SE , and even though I've never used the , I think could do so:

  • Insert content not in OnStartPage() , but in OnEndPage() .
  • When you make a new page, you will have to make sure that you have margins large enough for the header

Specifically, this answer from Bruno in English summarizes the problem, and gives suggestions on how to do so based on some examples. Here's a bad translation:

Original

  

Adding headers and footers is now done using page events . The examples are in Java, but you can find the C # port of the examples here and here .

     

Make sure you read the documentation. A common mistake many developers have made before you, adding content in the OnStartPage . You should only add content in the OnEndPage . It's also obvious that you need to add the content at absolute coordinates (for instance using ColumnText ) and that you need to reserve sufficient space for the header and footer by defining the margins of your document correctly.

Translation

  

Adding headers and footers is done using events on pages . The examples are in Java, but you can find the toll to the examples C # here and here .

     

Read the documentation. A common flaw that many developers have done before you, is to add content to the OnStartPage function. You should add content only in the OnEndPage function. It is also obvious that you need to add content with absolute coordinates (for example, using ColumnText ) and you need to reserve enough space for the header and footer by setting the margins of your document correctly.

    
30.07.2015 / 06:54