为有中文需求的客户提供多渠道中文技术支持.

Mon Apr 04, 2022 4:01 am

程式碼如下,套印的方式為橫向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");

heken833
 
Posts: 1
Joined: Wed Sep 22, 2021 7:01 am

Tue Apr 05, 2022 1:35 pm

您好,

感謝您的咨詢。

請您參考以下修改後的程式碼進行測試。如果您還有其他問題,請您隨時聯繫我們。
Code: Select all
            //新建一个PDF文档
            PdfDocument pdf = new PdfDocument();

            ////添加一页
            ///横向
            SizeF size = new SizeF(PdfPageSize.A4.Height, PdfPageSize.A4.Width);
            PdfPageBase page = pdf.Pages.Add(size, new PdfMargins());

            //page.Canvas.RotateTransform(-90);

            //创建PdfGrid类的对象
            PdfGrid grid = new PdfGrid();
            grid.AllowCrossPages = true;

            //设置单元格填充
            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(0, 80));


            //保存文档
            pdf.SaveToFile("Result.pdf");
            System.Diagnostics.Process.Start("Result.pdf");
Sincerely,
Andy
E-iceblue support team
User avatar

Andy.Zhou
 
Posts: 483
Joined: Mon Mar 29, 2021 3:03 am

Return to 中文技术支持

cron