How not to break the page on iTextSharp split my table into two pages

Asked

Viewed 187 times

-1

I would like to have my pdf put on the next sheet the table that did not have enough space in the last and did not divide this table in the two sheets.

  if (reader.HasRows)
        {
            while (reader.Read())
            {
                servico.idVenda = reader.GetInt32(0);

                if (servico.idVenda != idVendaOld)
                {
                    PdfPTable tableTotal = new PdfPTable(3);
                    float[] colsTableTotal = { 14, 3, 20 };
                    tableTotal.SetWidths(colsTableTotal);
                    tableTotal.WidthPercentage = 100f;
                    tableTotal.PaddingTop = 5;

                    tableTotal.DefaultCell.Border = PdfPCell.BOTTOM_BORDER;


                    tableTotal.AddCell(getNewCell("Total:", titulo, Element.ALIGN_LEFT, 2, PdfPCell.BOTTOM_BORDER, preto, branco));
                    tableTotal.AddCell(getNewCell(somaSubTotal.ToString("c"), font, Element.ALIGN_LEFT, 2, PdfPCell.BOTTOM_BORDER, preto, branco));
                    tableTotal.AddCell(getNewCell(somaTotal.ToString("C"), font, Element.ALIGN_LEFT, 2, PdfPCell.BOTTOM_BORDER, preto, branco));

                    somaTotal = 0;
                    somaSubTotal = 0;

                    if (contador != 0)
                    {
                        doc.Add(tableTotal);
                    }

                    PdfPTable tableTitulos = new PdfPTable(5);
                    BaseColor fundo = new BaseColor(200, 200, 200);


                    float[] colsW = { 5, 15, 10, 10, 10 };
                    tableTitulos.SetWidths(colsW);
                    tableTitulos.WidthPercentage = 100f;

                    tableTitulos.DefaultCell.Border = PdfPCell.BOTTOM_BORDER;
                    tableTitulos.DefaultCell.BorderColor = preto;
                    tableTitulos.DefaultCell.BorderColorBottom = new BaseColor(255, 255, 255);

                    servico.cliente = reader.GetString(1);
                    servico.carro = reader.GetString(2);
                    servico.placa = reader.GetString(3);
                    servico.data = reader.GetDateTime(7);


                    tableTitulos.AddCell(getNewCell("OS:", fontPeriodo, Element.ALIGN_LEFT, 2, PdfPCell.NO_BORDER, preto, fundo));
                    tableTitulos.AddCell(getNewCell("Cliente:", fontPeriodo, Element.ALIGN_LEFT, 2, PdfPCell.NO_BORDER, preto, fundo));
                    tableTitulos.AddCell(getNewCell("Carro:", fontPeriodo, Element.ALIGN_LEFT, 2, PdfPCell.NO_BORDER, preto, fundo));
                    tableTitulos.AddCell(getNewCell("Placa:", fontPeriodo, Element.ALIGN_LEFT, 2, PdfPCell.NO_BORDER, preto, fundo));
                    tableTitulos.AddCell(getNewCell("Data:", fontPeriodo, Element.ALIGN_LEFT, 2, PdfPCell.NO_BORDER, preto, fundo));


                    tableTitulos.AddCell(getNewCell(servico.idVenda.ToString(), font, Element.ALIGN_LEFT, 2, PdfPCell.BOTTOM_BORDER, preto, fundo));
                    tableTitulos.AddCell(getNewCell(servico.cliente, font, Element.ALIGN_LEFT, 2, PdfPCell.BOTTOM_BORDER, preto, fundo));
                    tableTitulos.AddCell(getNewCell(servico.carro, font, Element.ALIGN_LEFT, 2, PdfPCell.BOTTOM_BORDER, preto, fundo));
                    tableTitulos.AddCell(getNewCell(servico.placa, font, Element.ALIGN_LEFT, 2, PdfPCell.BOTTOM_BORDER, preto, fundo));
                    tableTitulos.AddCell(getNewCell(servico.data.ToShortDateString(), font, Element.ALIGN_LEFT, 2, PdfPCell.BOTTOM_BORDER, preto, fundo));

                    PdfPTable tableTitulosDados = new PdfPTable(5);
                    float[] colstableTitulosDados = { 25, 5, 10, 10, 15 };
                    tableTitulosDados.SetWidths(colstableTitulosDados);
                    tableTitulosDados.WidthPercentage = 100f;

                    tableTitulosDados.DefaultCell.Border = PdfPCell.NO_BORDER;

                    Paragraph espaco = new Paragraph((new Phrase("\n")));
                    doc.Add(espaco);

                    tableTitulosDados.AddCell(getNewCell("Serviços:", titulo, Element.ALIGN_LEFT, 2, PdfPCell.NO_BORDER, preto, branco));
                    tableTitulosDados.AddCell(getNewCell("Preço:", titulo, Element.ALIGN_LEFT, 2, PdfPCell.NO_BORDER, preto, branco));
                    tableTitulosDados.AddCell(getNewCell("Valor Cobrado:", titulo, Element.ALIGN_LEFT, 2, PdfPCell.NO_BORDER, preto, branco));
                    tableTitulosDados.AddCell(getNewCell("Situação:", titulo, Element.ALIGN_LEFT, 2, PdfPCell.NO_BORDER, preto, branco));
                    tableTitulosDados.AddCell(getNewCell("Forma de Pagamento:", titulo, Element.ALIGN_LEFT, 2, PdfPCell.NO_BORDER, preto, branco));

                    doc.Add(tableTitulos);
                    doc.Add(tableTitulosDados);

                    idVendaOld = servico.idVenda;
                    contador += 1;
                }


                servico.servico = reader.GetString(4);
                servico.preco = reader.GetDouble(5);
                servico.valorCobrado = reader.GetDouble(6);
                servico.formaPagamento = reader.GetString(9);

                PdfPTable tableDados = new PdfPTable(5);
                float[] colsTableDados = { 25, 5, 10, 10, 15 };
                tableDados.SetWidths(colsTableDados);
                tableDados.WidthPercentage = 100f;

                tableDados.DefaultCell.Border = PdfPCell.TOP_BORDER;
                tableDados.DefaultCell.Border = PdfPCell.BOTTOM_BORDER;

                tableDados.AddCell(getNewCell(servico.servico, font, Element.ALIGN_LEFT, 2, PdfPCell.BOTTOM_BORDER, preto, branco));
                tableDados.AddCell(getNewCell(servico.preco.ToString("C"), font, Element.ALIGN_LEFT, 2, PdfPCell.BOTTOM_BORDER, preto, branco));
                tableDados.AddCell(getNewCell(servico.valorCobrado.ToString("C"), font, Element.ALIGN_LEFT, 2, PdfPCell.BOTTOM_BORDER, preto, branco));

                if (servico.pago == true)
                {
                    tableDados.AddCell(getNewCell("PAGO", font, Element.ALIGN_LEFT, 2, PdfPCell.BOTTOM_BORDER, preto, branco));
                }
                else
                {
                    tableDados.AddCell(getNewCell("EM ABERTO", font, Element.ALIGN_LEFT, 2, PdfPCell.BOTTOM_BORDER, preto, branco));
                }


                tableDados.AddCell(getNewCell(servico.formaPagamento.ToString(), font, Element.ALIGN_LEFT, 2, PdfPCell.BOTTOM_BORDER, preto, branco));

                somaSubTotal += servico.preco;
                somaTotal += servico.valorCobrado;

                totalSomaTotal += servico.valorCobrado;
                totalSomaSubTotal += servico.preco;

                doc.Add(tableDados);


            }

            PdfPTable tableUltimoTotal = new PdfPTable(3);
            float[] colstableUltimoTotal = { 14, 3, 20 };
            tableUltimoTotal.SetWidths(colstableUltimoTotal);
            tableUltimoTotal.WidthPercentage = 100f;
            tableUltimoTotal.PaddingTop = 5;

            tableUltimoTotal.DefaultCell.Border = PdfPCell.BOTTOM_BORDER;


            tableUltimoTotal.AddCell(getNewCell("Total:", titulo, Element.ALIGN_LEFT, 2, PdfPCell.BOTTOM_BORDER, preto, branco));
            tableUltimoTotal.AddCell(getNewCell(somaSubTotal.ToString("c"), font, Element.ALIGN_LEFT, 2, PdfPCell.BOTTOM_BORDER, preto, branco));
            tableUltimoTotal.AddCell(getNewCell(somaTotal.ToString("C"), font, Element.ALIGN_LEFT, 2, PdfPCell.BOTTOM_BORDER, preto, branco));

            doc.Add(tableUltimoTotal);

1 answer

1


You can indicate that the object or certain lines must remain together

PdfPTable tableUltimoTotal = new PdfPTable(3);
tableUltimoTotal.KeepTogether = true;
//E ou...
tableUltimoTotal.KeepRowsTogether(0);

Browser other questions tagged

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