4
I would like to format the sizes of your cells and your text for PDF export.
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?
– Leonel Sanches da Silva
I am using itextsharp
– Hans Miller
It would also be interesting to correct the word Ignition Setting.
– Butzke
What formatting you want, plus changing the size of cells?
– Felipe Avelar
Highlighted header and remove rows from the table!
– Hans Miller
could adjust only the sizes of cells for the text not to break?
– Hans Miller