1

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");

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

6 Comments

I mean there will be colors to some texts in it and also draw lines . so still this is the convenient way? Text is huge..like terms and conditions. Adding this much in code behind is good?
Actually it is the terms and the conditions and other business details. So Is it good method?
are you asking , how to generate pdf dynamically or asking about dynamically generate pdf , is it convenient or not ?
I am sorry for not specific. As I told in the question, I am getting a test type pdf correctly. but to get a pdf with huge texts and styles this is the convenient idea?if not is there any other method?and if this is the correct method for my requirements, how can I drow lines and add styles to texts ?
|
1

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 =)"));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.