C#/VB.NET 设置PDF跨页表格重复显示表头行

本文介绍了如何在C#和VB.NET中使用Spire.PDF库,通过设置grid.RepeatHeader=true,解决PDF表格跨页时表头不显示的问题,提供详细代码示例和实际效果展示。

在创建表格时,如果表格内容出现跨页显示的时候,默认情况下该表格的表头不会在下一页显示,在阅读体验上不是很好。下面分享一个方法如何在表格跨页时显示表格的表头内容,在C#中只需要简单使用方法 grid.RepeatHeader = true即可。具体参考如下方法步骤。另附VB.NET代码,有需可供参考。

1.在VS程序中添加引用Spire.PDF.dll

方法1:通过Nuget搜索下载安装。

在“解决方案资源管理器”中,鼠标右键点击“添加引用”—“ 管理NuGet包”

完成安装。引用结果:

方法2:下载Free Spire.PDF for .NET包到本地。解压。在VS中的“解决方案资源管理器”中,鼠标右键点击“添加引用”-将解压包Bin文件夹下的dll添加引用至vs。

2.代码示例

C#

using Spire.Pdf;
using Spire.Pdf.Graphics;
using Spire.Pdf.Grid;
using System.Drawing;

namespace RepeatTableHeaderRow
{
    class Program
    {
        static void Main(string[] args)
        {
            //新建一个PDF文档
            PdfDocument pdf = new PdfDocument();

            //添加一页
            PdfPageBase page = pdf.Pages.Add();

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

            //设置单元格填充
            grid.Style.CellPadding = new PdfPaddings(1, 1, 1, 1);

            //添加表格列数
            grid.Columns.Add(3);

            //添加表头行及表格数据
            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;
                /*pdfGridRows[i].Style.Font = new PdfCjkStandardFont(PdfCjkFontFamily.HanyangSystemsGothicMedium,12f,PdfFontStyle.Regular);//绘制中日韩字体的方法
                pdfGridRows[i].Cells[0].Value = "이 름";
                pdfGridRows[i].Cells[1].Value = "科 目";
                pdfGridRows[i].Cells[2].Value = "ほしとり";
                pdfGridRows[i].Style.TextBrush = PdfBrushes.Blue;
                */

            }

            //设置重复表头(表格跨页时)
            grid.RepeatHeader = true;

            //添加数据到表格
            for (int i = 0; i < 60; 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, 20));          

            //保存文档
            pdf.SaveToFile("Result.pdf");
            System.Diagnostics.Process.Start("Result.pdf");
        }
    }
}

执行程序后,在VS的程序项目文件夹下可查看生成的PDF文档,如C:\Users\Administrator\Documents\Visual Studio 2017\Projects\DrawTable_PDF\RepeatTableHeaderRow\bin\Debug\Result.pdf

文件路径也可以定义为其他路径。

跨页表头效果:

VB.NET

Imports Spire.Pdf
Imports Spire.Pdf.Graphics
Imports Spire.Pdf.Grid
Imports System.Drawing

Namespace RepeatTableHeaderRow
	Class Program
		Private Shared Sub Main(args As String())
			'新建一个PDF文档
			Dim pdf As New PdfDocument()

			'添加一页
			Dim page As PdfPageBase = pdf.Pages.Add()

			'创建PdfGrid类的对象
			Dim grid As New PdfGrid()

			'设置单元格填充
			grid.Style.CellPadding = New PdfPaddings(1, 1, 1, 1)

			'添加表格列数
			grid.Columns.Add(3)

			'添加表头行及表格数据
			Dim pdfGridRows As PdfGridRow() = grid.Headers.Add(1)
			For i As Integer = 0 To pdfGridRows.Length - 1
				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.Font = new PdfCjkStandardFont(PdfCjkFontFamily.HanyangSystemsGothicMedium,12f,PdfFontStyle.Regular);'绘制中日韩字体的方法
'                pdfGridRows[i].Cells[0].Value = "이 름";
'                pdfGridRows[i].Cells[1].Value = "科 目";
'                pdfGridRows[i].Cells[2].Value = "ほしとり";
'                pdfGridRows[i].Style.TextBrush = PdfBrushes.Blue; 

				pdfGridRows(i).Style.TextBrush = PdfBrushes.Red
			Next

			'设置重复表头(表格跨页时)
			grid.RepeatHeader = True

			'添加数据到表格
			For i As Integer = 0 To 59
				Dim row As PdfGridRow = grid.Rows.Add()
				For j As Integer = 0 To grid.Columns.Count - 1
					row.Cells(j).Value = "(Row " + i + ", column " + j + ")"
				Next
			Next

			'在PDF页面绘制表格
			grid.Draw(page, New PointF(0, 20))

			'保存文档
			pdf.SaveToFile("Result.pdf")
			System.Diagnostics.Process.Start("Result.pdf")
		End Sub
	End Class
End Namespace

—End—

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值