python ruby-programming-language Smalltalk Vue.js Knockoutjs TypeScript JavaScript, OOPs JavaScript Angular 8,7,6,5,4 and 2 Interview Questions and Answers Angular 8,7,6,5,4 and 2 Interview Questions and Answers Angular 4 Angular 2 AngularJs 1.x NodeJs Perl Programming R Programming RequireJs Rust Programming Backbonejs closure programming language go-programming-language kotlin-programming-language
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

Hi there! Welcome to my blog. My name is Anil Singh. I'm a author of this blog. For more detail about me, kindly refer to this link..
My Blogs - http://code-sample.com and http://code-sample.xyz
My Books - Interview Questions and Answers Books- Get Your Book in 15+ Digital Stores Worldwide..

You Might Also Like
www.code-sample.com/. Powered by Blogger.
ASK Questions