/// <summary>Creates the table.</summary> /// <param name="section">The section.</param> /// <param name="tableObj">The table to convert to html.</param> private void CreateTable(Section section, AutoDocumentation.Table tableObj) { var table = section.AddTable(); table.Style = "Table"; table.Borders.Color = Colors.Blue; table.Borders.Width = 0.25; table.Borders.Left.Width = 0.5; table.Borders.Right.Width = 0.5; table.Rows.LeftIndent = 0; var gdiFont = new XFont("Arial", 10); XGraphics graphics = XGraphics.CreateMeasureContext(new XSize(2000, 2000), XGraphicsUnit.Point, XPageDirection.Downwards); // Add the required columns to the table. foreach (DataColumn column in tableObj.data.Table.Columns) { var column1 = table.AddColumn(); column1.Format.Alignment = ParagraphAlignment.Right; } // Add a heading row. var headingRow = table.AddRow(); headingRow.HeadingFormat = true; headingRow.Format.Font.Bold = true; headingRow.Shading.Color = Colors.LightBlue; for (int columnIndex = 0; columnIndex < tableObj.data.Table.Columns.Count; columnIndex++) { // Get column heading string heading = tableObj.data.Table.Columns[columnIndex].ColumnName; headingRow.Cells[columnIndex].AddParagraph(heading); // Get the width of the column double maxSize = graphics.MeasureString(heading, gdiFont).Width; for (int rowIndex = 0; rowIndex < tableObj.data.Count; rowIndex++) { // Add a row to our table if processing first column. MigraDoc.DocumentObjectModel.Tables.Row row; if (columnIndex == 0) { table.AddRow(); } // Get the row to process. row = table.Rows[rowIndex + 1]; // Convert potential HTML to the cell in our row. HtmlToMigraDoc.Convert(tableObj.data[rowIndex][columnIndex].ToString(), row.Cells[columnIndex], WorkingDirectory); // Update the maximum size of the column with the value from the current row. foreach (var element in row.Cells[columnIndex].Elements) { if (element is Paragraph) { var paragraph = element as Paragraph; var contents = string.Empty; foreach (var paragraphElement in paragraph.Elements) { if (paragraphElement is MigraDoc.DocumentObjectModel.Text) { contents += (paragraphElement as MigraDoc.DocumentObjectModel.Text).Content; } else if (paragraphElement is MigraDoc.DocumentObjectModel.Hyperlink) { contents += (paragraphElement as MigraDoc.DocumentObjectModel.Hyperlink).Name; } } var size = graphics.MeasureString(contents, gdiFont); maxSize = Math.Max(maxSize, size.Width); } } } table.Columns[columnIndex].Width = Unit.FromPoint(maxSize + 10); } //for (int rowIndex = 0; rowIndex < tableObj.data.Count; rowIndex++) //{ // row = table.AddRow(); // for (int columnIndex = 0; columnIndex < tableObj.data.Table.Columns.Count; columnIndex++) // { // string cellText = tableObj.data[rowIndex][columnIndex].ToString(); // // var match = hrefRegEx.Match(cellText); // if (match.Success) // { // var paragraph = row.Cells[columnIndex].AddParagraph(); // var hyperlink = paragraph.AddHyperlink(match.Groups[1].ToString().TrimStart('#'), HyperlinkType.Bookmark); // hyperlink.AddFormattedText(match.Groups[2].ToString(), TextFormat.Underline); // } // else // { // match = italicsRegEx.Match(cellText); // if (match.Success) // { // var para = row.Cells[columnIndex].AddParagraph(match.Groups[1].ToString()); // para.AddLineBreak(); // para.AddFormattedText(match.Groups[2].ToString(), TextFormat.Italic); // } // else // row.Cells[columnIndex].AddParagraph(cellText); // } // } // //} section.AddParagraph(); }
/// <summary>Creates the table.</summary> /// <param name="section">The section.</param> /// <param name="tableObj">The table to convert to html.</param> private void CreateTable(Section section, AutoDocumentation.Table tableObj) { var table = section.AddTable(); table.Style = "Table"; table.Borders.Color = Colors.Blue; table.Borders.Width = 0.25; table.Borders.Left.Width = 0.5; table.Borders.Right.Width = 0.5; table.Rows.LeftIndent = 0; var gdiFont = new XFont("Arial", 10); XGraphics graphics = XGraphics.CreateMeasureContext(new XSize(2000, 2000), XGraphicsUnit.Point, XPageDirection.Downwards); // Add the required columns to the table. foreach (DataColumn column in tableObj.data.Table.Columns) { var column1 = table.AddColumn(); column1.Format.Alignment = ParagraphAlignment.Right; } // Add a heading row. var headingRow = table.AddRow(); headingRow.HeadingFormat = true; headingRow.Format.Font.Bold = true; headingRow.Shading.Color = Colors.LightBlue; for (int columnIndex = 0; columnIndex < tableObj.data.Table.Columns.Count; columnIndex++) { // Get column heading string heading = tableObj.data.Table.Columns[columnIndex].ColumnName; headingRow.Cells[columnIndex].AddParagraph(heading); // Get the width of the column double maxSize = graphics.MeasureString(heading, gdiFont).Width; for (int rowIndex = 0; rowIndex < tableObj.data.Count; rowIndex++) { // Add a row to our table if processing first column. MigraDoc.DocumentObjectModel.Tables.Row row; if (columnIndex == 0) { table.AddRow(); } // Get the row to process. row = table.Rows[rowIndex + 1]; // Convert potential HTML to the cell in our row. HtmlToMigraDoc.Convert(tableObj.data[rowIndex][columnIndex].ToString(), row.Cells[columnIndex], WorkingDirectory); // Update the maximum size of the column with the value from the current row. foreach (var element in row.Cells[columnIndex].Elements) { if (element is Paragraph) { var paragraph = element as Paragraph; var contents = string.Empty; foreach (var paragraphElement in paragraph.Elements) { if (paragraphElement is MigraDoc.DocumentObjectModel.Text) { contents += (paragraphElement as MigraDoc.DocumentObjectModel.Text).Content; } else if (paragraphElement is MigraDoc.DocumentObjectModel.Hyperlink) { contents += (paragraphElement as MigraDoc.DocumentObjectModel.Hyperlink).Name; } } var size = graphics.MeasureString(contents, gdiFont); maxSize = Math.Max(maxSize, size.Width); } } } // maxWidth is the maximum allowed width of the column. E.g. if tableObj.ColumnWidth // is 50, then maxWidth is the amount of space taken up by 50 characters. // maxSize, on the other hand, is the length of the longest string in the column. // The actual column width is whichever of these two values is smaller. // MigraDoc will automatically wrap text to ensure the column respects this width. double maxWidth = graphics.MeasureString(new string('m', tableObj.ColumnWidth), gdiFont).Width; table.Columns[columnIndex].Width = Unit.FromPoint(Math.Min(maxWidth, maxSize) + 10); } section.AddParagraph(); }