andruhovski icon

Long Table in PDF

andruhovski | PRO | 12/09/17 07:14:41 PM UTC | 0 ⭐ | 578 👁️ | Never ⏰ | []
C# |

3.02 KB

|

None

|

0 👍

/

0 👎

using System;
using System.Collections.Generic;
using System.Data;
using System.Reflection;
using Aspose.Pdf;
 
 
namespace PdfLongTableDemo
{
    public static class PdfDemoExtensions
    {
        public static DataTable ToDataTable<TSource>(this IList<TSource> data)
        {
            DataTable dataTable = new DataTable(typeof(TSource).Name);
            PropertyInfo[] props = typeof(TSource).GetProperties(BindingFlags.Public | BindingFlags.Instance);
            foreach (PropertyInfo prop in props)
            {
                dataTable.Columns.Add(prop.Name, Nullable.GetUnderlyingType(prop.PropertyType) ??
                    prop.PropertyType);
            }
 
            foreach (TSource item in data)
            {
                var values = new object[props.Length];
                for (int i = 0; i < props.Length; i++)
                {
                    values[i] = props[i].GetValue(item, null);
                }
                dataTable.Rows.Add(values);
            }
            return dataTable;
        }
    }
    //RentItem is a class for demo purposes
    public class RentItem
    {
        public int Id { get; set; }
        public string ClientName { get; set; }
        public string Address { get; set; }
        public int LeaseTerm { get; set; }
        public decimal MonthlyPayment { get; set; }
    };
 
    class Program
    {
        static void Main(string[] args)
        {
 
            var rentItems = new List<RentItem>();
            for (int i = 0; i < 1000; i++)
            {
                rentItems.Add(new RentItem
                {
                    Id = i,
                    ClientName = Faker.Name.FullName(Faker.NameFormats.Standard),
                    Address = Faker.Address.StreetAddress(),
                    LeaseTerm = Faker.RandomNumber.Next(100),
                    MonthlyPayment = (Faker.RandomNumber.Next(3000) + 1000) / 10m
                });
            }
 
 
            Document doc = new Document();
            doc.Pages.Add();
 
            // Initializes a new instance of the Table
            Table table = new Table();
 
            // Set column widths of the table
            table.ColumnWidths = "30 100 120 60 100";
 
            // Set the table border color as LightGray
            table.Border = new BorderInfo(BorderSide.All, .5f, Aspose.Pdf.Color.LightGray);
 
            // Set the border for table cells
            table.DefaultCellBorder = new Aspose.Pdf.BorderInfo(Aspose.Pdf.BorderSide.All, .5f, Aspose.Pdf.Color.LightGray);
            table.ImportDataTable(rentItems.ToDataTable(), true, 0, 0);
 
            //Repeat Header
            table.RepeatingRowsCount = 1;
 
            // Add table object to first page of input document
            doc.Pages[1].Paragraphs.Add(table);
            string dataDir = @"long_table_example.pdf";
 
            // Save updated document containing table object
            doc.Save(dataDir);
            Console.WriteLine("Complete!");
        }
    }
}

Comments