<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Apitron</title>
    <description>The latest articles on DEV Community by Apitron (@apitron).</description>
    <link>https://dev.to/apitron</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F111100%2Fe3f37e56-1316-4b94-a754-9e41d4e1dbad.png</url>
      <title>DEV Community: Apitron</title>
      <link>https://dev.to/apitron</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/apitron"/>
    <language>en</language>
    <item>
      <title>Generating PDF file (invoice) using C#</title>
      <dc:creator>Apitron</dc:creator>
      <pubDate>Tue, 30 Oct 2018 13:15:42 +0000</pubDate>
      <link>https://dev.to/apitron/generating-pdf-file-invoice-using-c-347i</link>
      <guid>https://dev.to/apitron/generating-pdf-file-invoice-using-c-347i</guid>
      <description>&lt;p&gt;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. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fh67ftr5pcmo5iredow31.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fh67ftr5pcmo5iredow31.png" alt="Invoice"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;The code&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;I won’t dive into platform specifics here and focus only on PDF generation and related code. &lt;/p&gt;

&lt;p&gt;A few functions responsible for PDF generation are listed below:&lt;/p&gt;

&lt;h5&gt;
  
  
  private void GenerateInvoice(Stream stream)
&lt;/h5&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;string imagesPath = @"..\..\images";
FlowDocument document = new FlowDocument();

document.StyleManager.RegisterStyle("gridrow.tableHeader",new Style() {Background = RgbColors.LightSlateGray});
document.StyleManager.RegisterStyle("gridrow.centerAlignedCells &amp;gt; *,gridrow &amp;gt; *.centerAlignedCell",new Style() {Align = Align.Center, Margin = new Thickness(0)});
document.StyleManager.RegisterStyle("gridrow &amp;gt; *.leftAlignedCell",new Style() {Align = Align.Left, Margin = new Thickness(5, 0, 0, 0)});
document.StyleManager.RegisterStyle("gridrow &amp;gt; *",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));
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h5&gt;
  
  
  private Grid CreateProductsGrid()
&lt;/h5&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;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;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

</description>
      <category>csharp</category>
      <category>pdf</category>
      <category>aspose</category>
      <category>apitron</category>
    </item>
  </channel>
</rss>
