ststeiger / PdfSharpCore

Port of the PdfSharp library to .NET Core - largely removed GDI+ (only missing GetFontData - which can be replaced with freetype2)
Other
1.06k stars 235 forks source link

How to add a table with 3 rows and 5 columns in the document? #341

Open ldg1989 opened 1 year ago

ldg1989 commented 1 year ago

How to add a table with 3 rows and 5 columns in the document?

tossnet commented 11 months ago

You can inspire it with this code :

private static Table CreateAdvancedTable(Document document)
{
    document.LastSection.AddParagraph("Advanced Table", "Heading2");

    Table table = new();
    table.Borders.Width = 0.75;

    Column column = table.AddColumn(Unit.FromCentimeter(3));
    column.Format.Alignment = ParagraphAlignment.Center;

    table.AddColumn(Unit.FromCentimeter(2));
    table.AddColumn(Unit.FromCentimeter(2));
    table.AddColumn(Unit.FromCentimeter(3));
    table.AddColumn(Unit.FromCentimeter(4));
    table.AddColumn(Unit.FromCentimeter(5));

    Row row = table.AddRow();
    row.Shading.Color = Colors.PaleGoldenrod;
    Cell cell = row.Cells[0];
    cell.AddParagraph("Date");
    cell = row.Cells[1];
    cell.AddParagraph("Temps (C)");
    cell = row.Cells[2];
    cell.AddParagraph("Temps (F)");
    cell = row.Cells[3];
    cell.AddParagraph("Summary");

    // Create the header of the table
    row = table.AddRow();
    row.HeadingFormat = true;
    row.Format.Alignment = ParagraphAlignment.Center;
    row.Format.Font.Bold = true;
    row.Shading.Color = MigraDocCore.DocumentObjectModel.Colors.Green;
    row.Cells[0].AddParagraph("Item");
    row.Cells[0].Format.Font.Bold = false;
    row.Cells[0].Format.Alignment = ParagraphAlignment.Left;
    row.Cells[0].VerticalAlignment = MigraDocCore.DocumentObjectModel.Tables.VerticalAlignment.Bottom;
    row.Cells[0].MergeDown = 1;
    row.Cells[1].AddParagraph("Title and Author");
    row.Cells[1].Format.Alignment = ParagraphAlignment.Left;
    row.Cells[1].MergeRight = 3;
    row.Cells[5].AddParagraph("Extended Price");
    row.Cells[5].Format.Alignment = ParagraphAlignment.Left;
    row.Cells[5].VerticalAlignment = MigraDocCore.DocumentObjectModel.Tables.VerticalAlignment.Bottom;
    row.Cells[5].MergeDown = 1;

    row = table.AddRow();
    row.HeadingFormat = true;
    row.Format.Alignment = ParagraphAlignment.Center;
    row.Shading.Color = MigraDocCore.DocumentObjectModel.Colors.Orange;
    row.Cells[1].AddParagraph("Quantity");
    row.Cells[1].Format.Alignment = ParagraphAlignment.Left;
    row.Cells[2].AddParagraph("Unit Price");
    row.Cells[2].Format.Font.Bold = true;
    row.Cells[2].Format.Alignment = ParagraphAlignment.Left;
    row.Cells[3].AddParagraph("Discount (%)");
    row.Cells[3].Format.Alignment = ParagraphAlignment.Left;
    row.Cells[3].Shading.Color = MigraDocCore.DocumentObjectModel.Colors.Cyan;
    row.Cells[4].AddParagraph("Taxable");
    row.Cells[4].Format.Alignment = ParagraphAlignment.Left;
    row.Cells[4].Shading.Color = MigraDocCore.DocumentObjectModel.Colors.Transparent;
    row.Cells[4].Format.Font.Color = MigraDocCore.DocumentObjectModel.Colors.Red;

    table.SetEdge(0, 0, 6, 2, Edge.Box, BorderStyle.Single, 0.75, Color.Empty);

    document.LastSection.Add(table);

    return table;
}