Im new in C#. Using iTextSharp i can export data from dataGridView1 to PDF file as a table but, when i do it, the PDF dont show characters like ♫, → or ► (Unicode). So i was reading about this but i didnt understand, and basically all solutions are in ASP.NET or VB.NET. Im using Visual C# Express 2010. This the code that im using to export PDF:
public void GenerarDocumento(Document document)
{
//se crea un objeto PdfTable con el # de columnas del dataGridView
PdfPTable datatable = new PdfPTable(dataGridView1.ColumnCount);
//asignamos algunas propiedades para el diseño del pdf
datatable.DefaultCell.Padding = 3;
float[] headerwidths = GetTamañoColumnas(dataGridView1);
datatable.SetWidths(headerwidths);
datatable.WidthPercentage = 100;
datatable.DefaultCell.BorderWidth = 2;
datatable.DefaultCell.HorizontalAlignment = Element.ALIGN_CENTER;
//SE GENERA EL ENCABEZADO DE LA TABLA EN EL PDF
for (int i = 0; i < dataGridView1.ColumnCount; i++)
{
datatable.AddCell(dataGridView1.Columns[i].HeaderText);
}
datatable.HeaderRows = 1;
datatable.DefaultCell.BorderWidth = 1;
//SE GENERA EL CUERPO DEL PDF
for (int i = 0; i < dataGridView1.RowCount; i++)
{
for (int j = 0; j < dataGridView1.ColumnCount; j++)
{
datatable.AddCell(dataGridView1[j, i].Value.ToString());
}
datatable.CompleteRow();
}
//Add PdfTable to the doc
document.Add(datatable);
}
This method get the size of columns datagridview
public float[] GetTamañoColumnas(DataGridView dg)
{
float[] values = new float[dg.ColumnCount];
for (int i = 0; i < dg.ColumnCount; i++)
{
values[i] = (float)dg.Columns[i].Width;
}
return values;
}
Well this is the button export to PDF:
private void btnExportarPDF_Click(object sender, EventArgs e)
{
try
{
SaveFileDialog sfd = new SaveFileDialog();
sfd.Filter = "Documento PDF (*.pdf)|*.pdf";
sfd.FileName = DateTime.Now.ToString("dd-MM-yyyy", CultureInfo.InvariantCulture);
if (sfd.ShowDialog() == DialogResult.OK)
{
iTextSharp.text.Document doc = new iTextSharp.text.Document(PageSize.A4.Rotate(), 10, 10, 10, 10);
string filename = sfd.FileName;
FileStream file = new FileStream(sfd.FileName, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite);
PdfWriter.GetInstance(doc, file);
doc.Open();
GenerarDocumento(doc);
doc.Close();
System.Diagnostics.Process.Start(sfd.FileName);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
What i need to change in the code? What i have to do?