请问一下,
我要把DATATABLE 汇出成PDF ,汇出的资料页数不固定,
如果希望每一页都有标题,请问标题语法要怎么打?
谢谢
//设定中文字体
string fontsfolder =
System.Environment.GetFolderPath(System.Environment.SpecialFolder.Fonts);
string chFontPath = fontsfolder + "\\KAIU.ttf";//windows内建的
SimHei字体(中易黑体)
// string chFontPath = fontsfolder + "\\msjh.ttf";//windows内建的
SimHei字体(中易黑体)
BaseFont chBaseFont = BaseFont.CreateFont(chFontPath,
BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
// Phrase helloPhrase = new Phrase();
iTextSharp.text.Font twFont = new
iTextSharp.text.Font(chBaseFont, 12);
// iTextSharp.text.Font headerFont = new
iTextSharp.text.Font(chBaseFont, 18);
iTextSharp.text.Font headerFont = new
iTextSharp.text.Font(chBaseFont, 16, iTextSharp.text.Font.NORMAL, new
iTextSharp.text.BaseColor(51, 0, 153));
//Creating iTextSharp Table from the DataTable data
PdfPTable pdfTable = new PdfPTable(dataGridView1.ColumnCount);
pdfTable.DefaultCell.Padding = 3;
//use 100% page width
pdfTable.WidthPercentage = 100;//字段间距
pdfTable.HorizontalAlignment = Element.ALIGN_MIDDLE;//置中
pdfTable.DefaultCell.BorderWidth = 20;
//Adding Header row
foreach (DataGridViewColumn column in dataGridView1.Columns)
{
//Asign Chinese Font to cell
PdfPCell cell = new PdfPCell(new Phrase(column.HeaderText,
twFont));
cell.BackgroundColor = new iTextSharp.text.BaseColor(240,
240, 240);
pdfTable.AddCell(cell);
}
//Adding DataRow
foreach (DataGridViewRow row in dataGridView1.Rows)
{
foreach (DataGridViewCell cell in row.Cells)
{
if (cell.Value != null)
{
PdfPCell datacell = new PdfPCell(new
Phrase(cell.Value.ToString(), twFont));
datacell.BackgroundColor = new
iTextSharp.text.BaseColor(255, 255, 255);
pdfTable.AddCell(datacell);
}
}
}
//Exporting to PDF
string folderPath =
Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);//取得桌
面相对路径;
if (!Directory.Exists(folderPath))
{
Directory.CreateDirectory(folderPath);
}
using (FileStream stream = new FileStream(folderPath+
"\\"+textBox1.Text +"复健科治疗师服务量.pdf", FileMode.Create))
{
Document pdfDoc = new Document(PageSize.A4, 28f, 28f, 30f,
30f);
//大小方向 左边界 右边界 上边界 下边界
// Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f,
0f);//横向
PdfWriter.GetInstance(pdfDoc, stream);
pdfDoc.Open();
pdfDoc.Add(new Paragraph(" "+textBox1.Text+" 复健科治疗师服务
量", headerFont));//繁中
Paragraph paragraph = new Paragraph(" ");
pdfDoc.Add(paragraph);
pdfDoc.Add(pdfTable);
pdfDoc.Close();
stream.Close();
}