Generating PDF file (invoice) using C#
Tags: csharp, pdf, aspose, apitron
Published: 2018-10-30 13:15:42

---
title: Generating PDF file (invoice) using C#
published: true
description: Generating PDF document (invoice) using C#
tags: #csharp #pdf #aspose #apitron
---

In the my first post, I want to share some of my experience generating PDF documents using Apitron.PDF.Kit for .NET and introduce basic PDF creation techniques. As an example, I chose a frequently used document - an invoice. 

![Invoice](https://thepracticaldev.s3.amazonaws.com/i/ggs7sgiwv98ztmxfyg04.png)

###**The code**

I won’t dive into platform specifics here and focus only on PDF generation and related code. 

A few functions responsible for PDF generation are listed below:



#####private void GenerateInvoice(Stream stream)

    string imagesPath = @"..\..\images";
    FlowDocument document = new FlowDocument();

    document.StyleManager.RegisterStyle("gridrow.tableHeader",new Style() {Background = RgbColors.LightSlateGray});
    document.StyleManager.RegisterStyle("gridrow.centerAlignedCells > *,gridrow > *.centerAlignedCell",new Style() {Align = Align.Center, Margin = new Thickness(0)});
    document.StyleManager.RegisterStyle("gridrow > *.leftAlignedCell",new Style() {Align = Align.Left, Margin = new Thickness(5, 0, 0, 0)});
    document.StyleManager.RegisterStyle("gridrow > *",new Style() {Align = Align.Right, Margin = new Thickness(0, 0, 5, 0)});

    ResourceManager resourceManager = new ResourceManager();
    resourceManager.RegisterResource(
        new Apitron.PDF.Kit.FixedLayout.Resources.XObjects.Image("logo",
        Path.Combine(imagesPath, "storeLogo.png"), true) {Interpolate = true});

    resourceManager.RegisterResource(new Apitron.PDF.Kit.FixedLayout.Resources.XObjects.Image("stamp",Path.Combine(imagesPath, "stamp.png"), true) {Interpolate = true});
    document.PageHeader.Margin = new Thickness(0, 40, 0, 20);
    document.PageHeader.Padding = new Thickness(10, 0, 10, 0);
    document.PageHeader.Height = 120;
    document.PageHeader.Background = RgbColors.LightGray;
    document.PageHeader.LineHeight = 60;
    document.PageHeader.Add(new Image("logo"){Height = 50, Width = 50, VerticalAlign = VerticalAlign.Middle});
    document.PageHeader.Add(new TextBlock("Invoice")
                            {
                                Display = Display.InlineBlock,
                                Align = Align.Right,
                                Font = new Font(StandardFonts.CourierBold, 20),
                                Color = RgbColors.Black
                            });
    Section pageSection = new Section() {Padding = new Thickness(20)};
    pageSection.AddItems(CreateInfoSubsections(new string[] {txtCompany.Text,"Bill to:\r\n" + txtCustomerInfo.Text}));

    pageSection.Add(new Hr() {Padding = new Thickness(0, 20, 0, 20)});
    pageSection.Add(CreateProductsGrid());
    pageSection.Add(new Br {Height = 20});
    pageSection.Add(new Section() {Width = 250, Display = Display.InlineBlock});
    pageSection.Add(new Image("stamp"));

    document.Add(pageSection);
    document.Write(stream, resourceManager, new PageBoundary(Boundaries.A4));




#####private Grid CreateProductsGrid()
    Grid productsGrid = new Grid(20, Length.Auto, 30, 50, 55, 60);
    productsGrid.Add(new GridRow(new TextBlock("#"), new TextBlock("Product"), new TextBlock("Qty."), new TextBlock("Price"), new TextBlock("Disc.(%)"), new TextBlock("Total")) Class = "tableHeader centerAlignedCells"});Decimal invoiceTotal = 0;

    
    foreach (ProductEntry product in products)
    {
        TextBlock pos = new TextBlock(product.Pos.ToString()) {Class = "centerAlignedCell"};
        TextBlock description = new TextBlock(product.Description) {Class = "leftAlignedCell"};
        TextBlock qty = new TextBlock(product.Qty.ToString()) {Class = "centerAlignedCell"};
        TextBlock price = new TextBlock( product.Price.ToString(CultureInfo.InvariantCulture));
        TextBlock discount = new TextBlock(product.Discount.ToString(CultureInfo.InvariantCulture));
        TextBlock total = new TextBlock(product.Total.ToString(CultureInfo.InvariantCulture));
        productsGrid.Add(new GridRow(pos, description, qty, price, discount, total));
        invoiceTotal += product.Total;
    }

    productsGrid.Add(new GridRow(new TextBlock("Total(USD)") {ColSpan = 4}, new TextBlock(invoiceTotal.ToString(CultureInfo.InvariantCulture)) {ColSpan = 2}));

    return productsGrid;