Generate pdf from text file in ASP.Net

Last Reply 10 months ago By PraveenS

Posted 11 months ago

I am creating a text file through code. which contains html tags like div h1 p etc. It also contains images with absolute path. I am creating pdf from this file on a button click. Pdf generation works fine except images doesn't appear on the pdf. I refered your blog

http://www.aspsnippets.com/Articles/Export-ASPNet-Web-Page-with-images-to-PDF-using-ITextsharp.aspx

and appended my text cintent to a div which displayed correctly, and using that div i tried to create the pdf. only change i made was replaced page.rendercontrol with my divid.rendercontrol. but this didn't help either. can you please help me out on this.

Below is my text file content

<p><img alt="" src="http://localhost:11589/NewsLetterImages/2.jpg" style="height:363px; width:545px" /></p> <p> </p> <p>Two-time World Championship bronze-medallist Sindhu advanced to the last eight of women's singles by notching up a dominating 21-13 21-15 win in 40 minutes over Chinese Taipei's Tai Tzu Ying yesterday which should serve as a good morale boost against the formidable Yihan.</p> <p>Two-time World Championship bronze-medallist Sindhu advanced to the last eight of women's singles by notching up a dominating 21-13 21-15 win in 40 minutes over Chinese Taipei's Tai Tzu Ying yesterday which should serve as a good morale boost against the formidable Yihan.</p> <p>Two-time World Championship bronze-medallist Sindhu advanced to the last eight of women's singles by notching up a dominating 21-13 21-15 win in 40 minutes over Chinese Taipei's Tai Tzu Ying yesterday which should serve as a good morale boost against the formidable Yihan.</p> <p>Two-time World Championship bronze-medallist Sindhu advanced to the last eight of women's singles by notching up a dominating 21-13 21-15 win in 40 minutes over Chinese Taipei's Tai Tzu Ying yesterday which should serve as a good morale boost against the formidable Yihan.</p> <p><img alt="" src="http://localhost:11589/NewsLetterImages/6.jpg" style="height:304px; width:456px" /></p> <p> </p> <p>Two-time World Championship bronze-medallist Sindhu advanced to the last eight of women's singles by notching up a dominating 21-13 21-15 win in 40 minutes over Chinese Taipei's Tai Tzu Ying yesterday which should serve as a good morale boost against the formidable Yihan.</p> <p>Two-time World Championship bronze-medallist Sindhu advanced to the last eight of women's singles by notching up a dominating 21-13 21-15 win in 40 minutes over Chinese Taipei's Tai Tzu Ying yesterday which should serve as a good morale boost against the formidable Yihan.</p>

HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(ConfigurationSettings.AppSettings["AdminNewsLetters"] + filename + ".txt");
myRequest.Method = "GET";
WebResponse myResponse = myRequest.GetResponse();
StreamReader sr1 = new StreamReader(myResponse.GetResponseStream(), System.Text.Encoding.UTF8);
string result = sr1.ReadToEnd();
sr1.Close();
myResponse.Close();
//newslettercontent is my div id
newsLetterContent.InnerHtml = result;
pnlView.Visible = true;


Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=TestPage.pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
newsLetterContent.RenderControl(hw);
StringReader sr = new StringReader(sw.ToString());
Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 100f, 0f);
HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
pdfDoc.Open();
htmlparser.Parse(sr);
pdfDoc.Close();
Response.Write(pdfDoc);
LoadGridData();
Response.End();

Thanks

Praveen

Posted 10 months ago

Hi PraveenS,

I have checked your code and the image is exported to pdf also. Refer the below code.

HTML

<div id="newsLetterContent" runat="server">
</div>
<asp:Button ID="btnExport" runat="server" Text="Export" OnClick="btnExport_Click" />

C#

protected void btnExport_Click(object sender, EventArgs e)
{
    StreamReader sr1 = new StreamReader(Server.MapPath("~/Test.txt"));
    string result = sr1.ReadToEnd();
    sr1.Close();
    newsLetterContent.InnerHtml = result;
    Response.ContentType = "application/pdf";
    Response.AddHeader("content-disposition", "attachment;filename=TestPage.pdf");
    Response.Cache.SetCacheability(HttpCacheability.NoCache);
    StringWriter sw = new StringWriter();
    HtmlTextWriter hw = new HtmlTextWriter(sw);
    newsLetterContent.RenderControl(hw);
    StringReader sr = new StringReader(sw.ToString());
    Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 100f, 0f);
    HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
    PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
    pdfDoc.Open();
    htmlparser.Parse(sr);
    pdfDoc.Close();
    Response.Write(pdfDoc);
    Response.End();
}

test.txt

<p><img alt="" src="http://localhost:7120/855581_DownloadBAKFile/1.jpg" style="height:363px; width:545px" /></p> <p> </p> <p>Two-time World Championship bronze-medallist Sindhu advanced to the last eight of women's singles by notching up a dominating 21-13 21-15 win in 40 minutes over Chinese Taipei's Tai Tzu Ying yesterday which should serve as a good morale boost against the formidable Yihan.</p> <p>Two-time World Championship bronze-medallist Sindhu advanced to the last eight of women's singles by notching up a dominating 21-13 21-15 win in 40 minutes over Chinese Taipei's Tai Tzu Ying yesterday which should serve as a good morale boost against the formidable Yihan.</p> <p>Two-time World Championship bronze-medallist Sindhu advanced to the last eight of women's singles by notching up a dominating 21-13 21-15 win in 40 minutes over Chinese Taipei's Tai Tzu Ying yesterday which should serve as a good morale boost against the formidable Yihan.</p> <p>Two-time World Championship bronze-medallist Sindhu advanced to the last eight of women's singles by notching up a dominating 21-13 21-15 win in 40 minutes over Chinese Taipei's Tai Tzu Ying yesterday which should serve as a good morale boost against the formidable Yihan.</p> <p> <p/> <p> <p/> <p> <p/> <p> <p/> <p> <p/> <p><img alt="" src="http://localhost:7120/855581_DownloadBAKFile/6.jpg" style="height:304px; width:456px" /></p> <p> </p> <p>Two-time World Championship bronze-medallist Sindhu advanced to the last eight of women's singles by notching up a dominating 21-13 21-15 win in 40 minutes over Chinese Taipei's Tai Tzu Ying yesterday which should serve as a good morale boost against the formidable Yihan.</p> <p>Two-time World Championship bronze-medallist Sindhu advanced to the last eight of women's singles by notching up a dominating 21-13 21-15 win in 40 minutes over Chinese Taipei's Tai Tzu Ying yesterday which should serve as a good morale boost against the formidable Yihan.</p>

Screenshot


Posted 10 months ago

Hi,

Thanks For the reply.

After few days of scatching i found that the code is exactly the same, but problem is my images are of highres so they are not getting displayed. When i change them to a lower resolution they appeared on pdf. Do we have any solution or alternative to reduce image resolution while creating pdf.

 

Thanks

 

I agree, here is the link: https://www.e-iceblue.com/Introduce/spire-office-for-net-free.html?aff_id=108