PDF formatting

Asked

Viewed 606 times

4

I would like to format the sizes of your cells and your text for PDF export.

inserir a descrição da imagem aqui

protected void ExportPDF()
{

    int colCount = _gvConsultaRelatorio.Columns.Count - 1;
    PdfPTable table = new PdfPTable(colCount);
    table.HorizontalAlignment = 0;

    int[] colWidths = new int[_gvConsultaRelatorio.Columns.Count];

    PdfPCell cell;
    string cellText;

    //Criando Header
    for (int colIndex = 0; colIndex < colCount; colIndex++)
    {

        colWidths[colIndex] = (int)_gvConsultaRelatorio.Columns[colIndex].ItemStyle.Width.Value;

        cellText = Server.HtmlDecode(_gvConsultaRelatorio.HeaderRow.Cells[colIndex].Text);

        cell = new PdfPCell(new Phrase(cellText));

        cell.BackgroundColor = new BaseColor(System.Drawing.ColorTranslator.FromHtml("#d1dbe0"));

        table.AddCell(cell);
    }

    //Exportar dados da Grid para Tabela
    for (int rowIndex = 0; rowIndex < _gvConsultaRelatorio.Rows.Count; rowIndex++)
    {
        if (_gvConsultaRelatorio.Rows[rowIndex].RowType == DataControlRowType.DataRow)
        {
            for (int j = 0; j < _gvConsultaRelatorio.Columns.Count - 1; j++)
            {
                cellText = Server.HtmlDecode(_gvConsultaRelatorio.Rows[rowIndex].Cells[j].Text);

                cell = new PdfPCell(new Phrase(cellText));

                if (rowIndex % 2 != 0)
                {
                    cell.BackgroundColor = new BaseColor(System.Drawing.ColorTranslator.FromHtml("#9ab2ca"));
                }
                else
                {
                    cell.BackgroundColor = new BaseColor(System.Drawing.ColorTranslator.FromHtml("#f1f5f6"));
                }

                table.AddCell(cell);
            }
        }
    }

    Document pdfDoc = new Document(PageSize.A4, 10f, 0f, 10f, 0f);

    PdfWriter.GetInstance(pdfDoc, Response.OutputStream);

    pdfDoc.Open();
    pdfDoc.Add(table);
    pdfDoc.Close();

    Response.ContentType = "application/pdf";
    Response.AddHeader("content-disposition", "attachment;" + "filename=RelatorioPDF.pdf");
    Response.Cache.SetCacheability(HttpCacheability.NoCache);
    Response.Write(pdfDoc);
    Response.End();
    //this._gvConsultaRelatorio.AllowPaging = true;
}
  • What library is being used for PDF generation?

  • I am using itextsharp

  • 1

    It would also be interesting to correct the word Ignition Setting.

  • What formatting you want, plus changing the size of cells?

  • Highlighted header and remove rows from the table!

  • could adjust only the sizes of cells for the text not to break?

Show 1 more comment

1 answer

1

You can do it this way:

String[,] colunaLargura = { { "Equipamento", "2" }, { "Modelo", "3" }, { "Placa", "3" }, 
         { "Cor", "2" }, { "Ano Fabricação", "2" }, { "Data/Hora", "3" }, 
         { "Velocidade(Km/h)", "3"}, { "Iguinição", "2" }};


float[] headerwidths = new float[colunaLargura.GetLength(0)];

for (int i = 0; i < colunaLargura.GetLength(0); i++)
{
    String coluna = (String)colunaLargura[i, 0];
    float largura = float.Parse((String)colunaLargura[i, 1]);
    headerwidths[i] = largura;

    PdfPCell cell = CelulaCabecalho(coluna);

    tabela.AddCell(cell);
}

tabela.SetWidths(headerwidths);
tabela.WidthPercentage = 100;
tabela.HeaderRows = 1;

The numbers that are between quotation marks indicate the width of the column.

Browser other questions tagged

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