程式碼如下,套印的方式為橫向90度,第二頁之後會消失(如附圖)
//新建一个PDF文档
PdfDocument pdf = new PdfDocument();
////添加一页
PdfPageBase page = pdf.Pages.Add(PdfPageSize.A4, new PdfMargins(), PdfPageRotateAngle.RotateAngle90);
page.Canvas.RotateTransform(-90);
//创建PdfGrid类的对象
PdfGrid grid = new PdfGrid();
//设置单元格填充
grid.Style.CellPadding = new PdfPaddings(1, 1, 1, 1);
//添加表格列数
grid.Columns.Add(3);
grid.Columns[0].Width = 100f;
grid.Columns[1].Width = 100f;
grid.Columns[2].Width = 100f;
//添加表头行及表格数据
PdfGridRow[] pdfGridRows = grid.Headers.Add(1);
for (int i = 0; i < pdfGridRows.Length; i++)
{
pdfGridRows[i].Style.Font = new PdfTrueTypeFont(new Font("Arial", 11f, FontStyle.Regular), true);//指定字体
pdfGridRows[i].Cells[0].Value = "NAME";
pdfGridRows[i].Cells[1].Value = "SUBJECT";
pdfGridRows[i].Cells[2].Value = "SCORES";
pdfGridRows[i].Style.TextBrush = PdfBrushes.Red;
}
//设置重复表头(表格跨页时)
grid.RepeatHeader = true;
//添加数据到表格
for (int i = 0; i < 100; i++)
{
PdfGridRow row = grid.Rows.Add();
for (int j = 0; j < grid.Columns.Count; j++)
{
row.Cells[j].Value = "(Row " + i + ", column " + j + ")";
}
}
//在PDF页面绘制表格
grid.Draw(page, new PointF(-800, 80));
//保存文档
pdf.SaveToFile("Result.pdf");
System.Diagnostics.Process.Start("Result.pdf");