在migradoc

时间:2018-06-13 16:21:11

标签: c# .net migradoc

我的数据库表格看起来像

id | Name | ImagePath
-------------------------
 1 | aaa         | xxx
 2 | bbb         | yyy
 3 | ccc         | zzz

对于我想要创建表格的每一行,所以当我生成我的pdf时,每一行在pdf中都会如下所示

--------------------|-------------------
1                   | 
aaa                 |xxx (but the actual image)
--------------------|-------------------

--------------------|-------------------
2                   | 
bbb                 |yyy (but the actual image)
--------------------|-------------------

--------------------|-------------------
3                   | 
ccc                 |zzz (but the actual image)
--------------------|-------------------

将值传递给函数并创建表

        public MigraDoc.DocumentObjectModel.Tables.Table createTable(List<string> listOfValues)
        {
            MigraDoc.DocumentObjectModel.Tables.Table table = new MigraDoc.DocumentObjectModel.Tables.Table();
            //table = page.AddTable();
            table.Style = "Table";
            table.Borders.Color = Colors.Black;
            table.Borders.Width = 0.25;
            table.Borders.Left.Width = 0.5;
            table.Borders.Right.Width = 0.5;
            table.Rows.LeftIndent = 0;

            //create column
            MigraDoc.DocumentObjectModel.Tables.Column column = new MigraDoc.DocumentObjectModel.Tables.Column();
            column = table.AddColumn("10cm");
            column.Format.Alignment = ParagraphAlignment.Left;
            //MigraDoc.DocumentObjectModel.Tables.Row tableRow = table.AddRow();
                MigraDoc.DocumentObjectModel.Tables.Column ImageColumn = new MigraDoc.DocumentObjectModel.Tables.Column();
            ImageColumn = table.AddColumn("10cm");
            ImageColumn.Format.Alignment = ParagraphAlignment.Left;

            //create rows from list of values
            foreach (var value in listOfValues)
            {
                MigraDoc.DocumentObjectModel.Tables.Row tableRow = table.AddRow();
                //if it is an image add to column 2
                if (value.Contains("Image="))
                {
                    tableRow.Cells[1].AddParagraph(value);
                    //merge the cells for the image column (zero based)
                    tableRow.Cells[1].MergeDown = listOfValues.Count - 1;
                }
                else
                {
                    //add the value
                    tableRow.Cells[0].AddParagraph(value);
                }
              }
              return table
}

上面创建了一个表,然后将其返回,将其放入一个列表(类型为Table)

然后将表格列表传递给生成pdf的方法。

public void generatePDF(List<MigraDoc.DocumentObjectModel.Tables.Table> listOfTables) 
    {
    //who generated teh report
    string author = Environment.UserName;
    DateTime currentTime = DateTime.Now;
    Document document = new Document();

    string pageHeaderText = "Report";
    string pageFooterText = string.Format("Report Generated by {0} at {1} on {2}", author, currentTime.ToShortTimeString(), currentTime.ToShortDateString());
    Style style = document.Styles["Normal"];
    style.Font.Name = "Arial Unicode MS";

    //table Style
    style = document.Styles.AddStyle("Table", "Normal");
    style.Font.Name = "Verdana";
    style.Font.Size = 9;

    Section page = document.AddSection();

    //header
    Paragraph header = page.Headers.Primary.AddParagraph();
    header.AddText(pageHeaderText);
    header.Format.Alignment = ParagraphAlignment.Center;

    //footer
    Paragraph footer = page.Footers.Primary.AddParagraph();
    footer.AddText(pageFooterText);
    footer.Format.Alignment = ParagraphAlignment.Center;

    //Main paragraph
    Paragraph mainPara = page.AddParagraph("test\n");

    //go through each table in the list and add to teh page

    foreach (var table in listOfTables)
    {
        MigraDoc.DocumentObjectModel.Tables.Table tableToAdd = new MigraDoc.DocumentObjectModel.Tables.Table();
        tableToAdd = table;
        tableToAdd = page.AddTable();
    }

    PdfDocumentRenderer pdfRenderer = new PdfDocumentRenderer(true);//false, pdfRenderer.);
    pdfRenderer.Document = document;
    pdfRenderer.RenderDocument();

    //save and open the file
    fileName = "test;
    //show file save dialog
    SaveFileDialog sfd = new SaveFileDialog();
    sfd.DefaultExt = ".pdf";
    sfd.AddExtension = true;
    sfd.FileName = fileName;
    if (sfd.ShowDialog() == DialogResult.OK)
    {
        savePath = System.IO.Path.GetDirectoryName(sfd.FileName);
        pdfRenderer.PdfDocument.Save(savePath + "\\" + fileName + ".pdf" );
        Process.Start(savePath + "\\" + fileName + ".pdf");
    }
}

我遇到的问题是表格没有在pdf上生成。

如果我传入一个字符串列表而不是表的列表并循环,则会生成表,但我不确定为什么在循环时不会生成使用列表(类型表)的原因。 / p>

1 个答案:

答案 0 :(得分:1)

计算机通常做你告诉他们要做的事,而不是你想做的事。

看看这段代码:

    tableToAdd = table;
    tableToAdd = page.AddTable();

您向页面添加一个新的空表,并将新表分配给变量tableToAdd,从而丢失对包含内容的表的引用。

执行您期望的代码甚至更简单:

    page.Add(tableToAdd);

它将包含内容的表添加到该部分。

BTW:page不是Section对象的最佳名称。