ByteScout PDF SDK - C# - Create Invoice Spanning Multiple Page - ByteScout

ByteScout PDF SDK – C# – Create Invoice Spanning Multiple Page

  • Home
  • /
  • Articles
  • /
  • ByteScout PDF SDK – C# – Create Invoice Spanning Multiple Page

How to create invoice spanning multiple page in C# and ByteScout PDF SDK

Tutorial on how to create invoice spanning multiple page in C#

These source code samples are listed and grouped by their programming language and functions they use. What is ByteScout PDF SDK? It is the SDK for pdf documents generation, modification and updates. Can also generate and fill PDF forms. Provides support for text (fonts, style, size, font family), layers, pdf form fields, vector and raster drawings. It can help you to create invoice spanning multiple page in your C# application.

Fast application programming interfaces of ByteScout PDF SDK for C# plus the instruction and the code below will help you quickly learn how to create invoice spanning multiple page. In order to implement the functionality, you should copy and paste this code for C# below into your code editor with your app, compile and run your application. Test C# sample code examples whether they respond your needs and requirements for the project.

Trial version of ByteScout PDF SDK can be downloaded for free from our website. It also includes source code samples for C# and other programming languages.

On-demand (REST Web API) version:
 Web API (on-demand version)

On-premise offline SDK for Windows:
 60 Day Free Trial (on-premise)

Program.cs
      
using System; using System.Diagnostics; using Bytescout.PDF; namespace Tables { /// <summary> /// This example demonstrates how to create invoice which can span multiple pages. /// </summary> class Program { #region Global Declarations const int HeightConsumedByTableRow = 20; static int ContentHeightStart = 100; static int ContentHeightEnd = 700; static int HeightUtilized = 100; static int _RowCounter = 1; static Document pdfDocument; static Page page; #endregion static void Main() { #region Declarations and Implementations SolidPen solidPen = new SolidPen(); Brush blackBrush = new SolidBrush(new ColorRGB(0, 0, 0)); Font headerFont = new Font(StandardFonts.TimesBold, 24); Font headerFont2 = new Font(StandardFonts.TimesBold, 16); Font contentFont_12 = new Font(StandardFonts.TimesBold, 12); Font contentFont_10 = new Font(StandardFonts.Times, 10); // Create new document pdfDocument = new Document(); pdfDocument.RegistrationName = "demo"; pdfDocument.RegistrationKey = "demo"; #endregion // Add new page AddNewPage(); /* 1. Add Content */ page.Canvas.DrawString("COMPANY NAME", contentFont_12, blackBrush, 50, (HeightUtilized += 20)); page.Canvas.DrawString("Address", contentFont_10, blackBrush, 50, (HeightUtilized += 20)); page.Canvas.DrawString("DATE", contentFont_10, blackBrush, 400, HeightUtilized); page.Canvas.DrawString("Phone, fax", contentFont_10, blackBrush, 50, (HeightUtilized += 20)); page.Canvas.DrawString("INVOICE #", contentFont_10, blackBrush, 400, HeightUtilized); page.Canvas.DrawString("E-mail", contentFont_10, blackBrush, 50, (HeightUtilized += 20)); page.Canvas.DrawString("FOR", contentFont_10, blackBrush, 400, HeightUtilized); // Add some vertical space HeightUtilized += 50; /* 2. Add Table which can span multipage */ Console.WriteLine("Enter the number of rows to add to the table:"); int numberOfRows = Convert.ToInt32(Console.ReadLine()); DrawTable(NumberOfRows: numberOfRows); /* 3. Add Signature */ // Check new page needed for adding signature if (CheckNewPageNeeded(70)) { AddNewPage(); } page.Canvas.DrawString("Signature", contentFont_12, blackBrush, 400, (HeightUtilized + 57)); page.Canvas.DrawLine(solidPen, 450, (HeightUtilized + 70), 570, (HeightUtilized + 70)); /* 4. Add Header And Footer*/ for (int pageIndex = 0; pageIndex < pdfDocument.Pages.Count; pageIndex++) { var curPage = pdfDocument.Pages[pageIndex]; // Add Logo Image imageLogo = new Image("logo.png"); curPage.Canvas.DrawImage(imageLogo, 20, 45, imageLogo.Width / 2, imageLogo.Height / 2); // Add requisites curPage.Canvas.DrawString("INVOICE", headerFont, blackBrush, 450, 55); // Add Page Number curPage.Canvas.DrawLine(solidPen, 20, 750, 590, 750); curPage.Canvas.DrawString({code}quot;Page: {pageIndex + 1} of {pdfDocument.Pages.Count}", contentFont_10, blackBrush, 30, 760); } // Save document to file pdfDocument.Save("result.pdf"); // Cleanup pdfDocument.Dispose(); // Open result document in default associated application (for demo purpose) ProcessStartInfo processStartInfo = new ProcessStartInfo("result.pdf"); processStartInfo.UseShellExecute = true; Process.Start(processStartInfo); } /// <summary> /// Checks whether new page is needed /// </summary> /// <param name="expectedHeight"></param> /// <returns></returns> static bool CheckNewPageNeeded(int expectedHeight) { return (HeightUtilized + expectedHeight) > ContentHeightEnd; } /// <summary> /// Adds new page, and also set height utilized /// </summary> static void AddNewPage() { // Add page page = new Page(PaperFormat.Letter, PaperOrientation.Portrait); pdfDocument.Pages.Add(page); // Reset Height Utilized HeightUtilized = ContentHeightStart; } /// <summary> /// Draw table /// </summary> /// <param name="NumberOfRows"></param> static void DrawTable(int NumberOfRows) { while (NumberOfRows > 0) { // Get number of rows can be added in current page var NumberOfRowsCanBeAddedInCurrentPage = GetNumberOfRowsCanBeAddedInCurrentPage(); if (NumberOfRowsCanBeAddedInCurrentPage > 0) { var table = GetTable(Math.Min(NumberOfRows, NumberOfRowsCanBeAddedInCurrentPage)); page.Canvas.DrawTable(table, 50, HeightUtilized); // Update Height Utilized HeightUtilized += (Math.Min(NumberOfRows, NumberOfRowsCanBeAddedInCurrentPage) * HeightConsumedByTableRow); NumberOfRows -= Math.Min(NumberOfRows, NumberOfRowsCanBeAddedInCurrentPage); } else { // Add new page AddNewPage(); } } } /// <summary> /// Get Table Header with Content /// </summary> /// <param name="NumberOfRows"></param> /// <returns></returns> private static Table GetTable(int NumberOfRows) { Font fontTableHeader = new Font(StandardFonts.TimesBold, 11); Font fontTableRow = new Font(StandardFonts.Times, 10); Table table = new Table(); /* 1. Add Table Header */ table.Columns.Add(new TableColumn("Description", "Description")); table.Columns[0].Width = 220; table.Columns[0].Font = fontTableHeader; table.Columns[0].TextFormat.HorizontalAlign = HorizontalAlign.Left; table.Columns.Add(new TableColumn("Quantity", "Quantity")); table.Columns[1].Width = 80; table.Columns[1].Font = fontTableHeader; table.Columns[1].TextFormat.HorizontalAlign = HorizontalAlign.Right; table.Columns.Add(new TableColumn("Price", "Price")); table.Columns[2].Width = 100; table.Columns[2].Font = fontTableHeader; table.Columns[2].TextFormat.HorizontalAlign = HorizontalAlign.Right; table.Columns.Add(new TableColumn("Amount", "Amount")); table.Columns[3].Width = 120; table.Columns[3].Font = fontTableHeader; table.Columns[3].TextFormat.HorizontalAlign = HorizontalAlign.Right; /* 2. Add Table Content */ for (int index = 0; index < NumberOfRows; index++) { // Create new row and set its background color TableRow row = table.NewRow(); // Set row header text row["Description"].Text = {code}quot;Description for item - {_RowCounter++}"; row["Description"].Font = fontTableRow; row["Description"].TextFormat.HorizontalAlign = HorizontalAlign.Left; row["Quantity"].Text = "###"; row["Quantity"].Font = fontTableRow; row["Quantity"].TextFormat.HorizontalAlign = HorizontalAlign.Right; row["Price"].Text = "$.
<!-- code block begin -->
    <h5>
      <strong>{codeFileName}</strong>
    </h5>
    
    <pre>
      <div class="code-editor {codeExtension}">{code}</div>
    </pre>

    <h3>ON-PREMISE OFFLINE SDK</h3>

    <a class="btn btn-lg btn-primary" href="https://bytescout.com/download/web-installer">60 Day Free Trial</a> or <a class="btn btn-lg btn-primary" href="https://bytescout.com/products/developer/pdfsdk/index.html">Visit ByteScout PDF SDK Home Page</a> 
    <br/>
    <a class="btn btn-lg btn-primary" href="https://bytescout.com/documentation/index.html">Explore ByteScout PDF SDK Documentation</a>
    <br/>
    <a class="btn btn-lg btn-primary" href="https://github.com/bytescout/ByteScout-SDK-SourceCode/">Explore Samples</a>
    <br/>
    <a class="btn btn-lg btn-primary" href="https://academy.bytescout.com">Sign Up for ByteScout PDF SDK Online Training</a>
    
    
    <h3>ON-DEMAND REST WEB API</h3>
    
    <a class="btn btn-lg btn-primary" href="https://app.pdf.co/signup">Get Your API Key</a> 
    <br/>
    <a class="btn btn-lg btn-primary" href="https://pdf.co/documentation/api">Explore Web API Docs</a>
    <br/>
    <a class="btn btn-lg btn-primary" href="https://github.com/bytescout/ByteScout-SDK-SourceCode/tree/master/PDF.co%20Web%20API/">Explore Web API Samples</a>    
    
    <br/>

<!-- code block end -->    

quot;;
row["Price"].Font = fontTableRow;
row["Price"].TextFormat.HorizontalAlign = HorizontalAlign.Right;

row["Amount"].Text = "$.

<!-- code block begin -->
    <h5>
      <strong>{codeFileName}</strong>
    </h5>
    
    <pre>
      <div class="code-editor {codeExtension}">{code}</div>
    </pre>

    <h3>ON-PREMISE OFFLINE SDK</h3>

    <a class="btn btn-lg btn-primary" href="https://bytescout.com/download/web-installer">60 Day Free Trial</a> or <a class="btn btn-lg btn-primary" href="https://bytescout.com/products/developer/pdfsdk/index.html">Visit ByteScout PDF SDK Home Page</a> 
    <br/>
    <a class="btn btn-lg btn-primary" href="https://bytescout.com/documentation/index.html">Explore ByteScout PDF SDK Documentation</a>
    <br/>
    <a class="btn btn-lg btn-primary" href="https://github.com/bytescout/ByteScout-SDK-SourceCode/">Explore Samples</a>
    <br/>
    <a class="btn btn-lg btn-primary" href="https://academy.bytescout.com">Sign Up for ByteScout PDF SDK Online Training</a>
    
    
    <h3>ON-DEMAND REST WEB API</h3>
    
    <a class="btn btn-lg btn-primary" href="https://app.pdf.co/signup">Get Your API Key</a> 
    <br/>
    <a class="btn btn-lg btn-primary" href="https://pdf.co/documentation/api">Explore Web API Docs</a>
    <br/>
    <a class="btn btn-lg btn-primary" href="https://github.com/bytescout/ByteScout-SDK-SourceCode/tree/master/PDF.co%20Web%20API/">Explore Web API Samples</a>    
    
    <br/>

<!-- code block end -->    

quot;;
row["Amount"].Font = fontTableRow;
row["Amount"].TextFormat.HorizontalAlign = HorizontalAlign.Right;

// Add the row to the table
table.Rows.Add(row);
}

return table;
}

/// <summary>
/// Get number of rows can be added in current page
/// </summary>
/// <returns></returns>
private static int GetNumberOfRowsCanBeAddedInCurrentPage()
{
return (ContentHeightEnd - HeightUtilized) / HeightConsumedByTableRow;
}

}
}

ON-PREMISE OFFLINE SDK

60 Day Free Trial or Visit ByteScout PDF SDK Home Page

Explore ByteScout PDF SDK Documentation

Explore Samples

Sign Up for ByteScout PDF SDK Online Training

ON-DEMAND REST WEB API

Get Your API Key

Explore Web API Docs

Explore Web API Samples

VIDEO

ON-PREMISE OFFLINE SDK

60 Day Free Trial or Visit ByteScout PDF SDK Home Page

Explore ByteScout PDF SDK Documentation

Explore Samples

Sign Up for ByteScout PDF SDK Online Training

ON-DEMAND REST WEB API

Get Your API Key

Explore Web API Docs

Explore Web API Samples

Tutorials:

prev
next