Create Download PDF using itextsharp MVC 5

Create and Download PDF using itextsharp MVC 5

How To Create Download PDF using itextsharp MVC 5?

//Create and Download PDF using itextsharp MVC 5
//how to convert html to pdf with image tags using itextsharp?
 private static byte[] ExportHTMLToPDF(string fileName, StringBuilder sb)
 {
    string imagepath ="http://localhost:53865//Images/pdf-logo.png";
    FileStream obj;
    fileName = Helper.CreateDirectory(Constant.UploadedFileFolder, fileName);

    obj = new FileStream(fileName, FileMode.Create);

    StringReader sr = new StringReader(sb.ToString());
    Document pdfDoc = new Document(PageSize.LETTER, 70, 55, 40, 25);

    try
    {
        PdfWriter writer = PdfWriter.GetInstance(pdfDoc, obj);
        HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
       
        //Open PDF
        pdfDoc.Open();

        Image image = Image.GetInstance(imagepath);
        image.Alignment = Image.ALIGN_TOP;
        pdfDoc.Add(image);

        htmlparser.Parse(sr);   

        //Close PDF
        pdfDoc.Close();           
    }
    catch(Exception ex){
        throw ex;    
    }

   return System.IO.File.ReadAllBytes(fileName);    
}

//Create Directory if not exists!
public static string CreateDirectory(string dirPath, string fileName)
{
    dirPath = System.Web.HttpContext.Current.Server.MapPath(dirPath);
    string filePath = dirPath + fileName;
    bool exists = System.IO.Directory.Exists(dirPath);
    if (!exists)
        System.IO.Directory.CreateDirectory(dirPath);

    return filePath;
}


AND
Add logo image for pdf file created by iTextSharp in c# -
//Add logo image for each page on pdf file created by iTextSharp in c#
string imagepath = @"\Images\pdf-logo.png";
Document pdfDoc = new Document(PageSize.A4, 20f, 20f, 10f, 20f);

try
{
    _events e = new _events();
    PdfWriter pw = PdfWriter.GetInstance(pdfDoc, new FileStream("myPDFFile.pdf", FileMode.Create));
    pw.PageEvent = e;

    HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
    PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
    pdfDoc.Open();

    Image image = Image.GetInstance(Server.MapPath(imagepath));
    image.Alignment = Image.ALIGN_LEFT;
    pdfDoc.Add(image);
    htmlparser.Parse(sr);

    //Close PDF
    pdfDoc.Close();
}


OR
//CREATE and Download PDF using itextsharp MVC 5
private void CreatePdf()
    {
        Response.ContentType = "application/pdf";
        Response.AddHeader("content-disposition", "attachment;filename="myPDFFileName.pdf");
        Response.Cache.SetCacheability(HttpCacheability.NoCache);

        StringWriter stringWriter = new StringWriter();
        HtmlTextWriter htmlTextWriter = new HtmlTextWriter(stringWriter);
        pnPrint.RenderControl(htmlTextWriter);

        StringReader stringReader = new StringReader(stringWriter.ToString());
        string logoImagePath = Server.MapPath(".") + "\\Img\\pdf-logo.png";
        Document pdfDocument = new Document(PageSize.A4, 20f, 20f, 10f, 20f);

        try
        {
            HTMLWorker htmlparser = new HTMLWorker(pdfDocument);
            PdfWriter.GetInstance(pdfDocument, Response.OutputStream);

            //Open PDF Document
            pdfDocument.Open();

            Image image = Image.GetInstance(logoImagePath);
            image.Alignment = Image.ALIGN_LEFT;

            //Add Image in the pdf Document
            pdfDocument.Add(image);

            //Parse stringReader
            htmlparser.Parse(stringReader);

             //Close PDF
             pdfDoc.Close();
        }
        catch (Exception ex)
        {
            throw ex;
        }
        finally
        {
            Response.End();
        }
    }

Stayed Informed MVC 4 vs. MVC 5 vs. MVC 6

I hope you are enjoying with this post! Please share with you friends. Thank you!!
ANIL SINGH

Anil Singh is an author, tech blogger, and software programmer. Book writing, tech blogging is something do extra and Anil love doing it. For more detail, kindly refer to this link..
My Blogs - https://code-sample.com and https://code-sample.xyz
My Book1 - BEST SELLING ANGULAR BOOK (INCLUDING ALL VERSIONS 2, 4, 5, 6, 7)
My Book2 - ANGULAR 2 INTERVIEW QUESTIONS BOOK - Both Books are Available on WorldWide.

www.code-sample.com/. Powered by Blogger.
^