Text overwriting the header

Asked

Viewed 504 times

3

I tagged C# because virtually all the examples found on the internet are in this language and "translate" to vb.net is quite simple.

Man report based on the iTextSharp is dynamic. It has a header with the basic information and two dynamically generated tables.

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 table if it goes to a new page, both for the first and the second table.

Although several items are unnecessary for the question, follows 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

Upshot

Page 1: inserir a descrição da imagem aqui

Page 2: inserir a descrição da imagem aqui

Forgive the pollution in the images but I’m obliged to do so.

  • I see where you put the word Produtos:, but where the other information of the header - Número do Compromisso, Agente, Moeda, etc..

  • It was missing. I just added

  • 1

    Pollute the image more! If some malicious want to read the data, I think he can achieve in some places.

1 answer

1

Inspiring me with various responses to this question of en.SE, and even having never used the , I believe I could do so:

  • Inserts the content not in the OnStartPage(), but yes, in the OnEndPage().
  • When you make a new page, you have to make sure you have margins big enough for the header

Specifically, that answer of Bruno in English summarizes the problem, and gives suggestions on how to do it based on a few examples. The following is a maladequad 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 by Many Developers have made before you, is 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

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

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.