Hello. I want to export datagriedview to PDF with UTF8. When I export to PDF, header texts is shown with lack of letters. I am using Azerbaijani alphabet. We have letters like "ə, ı, ü, ö, ğ, ç, ş". Since I am beginner in C#, i don't know what to do. I please you to help me.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Reflection;
using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;
namespace Loan_Calculator
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
double kreditmeblegi;
double illikfaiz;
double fm;
double value1;
double ay;
double il;
double ayliqodenis;
double umumiodenis;
double esasborc;
double faiz;
double qaliqmebleg;
double odenilenfaiz;
int nomre;
DataTable dt = new DataTable();
private void button1_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(textBox1.Text) || string.IsNullOrEmpty(textBox2.Text) || string.IsNullOrEmpty(textBox3.Text))
{
MessageBox.Show("Rəqəmləri daxil edin.");
}
else
{
if (comboBox2.Text == "ay")
{
kreditmeblegi = Convert.ToDouble(textBox1.Text);
illikfaiz = Convert.ToDouble(textBox2.Text);
ay = Convert.ToDouble(textBox3.Text);
fm = illikfaiz / (12 * 100);
value1 = 1 - (1 / Math.Pow((1 + fm), ay));
ayliqodenis = (kreditmeblegi * fm) / value1;
umumiodenis = ayliqodenis * ay;
odenilenfaiz = umumiodenis - kreditmeblegi;
DateTime date = this.dateTimePicker1.Value.Date;
DateTime nyear = new DateTime();
string format = "dd / MM / yyyy";
qaliqmebleg = kreditmeblegi;
for (int i = 1; i <= ay; i++)
{
nomre = nomre + 1;
nyear = date.AddMonths(i);
faiz = qaliqmebleg * fm;
esasborc = ayliqodenis - faiz;
qaliqmebleg = qaliqmebleg - esasborc;
int n = dataGridView1.Rows.Add();
dataGridView1.Rows[n].Cells[0].Value = nomre.ToString();
dataGridView1.Rows[n].Cells[1].Value = nyear.ToString(format);
dataGridView1.Rows[n].Cells[2].Value = ayliqodenis.ToString("N2");
dataGridView1.Rows[n].Cells[3].Value = esasborc.ToString("N2");
dataGridView1.Rows[n].Cells[4].Value = faiz.ToString("N2");
dataGridView1.Rows[n].Cells[5].Value = qaliqmebleg.ToString("N2");
}
button1.Enabled = false;
}
else
{
kreditmeblegi = Convert.ToDouble(textBox1.Text);
illikfaiz = Convert.ToDouble(textBox2.Text);
il = Convert.ToDouble(textBox3.Text) * 12;
fm = illikfaiz / (12 * 100);
value1 = 1 - (1 / Math.Pow((1 + fm), il));
ayliqodenis = (kreditmeblegi * fm) / value1;
umumiodenis = ayliqodenis * il;
odenilenfaiz = umumiodenis - kreditmeblegi;
DateTime date = this.dateTimePicker1.Value.Date;
DateTime nyear = new DateTime();
string format = "dd / MM / yyyy";
qaliqmebleg = kreditmeblegi;
for (int j = 1; j <= il; j++)
{
nomre = nomre + 1;
nyear = date.AddMonths(j);
faiz = qaliqmebleg * fm;
esasborc = ayliqodenis - faiz;
qaliqmebleg = qaliqmebleg - esasborc;
int n = dataGridView1.Rows.Add();
dataGridView1.Rows[n].Cells[0].Value = nomre.ToString();
dataGridView1.Rows[n].Cells[1].Value = nyear.ToString(format);
dataGridView1.Rows[n].Cells[2].Value = ayliqodenis.ToString("N2");
dataGridView1.Rows[n].Cells[3].Value = esasborc.ToString("N2");
dataGridView1.Rows[n].Cells[4].Value = faiz.ToString("N2");
dataGridView1.Rows[n].Cells[5].Value = qaliqmebleg.ToString("N2");
}
button1.Enabled = false;
}
if (Convert.ToDouble(textBox1.Text) == 0 || Convert.ToDouble(textBox2.Text) == 0 || Convert.ToDouble(textBox3.Text) == 0)
{
MessageBox.Show("Sıfır daxil etmək olmaz!");
button1.Enabled = true;
}
label9.Text = ayliqodenis.ToString("N2") + " " + comboBox1.Text;
label10.Text = umumiodenis.ToString("N2") + " " + comboBox1.Text;
label12.Text = odenilenfaiz.ToString("N2") + " " + comboBox1.Text;
}
}
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar) &&
(e.KeyChar != '.'))
{
e.Handled = true;
}
// only allow one decimal point
if ((e.KeyChar == '.') && ((sender as TextBox).Text.IndexOf('.') > -1))
{
e.Handled = true;
}
}
private void textBox2_KeyPress(object sender, KeyPressEventArgs e)
{
if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar) &&
(e.KeyChar != '.'))
{
e.Handled = true;
}
// only allow one decimal point
if ((e.KeyChar == '.') && ((sender as TextBox).Text.IndexOf('.') > -1))
{
e.Handled = true;
}
}
private void textBox3_KeyPress(object sender, KeyPressEventArgs e)
{
if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar) &&
(e.KeyChar != '.'))
{
e.Handled = true;
}
// only allow one decimal point
if ((e.KeyChar == '.') && ((sender as TextBox).Text.IndexOf('.') > -1))
{
e.Handled = true;
}
}
private void haqqındaToolStripMenuItem_Click(object sender, EventArgs e)
{
About about = new About();
about.Show();
}
private void wiewHelpToolStripMenuItem_Click(object sender, EventArgs e)
{
Help melumat = new Help();
melumat.Show();
}
private void təmizləToolStripMenuItem_Click(object sender, EventArgs e)
{
textBox1.Clear();
textBox2.Clear();
textBox3.Clear();
comboBox1.Text = "AZN";
comboBox2.Text = "ay";
dateTimePicker1.Value = DateTime.Now;
label9.Text = "0";
label10.Text = "0";
label12.Text = "0";
dataGridView1.Rows.Clear();
nomre = 0;
button1.Enabled = true;
}
private void çıxToolStripMenuItem_Click(object sender, EventArgs e)
{
this.Close();
}
private void comboBox1_KeyPress(object sender, KeyPressEventArgs e)
{
e.Handled = true;
}
private void comboBox2_KeyPress(object sender, KeyPressEventArgs e)
{
e.Handled = true;
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void pDFəÇıxartToolStripMenuItem_Click(object sender, EventArgs e)
{
//Creating iTextSharp Table from the DataTable data
PdfPTable pdfTable = new PdfPTable(dataGridView1.ColumnCount);
pdfTable.DefaultCell.Padding = 2;
pdfTable.WidthPercentage = 90;
pdfTable.HorizontalAlignment = Element.ALIGN_CENTER;
pdfTable.DefaultCell.BorderWidth = 1;
//Adding Header row
foreach (DataGridViewColumn column in dataGridView1.Columns)
{
PdfPCell cell = new PdfPCell(new Phrase(column.HeaderText));
cell.BackgroundColor = new iTextSharp.text.Color(230, 230, 230);
pdfTable.AddCell(cell);
}
//Adding DataRow
foreach (DataGridViewRow row in dataGridView1.Rows)
{
foreach (DataGridViewCell celli in row.Cells)
{
try
{
pdfTable.AddCell(celli.Value.ToString());
}
catch { }
}
}
SaveFileDialog sfd = new SaveFileDialog();
sfd.Filter = "Pdf File |*.pdf";
if (sfd.ShowDialog() == DialogResult.OK)
{
Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 10f);
PdfWriter wri = PdfWriter.GetInstance(pdfDoc, new FileStream(sfd.FileName, FileMode.Create));
pdfDoc.Open();
Paragraph paragraph = new Paragraph("Kreditin Hesablanması");
paragraph.Alignment = Element.ALIGN_CENTER;
pdfDoc.Add(paragraph);
pdfDoc.Add(pdfTable);
pdfDoc.Close();
MessageBox.Show("Ödəniş cədvəli PDF-ə çıxarıldı.");
}
}
}
}