c# - how to add html content to document -


i trying convert html pdf.i have html code. ex:

var example_html = @"<p>this                         <em>is</em>                         <span class=""                               headline""                               style=""text-decoration: underline;"">some</span>                         <strong>sample                             <em>text</em>                         </strong>                         <span style=""color: red;"">!!!</span>                     </p>"; 

i have added below code generate html content pdf throwing error saying "the documnet has no pages"

response.clear(); response.contenttype = "application/pdf"; using (document doc = new document()) {     pdfwriter writer = pdfwriter.getinstance(doc, response.outputstream);     doc.open();     //doc.add(new paragraph(example_html));     using (textreader reader = file.opentext(server.mappath("~/gst/htmltopdf.aspx")))     {         xmlworkerhelper.getinstance().parsexhtml(writer, doc, reader);     }     doc.close(); } response.end(); 

in doc need pass or add html content right?? dont know how add html content can parse pdf.kindly help.thank you.

as error stated aren't adding pages document. following code worked me returns pdf title mydoc.pdf.

in question mention wanted convert example_html pdf code sample reading file named htmltopdf.aspx. make sure file has html. tried reading file , variable directly , works in both situations.

to add more pages call newpage method , html page shown below:

public actionresult index() {     httpcontext.response.clear();     httpcontext.response.contenttype = "application/pdf";     httpcontext.response.appendheader("content-disposition", "attachment; filename=" + "mydoc.pdf");      var page1 = @"<p>page 1 <em>is </em><span class=""headline"" style=""text-decoration: underline;"">some</span> <strong>sample <em> text</em></strong><span style=""color: red;"">!!!</span></p>";     var page2 = @"<p>page 2 <em>is </em><span class=""headline"" style=""text-decoration: underline;"">some</span> <strong>sample <em> text</em></strong><span style=""color: red;"">!!!</span></p>";     var page3 = @"<p>page 3 <em>is </em><span class=""headline"" style=""text-decoration: underline;"">some</span> <strong>sample <em> text</em></strong><span style=""color: red;"">!!!</span></p>";      memorystream outputstream = new memorystream();     using (document doc = new document())     {         pdfwriter writer = pdfwriter.getinstance(doc, response.outputstream);         doc.open();          xmlworkerhelper.getinstance().parsexhtml(writer, doc, new stringreader(page1));          // add more pages can call newpage , add other html snippets         doc.newpage();         xmlworkerhelper.getinstance().parsexhtml(writer, doc, new stringreader(page2));         doc.newpage();         xmlworkerhelper.getinstance().parsexhtml(writer, doc, new stringreader(page3));     }      return view(); } 

this sample creates 3-page pdf different html on each page


Comments

Popular posts from this blog

java - Date formats difference between yyyy-MM-dd'T'HH:mm:ss and yyyy-MM-dd'T'HH:mm:ssXXX -

c# - Get rid of xmlns attribute when adding node to existing xml -