使用iTextSharp导出为PDF并重复标题

时间:2016-07-12 13:32:39

标签: css asp.net-mvc-5 itext

我已经浏览了很多帖子并尝试了所有建议的内容,但是我的webgrid流向的页面上没有重复标题。

这是我的代码:

public FileStreamResult ExportCombinedPDF(int ProjectID)
    {
        List<PipelineDetails> PipeList = new List<PipelineDetails>();
        ProjectManager PM = new ProjectManager();

        PipeList = PM.GetPipelineList(ProjectID);

        //This is the css styling for the webgrid
        string webgridstyle = PM.Pipeline_HtmlForExport(ProjectID);
        //This is creating the table with other details also being exported
        string ProjectHtml = PM.Project_HtmlForExport(ProjectID);

        WebGrid grid = new WebGrid(source: PipeList, canPage: false, canSort: false);
        string gridHtml = grid.GetHtml(tableStyle: "webGrid",
                                       headerStyle: "webGridHeader",
                                       alternatingRowStyle: "webGridAlt",
                columns: grid.Columns(
                 grid.Column("NodeNumber", "Node Nr."),
                 grid.Column("Accumulated_Length", "Accumulated Length"),
                 grid.Column("Elevation", "Elevation"),
                 grid.Column("Pipe_Outside_Diameter", "Pipe Outside Diameter"),
                 grid.Column("Wall_Thickness", "Wall Thickness"),
                 grid.Column("Control_Point_Description", "Control Point Description"),
                 grid.Column("Control_Point_Size", "Control Point Size"))).ToString();

        string exportData = String.Format("<html><body>{0}{1} <br /> {2}</body></html>", "<style>" + webgridstyle + "</style>", ProjectHtml, gridHtml);

        var bytes = System.Text.Encoding.UTF8.GetBytes(exportData);
        using (var input = new MemoryStream(bytes))
        {
            var output = new MemoryStream();
            var document = new iTextSharp.text.Document(PageSize.A4, 50, 50, 50, 50);
            var writer = PdfWriter.GetInstance(document, output);

            Font headerFont = FontFactory.GetFont("Verdana", 10);
            Font rowfont = FontFactory.GetFont("Verdana", 10);

            writer.CloseStream = false;
            document.Open();

            var xmlWorker = iTextSharp.tool.xml.XMLWorkerHelper.GetInstance();
            xmlWorker.ParseXHtml(writer, document, input, System.Text.Encoding.UTF8);

            document.Close();
            output.Position = 0;

            return File(output, "application/pdf", "Pipeline_Report.pdf");

            //return new FileStreamResult(output, "application/pdf");
        }

    }

我尝试将repeat-header:yes添加到我的CSS中,但这不起作用。我也尝试在下面的代码中添加以下代码,但这也无效:

//This was done before the document was opened :
            PdfPTable table = new PdfPTable(7);

            table.HeaderRows = 2;

//This was done before the document was closed :

            document.Add(table);

但如上所述,这也行不通。我无法想象设置重复标题是如此困难。

否则,下一个选项是将WebGrid设置为在新页面上启动。这甚至可能吗?

我将不胜感激任何帮助。谢谢。

2 个答案:

答案 0 :(得分:0)

所以我能够使用分页功能。所以想到这也给了我一个我想要的结果,我会发布作为答案。

我通过添加PageBreak改变了我的exportData字符串:

string exportData = String.Format("<html><body>{0}{1} <p style='page-break-after:always;'></p> {2}</body></html>", "<style>" + webgridstyle + "</style>", ProjectHtml, gridHtml);

答案 1 :(得分:0)

如果您以这种方式在webgridstyle字符串中的repeat-header:yes内添加该行,那么我已成功使用.webgrid-table

string webgridstyle = " .webgrid-table {    " +

                "           border: solid 0.1px #000000; " +
                "           background-color: white; " +
                "           repeat-header:yes;  " +
                "       }   " +

这就是它对我有用的方式。正如您从屏幕截图中看到的那样,第二页也有我的重复标题

enter image description here