I have DB table that looks like
id | Name | ImagePath
-------------------------
1 | aaa | xxx
2 | bbb | yyy
3 | ccc | zzz
For each row I want to create a table, so when i generate my pdf each row will look like this in the pdf
--------------------|-------------------
1 |
aaa |xxx (but the actual image)
--------------------|-------------------
--------------------|-------------------
2 |
bbb |yyy (but the actual image)
--------------------|-------------------
--------------------|-------------------
3 |
ccc |zzz (but the actual image)
--------------------|-------------------
The values are passed into a function with creates the table
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
}
The above created a table and then returns it, which is put into a list (of type Table)
The list of tables is then passed into a method that generates the 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");
}
}
The problem that I am having is that the table does not get generated on the pdf.
If i pass in a list of strings rather than the list of tables and loop through, the table does get generated, but I am unsure why when using the list (of type tables) does not get generated when looping through.
Computers often do what you tell 'em to do, not what you want 'em to do.
Look at this piece of code:
tableToAdd = table;
tableToAdd = page.AddTable();
You add a new, empty table to the page and assign the new table to the variable tableToAdd
, thus losing the reference to the table with the contents.
The code that does what you expect is even simpler:
page.Add(tableToAdd);
It adds the table with the contents to the section.
BTW: page
is not the best name for a Section
object.