【问题标题】:Using iTextSharp to export to PDF and repeat headers使用 iTextSharp 导出为 PDF 并重复标题
【发布时间】:2016-11-14 18:49:47
【问题描述】:

我浏览了很多帖子并尝试了建议的所有内容,但我的 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 设置为在新页面上开始。这甚至可能吗?

如果有任何帮助,我将不胜感激。谢谢。

【问题讨论】:

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


    【解决方案1】:

    所以我能够让分页符工作。所以认为这也给了我想要的结果,我会发布作为答案。

    我通过添加 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);
    

    【讨论】:

      【解决方案2】:

      如果您以这种方式在 webgridstyle 字符串中的 .webgrid-table 中添加该行,我在 repeat-header:yes 上取得了成功

      string webgridstyle = " .webgrid-table {    " +
      
                      "           border: solid 0.1px #000000; " +
                      "           background-color: white; " +
                      "           repeat-header:yes;  " +
                      "       }   " +
      

      这就是它对我的工作方式。从屏幕截图中可以看出,第二页也有我的重复标题

      【讨论】:

      • 感谢您的建议。我已经有一段时间没有从事这个项目了,但如果我能找到时间,我会尝试你的建议并回复你。再次感谢!
      • 所以我需要在另一个项目上做同样的事情并尝试了你的建议,但它似乎不起作用:(
      • 您的 CSS 位于 PM.Pipeline_HtmlForExport()?你的css中的任何地方都有repeat-header:yes;这一行吗?
      • 正确的是。没有其他地方没有。 :(
      • 你能发布你的webgridstylecss
      猜你喜欢
      • 2023-03-12
      • 1970-01-01
      • 2013-08-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-06-19
      • 2011-12-31
      相关资源
      最近更新 更多