Click here to Skip to main content
15,900,586 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hai All,

How to Create a pdf with some textbox content in ASP.net(C#).please help me..Currently i am using the following code.Everthing is fine but it will open a download window to download or showing the pdf...i dont need the pop window but i want to access the created pdf to attach a mail in mail sending progrm.Anyone can explain how to modify the following code or send any new method

C#
string src = string.Empty;
            string AbsolutePath = string.Empty;
            _Filter.Clear();
            _Filter.Add("Id",hdnCertificateId.Value);
            _CertificatesEntityCollection = CertificatesManager.SelectFiltered(_Filter);

            ScriptManager.RegisterStartupScript(this, typeof(string), "script123", "AsPdf()", true);
            Response.Clear();
            Response.ClearContent();
            Response.ClearHeaders();
            Response.ContentType = "application/pdf";
            //Response.AddHeader("content-disposition", "attachment;filename=" + _CertificatesEntityCollection.First().Name + ".pdf");
            Response.AddHeader("Content-Disposition", "attachment; filename=\"" + _CertificatesEntityCollection.First().Name + "\"");

         

            Response.Cache.SetCacheability(HttpCacheability.NoCache);
            StringWriter sw = new StringWriter();
            HtmlTextWriter hw = new HtmlTextWriter(sw);

            pnlCertificate.RenderControl(hw);
            pnlCertificate.Style.Add("border", "2px solid black");
            pnlCertificate.Style.Add("margin-top", "60px");
            pnlCertificate.Style.Add("text-align", "justify");
            
            src = sw.ToString();
            AbsolutePath = HttpContext.Current.Request.Url.Scheme + "://" + HttpContext.Current.Request.Url.Authority + HttpContext.Current.Request.ApplicationPath;
            src = src.Replace("img src=\"/", string.Format("img src=\"{0}", AbsolutePath));
            StringReader sr = new StringReader(src);
            //Document pdfDoc = new Document(PageSize.A4_LANDSCAPE, 20f, 10f, 50f, 0f);
            Document pdfDoc = new Document();
            pdfDoc.SetPageSize(iTextSharp.text.PageSize.A4.Rotate());
            
            HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
            PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
            pdfDoc.Open();
            htmlparser.Parse(sr);
            pdfDoc.Close();
            Response.Write(pdfDoc);
            Response.End();


Thnks
Aatif Ali
Posted
Updated 13-Jun-13 20:37pm
v4
Comments
VICK 14-Jun-13 3:41am    
Have you tried IPDF sharp dll as well?? I think that works almost same like Itextsharp...
irwin.potter 5-Jul-17 5:51am    
First I would suggest you to take a look at this Word and PDF library for C#, it can enable you to straightforwardly convert a HTML content into PDF format from C#. For example:

StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);

pnlCertificate.RenderControl(hw);
pnlCertificate.Style.Add("border", "2px solid black");
pnlCertificate.Style.Add("margin-top", "60px");
pnlCertificate.Style.Add("text-align", "justify");

DocumentModel document = new DocumentModel();
document.Content.LoadText(sw.ToString(), LoadOptions.HtmlDefault);

// You could use the following to download a PDF file.
//document.Save(Response, "Document.pdf");

// Or you could use the following to save PDF to stream.
MemoryStream stream = new MemoryStream();
document.Save(stream, SaveOptions.PdfDefault);

Now regarding the mail, you could use this email library for C# and use the following code to send an email message with attachment in C#.

You can use iTextSharp:
C#
var doc1 = new iTextSharp.text.Document();
        string path = Server.MapPath("PDF_Files");
        PdfWriter.GetInstance(doc1, new FileStream(path + "/Doc1.pdf", FileMode.Create));
        doc1.Open();
        doc1.Add(new Paragraph(textbox.text));
        doc1.Close();

Found it: here[^]

Also check out:
PDF File Writer C# Class Library[^]
Generate PDF Using C#[^]
Generating PDF using ItextSharp with Footer in C#[^]
 
Share this answer
 
Comments
Thanks7872 14-Jun-13 2:30am    
Good shot.. :).... ↑voted...I was about to post this ones... :D..!
Prasad_Kulkarni 14-Jun-13 2:36am    
Thank you Rohan!
Thanks7872 14-Jun-13 2:36am    
Most welcome prasad.. :)
Aatif Ali from Bangalore 14-Jun-13 2:53am    
Thanks Prasad...now we are adding textbox content...ma content is like

Test Track
Certificate...

<br><br><p style="font-size:14px;Textalign:
center">    This is to certify that Muhammed.. Ali
bearing the address Oklahoma,<br><br>Reesiyo,
Nebraska, 90010 has enrolled for the track named
Test track
on 06-12-2012<br><br> and has successfully completed
the track of studies on 14-02-2013 with a percentage of
<br><br>     53.</p><br><br>------------
------------------------
<br>Best Compliments<br>David
Gadish
<br>Date:14-02-2013<br><br><br>
hwo to change thsese text to normal text with specified design...and add to pdf...?
Prasad_Kulkarni 14-Jun-13 2:57am    
If you are converting HTML TO PDF then refer my previous answer for same:
Html to PDF conversion using c#[^]

..and if my answers solves your problem then hit button to 'Accept Solution' so others can refer it.
This link will be enough to resolve the problem
Add Images and Textboxes to PDF[^]

But before starting with that I would suggest you to take a look at PDF Reference and Adobe Extensions to the PDF Specification[^]


--Amit
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900