Thomas Hövel wrote:
I suspect you reduced the number of columns without adjusting the MergeRight property of some cells.
Merging with cells that don't exist causes an index out of range exception.
MergeRight is similar to colspan in HTML.
OK, thanks. See the source code:
protected void CreatePDF()
{
try
{
document = new Document();
document.Info.Title = "PdfSharpToPdf.aspx.cs";
document.Info.Subject = "Testing MigraDoc PDF Generator";
document.Info.Author = "Antony Lau";
//You should create at least one section for each MigraDoc document.
Section section = document.AddSection();
//Add a table to this section.
table = section.AddTable();
table.Borders.Color = new Color(81, 125, 192);
Column column;
//You must define your columns before you can add a row.
column = table.AddColumn("3cm"); // for first, last names
column = table.AddColumn("3cm"); // for phone and its extension
column = table.AddColumn("3cm"); // for location and room number.
Row row = table.AddRow(); // row 1, the header
row.HeadingFormat = true;
row.Format.Font.Bold = true;
row.Cells[0].AddParagraph("This is the header of the table in Bold");
row.Cells[0].MergeRight = 3;
row = table.AddRow(); // row 2
row.Format.Font.Bold = true;
row.Shading.Color = new Color(211, 211, 211);
row.Cells[1].AddParagraph("Visiting Scholars of Biochemistry".ToUpper());
row.Cells[1].Format.Alignment = ParagraphAlignment.Center;
row.Cells[1].MergeRight = 3;
row = table.AddRow(); // contact info row, row 3
row.Format.Font.Bold = false;
row.Cells[0].AddParagraph("Marietta Johnson");
row.Cells[1].AddParagraph("123-456-7890");
row.Cells[2].AddParagraph("Room 3141, Gregory Hall");
// How do we use SetEdge? For this table, I have 3 columns and 3 rows, so,
// should I do it like below?
table.SetEdge(0, 0, 3, 3, Edge.Box, MigraDoc.DocumentObjectModel.BorderStyle.Single, 0.75);
PdfDocumentRenderer pdfRender = new PdfDocumentRenderer(true);
pdfRender.Document = document;
pdfRender.RenderDocument(); // This line generates error.
pdfRender.Save("c:/phonebook_pdfsharp.pdf");
}
catch (Exception ex)
{
Response.Write("<b>StackTrace: </b><br/>" +ex.StackTrace + "<br/><br/>");
Response.Write("<b>Exception Message:</b><br/>" + ex.Message);
}