Articles in this section

How to group contents in a PdfLightTable?

Currently WinForms PDF does not support grouping the contents present in a table. To achieve this, group the input DataTable before applying the values to PdfLightTable.

Assemblies Required:

  1. Syncfusion.Pdf.Base.dll
  2. Syncfusion.Compression.Base.dll

The below code snippet illustrates how to group the contents in a PdfLightTable.

C#

// Create a new PDF document instance
PdfDocument document = new PdfDocument();

#region Style for Table

// Create a border with a transparent color.
PdfPen borderPen = new PdfPen(Color.Black);
borderPen.Width = 1.0f;

// Assign the border pen to the table cell.
PdfCellStyle defStyle = new PdfCellStyle();
defStyle.BorderPen = borderPen;

PdfLightTableLayoutFormat format = new PdfLightTableLayoutFormat();
format.Layout = PdfLayoutType.Paginate;
#endregion

#region Functionality for the requirement
// Sort the DataTable
dataTable.DefaultView.Sort = "Column1 asc";
dataTable = dataTable.DefaultView.ToTable();

string lastValue = dataTable.Rows[0][0] as string;
DataTable tempDataTable = new DataTable();
tempDataTable = dataTable.Clone();

// Logic to separate the DataTable
foreach (DataRow row in dataTable.Rows)
{
    if (lastValue == row.ItemArray[0] as string)
    {
        tempDataTable.Rows.Add(row.ItemArray);
    }
    else
    {
        lastValue = row.ItemArray[0] as string;
        // Create the PdfLightTable
        PdfLightTable tblPDF = new PdfLightTable();
        tblPDF.Style.DefaultStyle = defStyle;
        tblPDF.Style.ShowHeader = true;
        tblPDF.Style.RepeatHeader = false;

        // Assign the data source
        tblPDF.DataSource = tempDataTable;

        // Draw the table
        tblPDF.Draw(document.Pages.Add(), 0, 0);
        tempDataTable = new DataTable();
        tempDataTable = dataTable.Clone();
    }
}
tempDataTable.Rows.Add(dataTable.Rows[dataTable.Rows.Count - 1].ItemArray);
PdfLightTable tblPDF1 = new PdfLightTable();
tblPDF1.Style.DefaultStyle = defStyle;
tblPDF1.Style.ShowHeader = true;
tblPDF1.Style.RepeatHeader = false;
tblPDF1.DataSource = tempDataTable;

tblPDF1.Draw(document.Pages.Add(), 0, 0);
#endregion

// Save the PDF document
document.Save("Sample1.pdf");
document.Close(true);
System.Diagnostics.Process.Start("Sample1.pdf");
this.Close();

 

The working sample can be downloaded from the below link:

https://www.syncfusion.com/downloads/support/directtrac/general/PDFlight_table-205499629.zip

Conclusion

I hope you enjoyed learning about how to group contents in a Pdf Light Table.

You can refer to our WinForms PDF’s feature tour page to know about its other groundbreaking feature representations.

For current customers, you can check out our WinForms  from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our WinForms PDF and other WinForms components.

If you have any queries or require clarifications, please let us know in comments below. You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!

 

 

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Access denied
Access denied