c# pdf한글출력하기 & 프린트페이지 띄우기
private void button_pdf_Click(object sender, EventArgs e)
{
Document doc = new Document(iTextSharp.text.PageSize.LETTER, 10, 10, 42, 35);
PdfWriter wri = PdfWriter.GetInstance(doc, new FileStream("Test.pdf", FileMode.Create));
doc.Open();
PdfPTable table = new PdfPTable(dataGridView1.Columns.Count);
// 한글출력하는 부분 ////////////////////////////////////////////////////
// string batangttf = System.IO.Path.Combine(Environment.GetEnvironmentVariable("SystemRoot"), @"Fonts\BareunBatangB.ttf");
string batangttf = System.IO.Path.Combine(Environment.GetEnvironmentVariable("SystemRoot"), @"Fonts\HYGSRB.ttf");
BaseFont batangBase = BaseFont.CreateFont(batangttf, BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
var batang = new iTextSharp.text.Font(batangBase, 12F);
/////////////////////////////////////////////////////////////////////////////////////////////////////
for(int j=0;j<dataGridView1.Columns.Count;j++)
{
table.AddCell(new Phrase(dataGridView1.Columns[j].HeaderText,batang));
}
table.HeaderRows = 1;
for (int i=0; i<dataGridView1.Rows.Count;i++)
{
for(int k=0; k<dataGridView1.Columns.Count;k++)
{
if(dataGridView1[k,i].Value != null)
{
table.AddCell(new Phrase(dataGridView1[k, i].Value.ToString()));
}
}
}
doc.Add(table);
doc.Close();
}
프린트페이지띄우기
Bitmap bmp;
private void button_pdf_Click(object sender, EventArgs e)
{
int height = dataGridView1.Height;
dataGridView1.Height = dataGridView1.RowTemplate.Height = 2;
bmp = new Bitmap(dataGridView1.Width, dataGridView1.Height);
dataGridView1.DrawToBitmap(bmp, new Rectangle(0, 0, dataGridView1.Width, dataGridView1.Height));
dataGridView1.Height = height;
printPreviewDialog1.ShowDialog();
}
private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
e.Graphics.DrawImage(bmp, 0, 0);
}
}