Bhavin Bhaskaran
Bhavin Bhaskaran

Reputation: 582

create multiple page pdf from asp.net mvc?

I was trying to create a pdf dynamically and send it by attaching in the mail. This is my code and it works perfectly for me.

   public ActionResult sendmail()
    {
        MemoryStream ms = new MemoryStream();
        Document doc = new Document(PageSize.A4, 10f, 10f, 100f, 0.0f);
        PdfWriter writer = PdfWriter.GetInstance(doc, ms);
        doc.Open(); //open doc for editing
        doc.Add(new Paragraph("First Paragraph"));
        doc.Add(new Paragraph("Second Paragraph"));
        writer.CloseStream = false; //important
        doc.Close(); //build the doc.

        ms.Position = 0;

        SmtpClient smtpClient = new SmtpClient();
        smtpClient.Host = "provider.com";
        smtpClient.Credentials = new NetworkCredential("credentialmailid", "password");


        MailMessage mailMessage = new MailMessage()
        {
            From = new MailAddress("[email protected]")
        };
        mailMessage.To.Add(new MailAddress("[email protected]"));
        mailMessage.Subject = "Pdf attached";            
        mailMessage.Attachments.Add(new Attachment(ms, "pdfname.pdf"));
        smtpClient.Send(mailMessage);
        return RedirectToAction("index");
    }

Now my issue is : Document that I have to send is a purchase confirmation . it will have 3 pages. Many headings and styles will be there in it.

also some values I have to pass dynamically like who purchased it and date like wise a lot datas should pass dynamically

How to do this? I thought to create an Html of pdf file to be send and use something like this add parameters dynamically...

        string mailpath = Server.MapPath("~/Mail/HtmlOF_pdfToSend.html");
        string mailbody = System.IO.File.ReadAllText(mailpath);
        mailbody = mailbody.Replace("##CompanyName", "Bhavin Merchant");
        mailbody = mailbody.Replace("##BusinessType", "Bhavin business");

Upvotes: 1

Views: 2387

Answers (2)

Anurag Jain
Anurag Jain

Reputation: 1389

Fist You have to add iTextSharp dll then u have to add some packages :

using iTextSharp.text;
using iTextSharp.text.pdf;
using iTextSharp.text.html;
using iTextSharp.text.xml;
using iTextSharp.text.html.simpleparser;

then as per your question. you want to pass dynamically values so i post some syntax as example :

       // Create a Document object
        var document = new Document(PageSize.A4, 50, 50, 25, 25);
        var output = new MemoryStream();

    // Create a new PdfWriter object, specifying the output stream
    var writer = PdfWriter.GetInstance(document, output);

   // Open the Document for writing
        document.Open();

Suppose you have header in your pdf documnet so syntax will be :

        var logo = iTextSharp.text.Image.GetInstance(Server.MapPath("~/images/it.jpg"));
        logo.SetAbsolutePosition(300, 750);
        document.Add(logo);

If you want to add phrase:

        Phrase titl = new Phrase("\nE-Ticket\n");
        titl.Font.SetStyle(Font.BOLD);
        document.Add(titl);

Add lines :

   Phrase titl1 = new   Phrase("--------------------------------------------------------------------------------------\n\n");
        titl1.Font.SetStyle(Font.BOLD);
        document.Add(titl1);

Change the style of text :

Here you can change the font style & color.

       Phrase title = new Phrase("Booking Date-" + txtDate1.Text + "\n");
        title.Font.SetStyle(Font.BOLD);
        document.Add(title);

If you want to add pdf table:-dt is data table.

      PdfPTable UserInfoTable = new PdfPTable(dt.Columns.Count);
        PdfPRow row = null;
       UserInfoTable.AddCell(--add cell----);
       document.Add(UserInfoTable);

Close the Document - this saves the document contents to the output stream

        document.Close();


       Response.ContentType = "application/pdf";
        Response.AddHeader("Content-Disposition", string.Format("attachment;filename=Receipt-{0}.pdf", "hello"));
        Response.BinaryWrite(output.ToArray())

Here I paste some example code as your question.

Upvotes: 1

BobbyTables
BobbyTables

Reputation: 4705

You can add more pages to the document like this:

 doc.Open(); //open doc for editing
 doc.Add(new Paragraph("First Paragraph"));
 doc.newPage();
 doc.add(new Paragraph("This is a new page =)"));

Upvotes: 1

Related Questions