Generate Invoice (Bill) Receipt with Image PDF from database in ASP.Net using C# and VB.Net

Last Reply 5 months ago By pandeyism

Posted 5 months ago

I am generating an pdf invoice by passing table in string builder but dont know how to insert image in the invoice can somebody please help

Posted 5 months ago

Hi Sadakat,

Refer below sample.

HTML

 <asp:Button ID="Button1" Text="Generate Invoice" OnClick="GenerateInvoicePDF" runat="server" />

Namespaces

C#

using System.IO;
using System.Text;
using iTextSharp.text;
using iTextSharp.text.pdf;
using iTextSharp.text.html.simpleparser;

VB.Net

Imports System.IO
Imports iTextSharp.text.html.simpleparser
Imports iTextSharp.text.pdf
Imports iTextSharp.text

Code

 C#

protected void GenerateInvoicePDF(object sender, EventArgs e)
{
    string path = Server.MapPath("~/File/2.png");
    StringBuilder sb = new StringBuilder();
    sb.Append("<div align='center';>");
    sb.Append(@"<img src='" + path + "' height='50px' width='50px' />");        
    sb.Append("<h2>AL-MADANI INTERNATIONAL</h2><table class='table table-bordered'><tr><td colspan='3'>Invoice No.</td><td colspan='3'>Invoice Date:</td></tr><tr><td colspan='3'>Tourist Name:</td><td colspan='3'>Total Passengers:</td></tr><tr><td colspan='3'>City:</td><td colspan='3'>District:</td></tr><tr><td colspan='3'>State:</td><td colspan='3'>Post Code:</td></tr><tr><td colspan='3'>Payment Mode:</td><td colspan='3'>Payment Date:</td><tr><tr><td colspan='3'>Payment Details*:</td><td colspan='3'>Journey Date:</td><tr><th colspan='6'>DETAILS OF RECEIVER - BILLED TO</th><tr><td colspan='6'>Name:</td></tr><tr><td colspan='6'>Address:</td></tr></tr><tr><th colspan='3'>Description</th><th>Rate</th><th>Total Passenger</th><th>Total Amount</th></tr><tr><td colspan='3'>ECONOMY PACKAGE FOR UMRAH ON OCTOBER 2019</td><td>40000</td><td>2</td><td>80000</td></tr><tr><td colspan='3'>PAID AMOUNT = </td><td></td><td></td><td></td></tr><tr><tr><td colspan='3'>BALANCE AMOUNT = </td><td></td><td></td><td></td></tr><tr><th colspan='6'>TOTAL</tr><tr></th><th colspan='3' rowspan='5'>Amount in words</th><th colspan='2'>Total Amount</th><td>80000</td><tr><th colspan='2'>.</th><td></td></tr><tr><th colspan='2'>.</th><td></td></tr><tr><th colspan='2'>.</th><td></td></tr><tr><th colspan='2'>Grand Total</th><td>80000</td></tr></table>");
    sb.Append("</div>");
    StringReader sr = new StringReader(sb.ToString()); 
    Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 10f);
    HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
    PdfWriter writer = PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
    pdfDoc.Open();
    htmlparser.Parse(sr);
    pdfDoc.Close();
    Response.ContentType = "application/pdf";
    Response.AddHeader("content-disposition", "attachment;filename=Invoice_" + "invoice" + ".pdf");
    Response.Cache.SetCacheability(HttpCacheability.NoCache);
    Response.Write(pdfDoc);
    Response.End();
}

VB.Net

Protected Sub GenerateInvoicePDF(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
    Dim path As String = Server.MapPath("~/File/2.png")
    Dim sb As StringBuilder = New StringBuilder()
    sb.Append("<div align='center';>")
    sb.Append("<img src='" & path & "' height='50px' width='50px' />")
    sb.Append("<h2>AL-MADANI INTERNATIONAL</h2><table class='table table-bordered'><tr><td colspan='3'>Invoice No.</td><td colspan='3'>Invoice Date:</td></tr><tr><td colspan='3'>Tourist Name:</td><td colspan='3'>Total Passengers:</td></tr><tr><td colspan='3'>City:</td><td colspan='3'>District:</td></tr><tr><td colspan='3'>State:</td><td colspan='3'>Post Code:</td></tr><tr><td colspan='3'>Payment Mode:</td><td colspan='3'>Payment Date:</td><tr><tr><td colspan='3'>Payment Details*:</td><td colspan='3'>Journey Date:</td><tr><th colspan='6'>DETAILS OF RECEIVER - BILLED TO</th><tr><td colspan='6'>Name:</td></tr><tr><td colspan='6'>Address:</td></tr></tr><tr><th colspan='3'>Description</th><th>Rate</th><th>Total Passenger</th><th>Total Amount</th></tr><tr><td colspan='3'>ECONOMY PACKAGE FOR UMRAH ON OCTOBER 2019</td><td>40000</td><td>2</td><td>80000</td></tr><tr><td colspan='3'>PAID AMOUNT = </td><td></td><td></td><td></td></tr><tr><tr><td colspan='3'>BALANCE AMOUNT = </td><td></td><td></td><td></td></tr><tr><th colspan='6'>TOTAL</tr><tr></th><th colspan='3' rowspan='5'>Amount in words</th><th colspan='2'>Total Amount</th><td>80000</td><tr><th colspan='2'>.</th><td></td></tr><tr><th colspan='2'>.</th><td></td></tr><tr><th colspan='2'>.</th><td></td></tr><tr><th colspan='2'>Grand Total</th><td>80000</td></tr></table>")
    sb.Append("</div>")
    Dim sr As StringReader = New StringReader(sb.ToString())
    Dim pdfDoc As Document = New Document(PageSize.A4, 10.0F, 10.0F, 10.0F, 10.0F)
    Dim htmlparser As HTMLWorker = New HTMLWorker(pdfDoc)
    Dim writer As PdfWriter = PdfWriter.GetInstance(pdfDoc, Response.OutputStream)
    pdfDoc.Open()
    htmlparser.Parse(sr)
    pdfDoc.Close()
    Response.ContentType = "application/pdf"
    Response.AddHeader("content-disposition", "attachment;filename=Invoice_" & "invoice" & ".pdf")
    Response.Cache.SetCacheability(HttpCacheability.NoCache)
    Response.Write(pdfDoc)
    Response.[End]()
End Sub

Screenshot