PDF formatting

4

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

protectedvoidExportPDF(){intcolCount=_gvConsultaRelatorio.Columns.Count-1;PdfPTabletable=newPdfPTable(colCount);table.HorizontalAlignment=0;int[]colWidths=newint[_gvConsultaRelatorio.Columns.Count];PdfPCellcell;stringcellText;//CriandoHeaderfor(intcolIndex=0;colIndex<colCount;colIndex++){colWidths[colIndex]=(int)_gvConsultaRelatorio.Columns[colIndex].ItemStyle.Width.Value;cellText=Server.HtmlDecode(_gvConsultaRelatorio.HeaderRow.Cells[colIndex].Text);cell=newPdfPCell(newPhrase(cellText));cell.BackgroundColor=newBaseColor(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;
}
    
asked by anonymous 02.06.2015 / 20:03

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;

Numbers that are between quotes indicate the width of the column.

    
12.09.2016 / 19:35