How to Merge Cells, Contorl Styling, and {DF Table Content in .NET

How to Merge Cells, Contorl Styling, and {DF Table Content in .NET

Professional publishing and design require tables that go beyond plain rows and columns. With the Aspose.PDF.TableGenerator Plugin for .NET, you can create visually compelling tables with merged cells, custom styling, and rich content—ideal for reports, catalogs, and digital publishing.


Borders and Background Colors

Customize cell and row appearance:

using Aspose.Pdf.Plugins;
using System.Drawing;

var generator = new TableGenerator();
var tableOptions = new TableOptions()
    .InsertPageAfter(1)
    .AddTable();

// Header row with background and border styling
var header = new TableRowBuilder()
    .SetBackgroundColor(Color.LightSteelBlue)
    .SetBorder(Color.DarkBlue, 2);
header.AddCell(new TableCellBuilder().AddParagraph(new TextFragment("Header 1")));
header.AddCell(new TableCellBuilder().AddParagraph(new TextFragment("Header 2")));
tableOptions.AddRow(header);

// Data row with custom cell color
var row = new TableRowBuilder();
row.AddCell(new TableCellBuilder()
    .SetBackgroundColor(Color.PaleGreen)
    .AddParagraph(new TextFragment("Value 1")));
row.AddCell(new TableCellBuilder()
    .SetBackgroundColor(Color.WhiteSmoke)
    .AddParagraph(new TextFragment("Value 2")));
tableOptions.AddRow(row);

tableOptions.AddInput(new FileDataSource(@"C:\Docs\input.pdf"));
tableOptions.AddOutput(new FileDataSource(@"C:\Docs\styled_table.pdf"));
generator.Process(tableOptions);

Row/Column Merging (Colspan/Rowspan)

Merge cells to create summary rows, headers, or special sections:

var mergedRow = new TableRowBuilder();
mergedRow.AddCell(
    new TableCellBuilder()
        .SetColSpan(2) // Merge two columns
        .SetBackgroundColor(Color.LightYellow)
        .AddParagraph(new TextFragment("Merged across two columns")));
tableOptions.AddRow(mergedRow);

Advanced Content: HTML and Rich Text

  • HTML fragments: Use HtmlFragment to embed styled text, hyperlinks, or inline images in any cell.
  • Images: Insert logos or graphics using ImageFragment.
  • TeX/Math: Add equations with TeXFragment.
tableOptions.AddRow(new TableRowBuilder()
    .AddCell(new TableCellBuilder().AddParagraph(new HtmlFragment("<b>Bold &amp; styled text</b>"))));

Setting Cell Widths and Alignment

  • Use SetWidth, SetMinWidth, and SetMaxWidth for custom sizing.
  • Control text alignment with SetHorizontalAlignment and SetVerticalAlignment.

Use Cases

  • Business/financial reports with summary rows and highlights
  • Catalogs and pricing tables with styled headers
  • Academic documents with equations, multi-line cells, and references

Frequently Asked Questions

Q: Are HTML fragments supported in table cells? A: Yes! Use HtmlFragment for styled text, links, or images within any cell.

Q: How do I set custom cell widths? A: Use SetWidth on a TableCellBuilder for absolute or relative sizing.

Q: Can I merge cells vertically (rowspan)? A: Yes—use SetRowSpan to merge cells across rows (if supported by plugin API version).


Pro Tip: Experiment with custom colors, images, and layout for standout business and design documents. Combine with batch table generation for reporting at scale.

 English