Advertisement
andruhovski

Long Table in PDF

Dec 9th, 2017
226
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.02 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Data;
  4. using System.Reflection;
  5. using Aspose.Pdf;
  6.  
  7.  
  8. namespace PdfLongTableDemo
  9. {
  10.     public static class PdfDemoExtensions
  11.     {
  12.         public static DataTable ToDataTable<TSource>(this IList<TSource> data)
  13.         {
  14.             DataTable dataTable = new DataTable(typeof(TSource).Name);
  15.             PropertyInfo[] props = typeof(TSource).GetProperties(BindingFlags.Public | BindingFlags.Instance);
  16.             foreach (PropertyInfo prop in props)
  17.             {
  18.                 dataTable.Columns.Add(prop.Name, Nullable.GetUnderlyingType(prop.PropertyType) ??
  19.                     prop.PropertyType);
  20.             }
  21.  
  22.             foreach (TSource item in data)
  23.             {
  24.                 var values = new object[props.Length];
  25.                 for (int i = 0; i < props.Length; i++)
  26.                 {
  27.                     values[i] = props[i].GetValue(item, null);
  28.                 }
  29.                 dataTable.Rows.Add(values);
  30.             }
  31.             return dataTable;
  32.         }
  33.     }
  34.     //RentItem is a class for demo purposes
  35.     public class RentItem
  36.     {
  37.         public int Id { get; set; }
  38.         public string ClientName { get; set; }
  39.         public string Address { get; set; }
  40.         public int LeaseTerm { get; set; }
  41.         public decimal MonthlyPayment { get; set; }
  42.     };
  43.  
  44.     class Program
  45.     {
  46.         static void Main(string[] args)
  47.         {
  48.  
  49.             var rentItems = new List<RentItem>();
  50.             for (int i = 0; i < 1000; i++)
  51.             {
  52.                 rentItems.Add(new RentItem
  53.                 {
  54.                     Id = i,
  55.                     ClientName = Faker.Name.FullName(Faker.NameFormats.Standard),
  56.                     Address = Faker.Address.StreetAddress(),
  57.                     LeaseTerm = Faker.RandomNumber.Next(100),
  58.                     MonthlyPayment = (Faker.RandomNumber.Next(3000) + 1000) / 10m
  59.                 });
  60.             }
  61.  
  62.  
  63.             Document doc = new Document();
  64.             doc.Pages.Add();
  65.  
  66.             // Initializes a new instance of the Table
  67.             Table table = new Table();
  68.  
  69.             // Set column widths of the table
  70.             table.ColumnWidths = "30 100 120 60 100";
  71.  
  72.             // Set the table border color as LightGray
  73.             table.Border = new BorderInfo(BorderSide.All, .5f, Aspose.Pdf.Color.LightGray);
  74.  
  75.             // Set the border for table cells
  76.             table.DefaultCellBorder = new Aspose.Pdf.BorderInfo(Aspose.Pdf.BorderSide.All, .5f, Aspose.Pdf.Color.LightGray);
  77.             table.ImportDataTable(rentItems.ToDataTable(), true, 0, 0);
  78.  
  79.             //Repeat Header
  80.             table.RepeatingRowsCount = 1;
  81.  
  82.             // Add table object to first page of input document
  83.             doc.Pages[1].Paragraphs.Add(table);
  84.             string dataDir = @"long_table_example.pdf";
  85.  
  86.             // Save updated document containing table object
  87.             doc.Save(dataDir);
  88.             Console.WriteLine("Complete!");
  89.         }
  90.     }
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement