private PdfPTable GetTable(Notes.Table table) { PdfPTable ownerTable = new PdfPTable(table.Data.GetLength(1)); ownerTable.HorizontalAlignment = Element.ALIGN_CENTER; ownerTable.SpacingBefore = 10; ownerTable.SetWidths(table.Widths); int rows = table.Data.GetLength(0); int columns = table.Data.GetLength(1); if (columns <= 2) { ownerTable.WidthPercentage = 50f; } else { ownerTable.WidthPercentage = 100f; } if (table.Header != null) { for (int i = 0; i < table.Header.Length; i++) { int alignment = i == 0 ? Element.ALIGN_LEFT : Element.ALIGN_RIGHT; PdfPCell cell = GetDataCell(table.Header[i], alignment, tableHeaderFont); //cell.BackgroundColor = backgroundColor; //cell.BorderColor = BaseColor.LightGray; cell.BorderWidth = 0; ownerTable.AddCell(cell); } } for (int i = 0; i < rows; i++) { for (int j = 0; j < columns; j++) { int alignment = j == 0 ? Element.ALIGN_LEFT : Element.ALIGN_RIGHT; PdfPCell cell = GetDataCell(table.Data[i, j], alignment, tableFont); ownerTable.AddCell(cell); } } return(ownerTable); }
public static Table GetTable(Reports.Table dataTable) { var tableWidth = dataTable.Data.GetLength(1) > 2 ? "5000" : "3000"; Table table = new Table(); TableStyle tableStyle = new TableStyle() { Val = "TableGrid" }; table.AppendChild(tableStyle); var borderColor = "909090"; // Create a TableProperties object and specify its border information. TableProperties tblProp = new TableProperties( new TableBorders( new TopBorder() { Val = new EnumValue <BorderValues>(BorderValues.BasicThinLines), Size = 2, Color = new StringValue(borderColor) }, new BottomBorder() { Val = new EnumValue <BorderValues>(BorderValues.BasicThinLines), Size = 2, Color = new StringValue(borderColor) }, new LeftBorder() { Val = new EnumValue <BorderValues>(BorderValues.BasicThinLines), Size = 2, Color = new StringValue(borderColor) }, new RightBorder() { Val = new EnumValue <BorderValues>(BorderValues.BasicThinLines), Size = 2, Color = new StringValue(borderColor) }, new InsideHorizontalBorder() { Val = new EnumValue <BorderValues>(BorderValues.BasicThinLines), Size = 2, Color = new StringValue(borderColor) }, new InsideVerticalBorder() { Val = new EnumValue <BorderValues>(BorderValues.BasicThinLines), Size = 2, Color = new StringValue(borderColor) } ), new TableCellMargin() { TopMargin = new TopMargin() { Width = new StringValue("100") }, BottomMargin = new BottomMargin() { Width = new StringValue("100") }, LeftMargin = new LeftMargin() { Width = new StringValue("200") }, RightMargin = new RightMargin() { Width = new StringValue("200") } }, new TableJustification() { Val = TableRowAlignmentValues.Center }, new TableWidth() { Type = TableWidthUnitValues.Pct, Width = new StringValue(tableWidth) } ); // Append the TableProperties object to the empty table. table.AppendChild <TableProperties>(tblProp); //table.AppendChild(grid); var cellProperties = new TableCellProperties() { TableCellWidth = new TableCellWidth() { Width = new StringValue("1250"), Type = TableWidthUnitValues.Pct }, TableCellMargin = new TableCellMargin() { BottomMargin = new BottomMargin() { Width = new StringValue("100"), Type = TableWidthUnitValues.Dxa }, TopMargin = new TopMargin() { Width = new StringValue("100"), Type = TableWidthUnitValues.Dxa }, LeftMargin = new LeftMargin() { Width = new StringValue("100"), Type = TableWidthUnitValues.Dxa }, RightMargin = new RightMargin() { Width = new StringValue("100"), Type = TableWidthUnitValues.Dxa } }, TableCellVerticalAlignment = new TableCellVerticalAlignment() { Val = TableVerticalAlignmentValues.Center } }; if (dataTable.Header != null) { TableRow tr = new TableRow(); for (int i = 0; i < dataTable.Header.Length; i++) { var title = dataTable.Header[i]; TableCell tc1 = new TableCell(); var justification = ((i > 0) ? JustificationValues.Right : JustificationValues.Left); var paragraph = new Paragraph() { ParagraphProperties = new ParagraphProperties() { Justification = new Justification() { Val = justification }, ContextualSpacing = new ContextualSpacing() { Val = false }, } }; var run = paragraph.AppendChild(new Run()); run.AppendChild(new Text(title)); run.RunProperties = new RunProperties(); run.RunProperties.AppendChild(new Bold()); run.RunProperties.AppendChild(new Color() { Val = "4682B4" }); var properties = (TableCellProperties)cellProperties.Clone(); properties.TableCellWidth = new TableCellWidth() { Width = getWidthPercentage(i).ToString(), Type = TableWidthUnitValues.Pct }; tc1.AppendChild <TableCellProperties>(properties); tc1.Append(paragraph); tr.Append(tc1); } table.Append(tr); } int rows = dataTable.Data.GetLength(0); int columns = dataTable.Data.GetLength(1); for (int i = 0; i < rows; i++) { TableRow tr = new TableRow(); for (int j = 0; j < columns; j++) { TableCell tc1 = new TableCell(); var properties = (TableCellProperties)cellProperties.Clone(); properties.TableCellWidth = new TableCellWidth() { Width = getWidthPercentage(j).ToString(), Type = TableWidthUnitValues.Pct }; tc1.AppendChild <TableCellProperties>(properties); var justification = ((j > 0) ? JustificationValues.Right : JustificationValues.Left); var paragraph = new Paragraph() { ParagraphProperties = new ParagraphProperties() { Justification = new Justification() { Val = justification } } }; var run = paragraph.AppendChild(new Run()); run.RunProperties = new RunProperties(); run.RunProperties.Color = new Color() { Val = "363844" }; run.AppendChild(new Text(dataTable.Data[i, j])); tc1.Append(paragraph); tr.Append(tc1); } table.Append(tr); } return(table); }