How to Start Tutorials Troubleshooting Main Operations Convert PDF Read PDF Edit PDF PDF Report Generator Work with PDF Modules PDF Document PDF Pages Text Image Graph & Path Annotation, Markup & Drawing Redaction Security Digital Signature Forms Watermark Bookmark Link File Attachment File Metadata Printing Work with Other SDKs Barcode read Barcode create OCR Twain

Using C# PDF Generator Library
How to create, generate a table in PDF file with xml template applied in C# asp.net, windows application


C# Demo Code to add a table with table template applied to adobe pdf file





In the following C# tutorial, you will learn how to create a PDF with table programmatically with XML template applied in your C# ASP.NET Web application and Windows application.

  • Design data class
  • Prepare the template XML configure file
  • Load the sample data
  • Generate PDF report file from database with template applied

How to create a PDF with template applied using C#

  1. Download XDoc.PDF Generator C# library
  2. Install C# library to create PDF file with XML template
  3. Step by Step Tutorial










In the following C# tutorial, you will learn how to generate a PDF with table programmatically with XML template applied in C#. After the guide steps finished, you will generate a PDF report below.









1. Design data class for customer data


In the following C# source code, you will learn how to design the table data.



public class Sample1TableDef
{
    //  Must list all properties of the Header tag in the XML file. (Name must be identity.)
    public const String WeeklySalesRecord_ProductNo = "Product No";
    public const String WeeklySalesRecord_ProductCode = "Product Code";
    public const String WeeklySalesRecord_ProductName = "Product Name";
    public const String WeeklySalesRecord_MondayCount = "Monday Count";
    public const String WeeklySalesRecord_TuesdayCount = "Tuesday Count";
    public const String WeeklySalesRecord_WednesdayCount = "Wednesday Count";
    public const String WeeklySalesRecord_ThursdayCount = "Thursday Count";
    public const String WeeklySalesRecord_FridayCount = "Friday Count";
    public const String WeeklySalesRecord_SaturdayCount = "Saturday Count";
    public const String WeeklySalesRecord_SundayCount = "Sunday Count";
    public const String WeeklySalesRecord_TotalCount = "Total Count";

    // Must list all styles in the Style tag. (Name must be identity.)
    public const String StyleID_Body_Default = "BodyDefault";
    public const String StyleID_Head_Default = "HeaderDefault";

    public static String GetHeaderType(PropEntry entry)
    {
        return Sample1TableDef.StyleID_Head_Default;
    }
}
public class WeeklySalesRecord : ValueEntry
{
    private String _num;
    private String _code;
    private String _product;

    private String _mon;
    private String _tue;
    private String _wed;
    private String _thu;
    private String _fri;
    private String _sat;
    private String _sun;

    private String _total;

    public WeeklySalesRecord(String num, String code, String product,
                             String mon, String tue, String wed, String thu, String fri, String sat, String sun,
                             String total)
        : base()
    {
        this._num = num;
        this._code = code;
        this._product = product;
        this._mon = mon;
        this._tue = thu;
        this._wed = wed;
        this._thu = thu;
        this._fri = fri;
        this._sat = sat;
        this._sun = sun;
        this._total = total;

        AddPropertyEntry(Sample1TableDef.WeeklySalesRecord_ProductNo, this._num, 1);
        AddPropertyEntry(Sample1TableDef.WeeklySalesRecord_ProductCode, this._code, 1);
        AddPropertyEntry(Sample1TableDef.WeeklySalesRecord_ProductName, this._product, 1);
        AddPropertyEntry(Sample1TableDef.WeeklySalesRecord_MondayCount, this._mon, 1);
        AddPropertyEntry(Sample1TableDef.WeeklySalesRecord_TuesdayCount, this._tue, 1);
        AddPropertyEntry(Sample1TableDef.WeeklySalesRecord_WednesdayCount, this._wed, 1);
        AddPropertyEntry(Sample1TableDef.WeeklySalesRecord_ThursdayCount, this._thu, 1);
        AddPropertyEntry(Sample1TableDef.WeeklySalesRecord_FridayCount, this._fri, 1);
        AddPropertyEntry(Sample1TableDef.WeeklySalesRecord_SaturdayCount, this._sat, 1);
        AddPropertyEntry(Sample1TableDef.WeeklySalesRecord_SundayCount, this._sun, 1);
        AddPropertyEntry(Sample1TableDef.WeeklySalesRecord_TotalCount, this._total, 1);
    }

    public override string GetStyleID(PropEntry entry, TableState state)
    {
        return Sample1TableDef.StyleID_Body_Default;
    }
}




2. Prepare table template configure file (.xml)


Here you will customize the report data table



<?xml version="1.0" encoding="utf-8" ?>
<TableTemplate>
  <Settings TableWidth="500" RelativeWidth="0.04 0.08 0.15 0.09 0.09 0.09 0.09 0.09 0.09 0.09 0.10" />
  <Header>
    <Property Name="Product No" Content="No" ColspanNum="1" RowspanNum="1" />
    <Property Name="Product Code" Content="Code" ColspanNum="1" RowspanNum="1" />
    <Property Name="Product Name" Content="Product" ColspanNum="1" RowspanNum="1" />
    <Property Name="Monday Count" Content="Mon" ColspanNum="1" RowspanNum="1" />
    <Property Name="Tuesday Count" Content="Tue" ColspanNum="1" RowspanNum="1" />
    <Property Name="Wednesday Count" Content="Wed" ColspanNum="1" RowspanNum="1" />
    <Property Name="Thursday Count" Content="Thu" ColspanNum="1" RowspanNum="1" />
    <Property Name="Friday Count" Content="Fri" ColspanNum="1" RowspanNum="1" />
    <Property Name="Saturday Count" Content="Sat" ColspanNum="1" RowspanNum="1" />
    <Property Name="Sunday Count" Content="Sun" ColspanNum="1" RowspanNum="1" />
    <Property Name="Total Count" Content="Total" ColspanNum="1" RowspanNum="1" />
  </Header>
  <Styles>
    <Style ID="BodyDefault">
      <FontFamily>Helvetica</FontFamily>
      <FontSize>10</FontSize>
      <!--FontStyle valid value: [Regular|Bold|Italic|BoldItalic]-->
      <FontStyle>Regular</FontStyle>
      <FontColor>#000000</FontColor>
      <BorderLineWidth>0.5</BorderLineWidth>
      <BackGroundColor>#FFFFFF</BackGroundColor>
      <HoriAlignment>Center</HoriAlignment>
      <VertAlignment>Middle</VertAlignment>
      <IsTopLineExit>True</IsTopLineExit>
      <IsBottomLineExit>True</IsBottomLineExit>
      <IsLeftLineExit>True</IsLeftLineExit>
      <IsRightLineExit>True</IsRightLineExit>
      <LineType>Solid</LineType>
    </Style>
    <Style ID="HeaderDefault">
      <FontFamily>Helvetica</FontFamily>
      <FontSize>12</FontSize>
      <!--FontStyle valid value: [Regular|Bold|Italic|BoldItalic]-->
      <FontStyle>Bold</FontStyle>
      <FontColor>#000000</FontColor>
      <BorderLineWidth>0.5</BorderLineWidth>
      <BackGroundColor>#C1CCE8</BackGroundColor>
      <HoriAlignment>Center</HoriAlignment>
      <VertAlignment>Middle</VertAlignment>
      <IsTopLineExit>True</IsTopLineExit>
      <IsBottomLineExit>True</IsBottomLineExit>
      <IsLeftLineExit>True</IsLeftLineExit>
      <IsRightLineExit>True</IsRightLineExit>
      <LineType>Solid</LineType>
    </Style>    
  </Styles>
</TableTemplate>




3. Prepare sample data


Here we have prepared the sample dataset. You can also load the real data from database in your C# ASP.NET, Windows application.



public class Sample1Database
{
public static String[] datas = new String[] {
    "1;000001;Apple;200;200;150;20;20;0;0;500",
    "2;000003;Banana;100;50;150;20;20;0;0;100",
    "3;000005;Dog;10;20;15;0;10;0;0;60",
    "4;000112;TV;10;20;15;5;5;0;0;30",
};

public static List<ValueEntry> CreateTableEntries()
{
    List<ValueEntry> values = new List<ValueEntry>();

    foreach (String data in datas)
    {
        String[] tmps = data.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
        if (tmps.Length < 11) continue;

        values.Add(new WeeklySalesRecord(tmps[0], tmps[1], tmps[2],
                                         tmps[3], tmps[4], tmps[5], tmps[6], tmps[7], tmps[8], tmps[9],
                                         tmps[10]));
    }

    return values;
}
}




4. Create document with the table


The C# source code below, you will learn how to create a new PDF report file from dataset, with report template applied.



String outputFilePath = Program.OutputFolder + "\\" + OutputFileName;

Document doc = new Document();
PDFBuildHandler.Create(doc, outputFilePath);

doc.Open();

Paragraph p = new Paragraph("Weekly Sales Report");
p.Alignment = Alignment.ALIGN_CENTER;
p.TextFont = new Font(Font.FontFamily.TimesRoman, 36F, Font.FontStyle.Bold, Color.Black);
p.SpacingAfter = 36;
doc.Add(p);

Paragraph p1 = new Paragraph("Sales Name/Store: ");
p1.TextFont = new Font(Font.FontFamily.Helvetica, 16F, Font.FontStyle.Regular, Color.Black);
doc.Add(p1);

Paragraph p2 = new Paragraph("Week Period: ");
p2.TextFont = new Font(Font.FontFamily.Helvetica, 16F, Font.FontStyle.Regular, Color.Black);
p2.SpacingAfter = 12;
doc.Add(p2);

addTable(doc);

doc.Close();
private static void addTable(Document doc)
{
    String xmlFilePath = Program.OutputFolder + "\\" + TableTemplateXMLFileName;
    TabelTemplate tableTemplate = TabelTemplate.LoadTemplate(xmlFilePath);
    tableTemplate.SetHeaderPropertyFunc(Sample1TableDef.GetHeaderType);

    doc.SetTableTemplate(tableTemplate);            

    List<ValueEntry> values = Sample1Database.CreateTableEntries();
    foreach (ValueEntry value in values)
    {
        doc.AddTableEntry(value);
    }

    doc.CloseCurrentTable();
}


Let's see the result of adding a table with template: