using System; using System.Data; using System.IO; using System.Linq; using Aspose.Pdf; using Aspose.Pdf.Drawing; using Aspose.Pdf.Text; using TextState=Aspose.Pdf.Text.TextState; namespace ExampleConsole { class Program { private static string _cAsposeTemplatePdf = @"c:\aspose\template.pdf"; private static string _cAsposeDocumentPdf = @"c:\aspose\document.pdf"; private static DataTable _productsDataTable; static void Main() { _productsDataTable = GenerateDataTable(); AddInvoiceData(); } private static DataTable GenerateDataTable() { var dt = new DataTable("InovoiceItems"); dt.Columns.Add("ID", typeof(int)); dt.Columns.Add("ItemName", typeof(string)); dt.Columns.Add("Price", typeof(float)); dt.Columns.Add("Qunatity", typeof(int)); dt.Columns.Add("Summa", typeof(float)); // Add 5 rows into the DataTable object programmatically var dr = dt.NewRow(); for (var i = 0; i < 5; i++) { dr[0] = i+1; dr[1] = $"Sun Glasses Type {i+1}"; dr[2] = 199.99; dr[3] = 1; dr[4] = 199.99; dt.Rows.Add(dr); dr = dt.NewRow(); } return dt; } private static void AddInvoiceData() { //var tenants = _dbContext.Tenants.ToList(); if (!File.Exists(_cAsposeTemplatePdf)) GenerateLetterHead(); // Create new a PDF document var document = new Document(_cAsposeTemplatePdf) { PageInfo = new PageInfo { Margin = new MarginInfo(28, 28, 28, 42) } }; //----------------------------------- // Create TextAbsorber object to find all instances var textFragmentAbsorber = new TextFragmentAbsorber(); // Accept the absorber for the page index 1 document.Pages[1].Accept(textFragmentAbsorber); // Get the extracted text fragments var textFragmentCollection = textFragmentAbsorber.TextFragments; var m1= textFragmentCollection.Cast().Max(fragment => (float)fragment.Rectangle.URY); var m2 = textFragmentCollection.Cast().Min(fragment => (float)fragment.Rectangle.LLY); var height = m1-m2; //----------------------------------- var pdfPage = document.Pages[1]; // Create Graph object Aspose.Pdf.Drawing.Graph canvas = new Aspose.Pdf.Drawing.Graph(100, height); // Add graph object to paragraphs collection of page object pdfPage.Paragraphs.Add(canvas); // Set value to not change position for graph object canvas.IsChangePosition = true; // Initializes a new instance of the Table var table = new Table { // Set column auto widths of the table ColumnWidths = "10 10 10 10 10", ColumnAdjustment = ColumnAdjustment.AutoFitToContent, // Set cell padding DefaultCellPadding = new MarginInfo(5, 5, 5, 5), // Set the table border color as Black Border = new BorderInfo(BorderSide.All, .5f, Color.Black), // Set the border for table cells as Black DefaultCellBorder = new BorderInfo(BorderSide.All, .2f, Color.Black), DefaultCellTextState = new TextState("TimesNewRoman", 10) }; var paymentFormat = new TextState("TimesNewRoman", 10) { HorizontalAlignment = HorizontalAlignment.Right }; table.SetColumnTextState(2, paymentFormat); table.SetColumnTextState(4, paymentFormat); table.ImportDataTable(_productsDataTable,true,0,1,5,5); //Repeat Header table.RepeatingRowsCount = 1; // Add table object to first page of input document pdfPage.Paragraphs.Add(table); using (var streamOut = new FileStream(_cAsposeDocumentPdf, FileMode.OpenOrCreate)) { document.Save(streamOut); } } private static void GenerateLetterHead() { // Create new a PDF document var document = new Document() { PageInfo = new PageInfo { Margin = new MarginInfo(28, 28, 28, 28) } }; var pdfPage = document.Pages.Add(); // Initializes a new instance of the TextFragment for report's title or other text var textFragment = new Aspose.Pdf.Text.TextFragment("Invoice #123 or other text."); // Set text properties textFragment.TextState.FontSize = 12; textFragment.TextState.Font = Aspose.Pdf.Text.FontRepository.FindFont("TimesNewRoman"); textFragment.TextState.FontStyle = Aspose.Pdf.Text.FontStyles.Bold; // Add text fragment object to first page of input document pdfPage.Paragraphs.Add(textFragment); pdfPage.Paragraphs.Add(textFragment); pdfPage.Paragraphs.Add(textFragment); using (var streamOut = new FileStream(_cAsposeTemplatePdf, FileMode.OpenOrCreate)) { document.Save(streamOut); } } } }