1

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?

Rickypauel
  • 11
  • 1
  • 3
  • Possible duplicate of [iText : Unable to print mathematical characters like ∈, ∩, ∑, ∫, ∆ √, ∠](http://stackoverflow.com/questions/31268867/itext-unable-to-print-mathematical-characters-like-%e2%88%88-%e2%88%a9-%e2%88%91-%e2%88%ab-%e2%88%86-%e2%88%9a-%e2%88%a0) – Amedee Van Gasse Jul 15 '16 at 05:41
  • And yes I know that other question is in Java. It doesn't matter. The answer boils down to "if you want Unicode characters, you need to use a font that contains Unicode characters". – Amedee Van Gasse Jul 15 '16 at 06:28

0 Answers0