c# - create pdf from current page ASP.NET -
i want create pdf current page on button click. code looks that:
protected void btnsave_click(object sender, eventargs e) { renderpdf = true; } protected override void render(htmltextwriter writer) { if (renderpdf == true) { dtanswer = taanswer.getdata(); memorystream mem = new memorystream(); streamwriter twr = new streamwriter(mem); htmltextwriter mywriter = new htmltextwriter(twr); base.render(mywriter); mywriter.flush(); mywriter.dispose(); streamreader strmrdr = new streamreader(mem); //strmrdr.basestream.position = 3300; //string pagecontent = strmrdr.readtoend(); //strmrdr.dispose(); //mem.dispose(); //writer.write(pagecontent); //createpdfdocument(pagecontent); stringbuilder sb = new stringbuilder(); sb.append("<table border='1' width='100%' style='font-size:11px; font-weight:bold; border-collapse:collapse; font-family: arial, 'lucida sans unicode', sans-serif; '>"); sb.append("<tr>"); sb.append("<td style='background-color:red; text-align:center;' colspan ='3'>maxon motor</td>"); sb.append("</tr>"); sb.append("<tr>"); sb.append("<td width='18%'><p>maxon motor ag</p><p>sachseln</p></td>"); sb.append("<td width ='64%' style='text-align:center'><p>vorstellungsgespräch</p></td>"); sb.append("<td width='18%'><p>seite</p></td>"); sb.append("</tr>"); sb.append("</table>"); sb.append("<br /><br />"); sb.append("<table style=\"width:100%;font-weight:bold;border-collapse:collapse; font-family: arial, 'lucida sans unicode', sans-serif;\">"); sb.append("<tr>"); sb.append("<td><p>name / vorname:</p></td>"); sb.append("<td></td>"); sb.append("</tr>"); sb.append("<tr>"); sb.append("<td><p>geb. datum:</p></td>"); sb.append("<td></td>"); sb.append("</tr>"); sb.append("<tr>"); sb.append("<td><p>offene stelle zugewiesen:</p></td>"); sb.append("<td></td>"); sb.append("</tr>"); sb.append("<tr>"); sb.append("<td><p>gesprächsteilnehmer:</p></td>"); sb.append("<td></td>"); sb.append("</tr>"); sb.append("</table>"); sb.append("<br /><br />"); sb.append("<div>"); (int = 0; < pquestions.controls.count; i++) { string sname = "lbl" + pquestions.controls[i].id.substring(3, pquestions.controls[i].id.length - 3); if (pquestions.controls[i].id == sname) { if (!pquestions.controls[i].id.contains(",")) { string sblockname = pquestions.controls[i].id.substring(3, pquestions.controls[i].id.length - 3); sb.append("<p><b>" + sblockname + "</b></p>"); sb.append("<br />"); (int t = 0; t < pquestions.controls.count; t++) { if (pquestions.controls[t].id.contains(sblockname)) { if (pquestions.controls[t].id.contains("lbl") && pquestions.controls[t].id != pquestions.controls[i].id) { sb.append("<p>" + ((label)(pquestions.controls[t])).text + "</p>"); sb.append("<br />"); } else if (pquestions.controls[t].id.contains("txt")) { if (((textbox)(pquestions.controls[t])).text != "") { sb.append("<p>" + ((textbox)(pquestions.controls[t])).text + "</p>"); sb.append("<br />"); } else { sb.append("<br /><br /><br /><br />"); } } } } } } } sb.append("</div>"); stringreader sr = new stringreader(sb.tostring()); string rr= sb.tostring(); createpdfdocument(rr); } else { system.text.stringbuilder sb = new system.text.stringbuilder(); htmltextwriter tw = new htmltextwriter(new stringwriter(sb)); base.render(tw); // captured markup string string pagesource = tw.tostring(); //get rendered content string scontent = sb.tostring(); //now output page, if want writer.write(scontent); } } public void createpdfdocument(string strhtml) { string strhtmlpath = "c:\\myhtml.html"; streamwriter strwriter = new streamwriter(strhtmlpath, false, system.text.encoding.utf8); strwriter.write(strhtml); strwriter.close(); string strfilename = "c:\\map1.pdf"; // step 1: creation of document-object document document = new document(); // step 2: // create writer listens document pdfwriter.getinstance(document, new filestream(strfilename, filemode.create)); stringreader se = new stringreader(strhtml); textreader tr = new streamreader("c:\\myhtml.html"); //add collection document document.open(); htmlworker worker = new htmlworker(document); worker.startdocument(); //// step 5: parse html document worker.parse(tr); //// step 6: close document , worker worker.enddocument(); //worker.parse(tr); // getting error "illegal characters in path" document.close(); showpdf(strfilename); } public void showpdf(string strfilename) { response.clearcontent(); response.clearheaders(); response.addheader("content-disposition", "inline;filename=" + strfilename); response.contenttype = "application/pdf"; response.writefile(strfilename); response.flush(); response.clear(); } now, problem is, want create e pdf 1 part of page , not whole page. how can realize that?
thanks lot! sarah
you have create string congaing html want see in pdf css not applied through class. applying css have write inline-css dom elements. can below
string = "pdf contents in html format"; stringreader sr = new stringreader(sb.tostring()); document pdfdoc = new document(pagesize.a4, 10f, 10f, 10f, 0f); htmlworker htmlparser = new htmlworker(pdfdoc); using (memorystream memorystream = new memorystream()) { pdfwriter writer = pdfwriter.getinstance(pdfdoc, memorystream); pdfdoc.open(); htmlparser.parse(sr); pdfdoc.close(); byte[] bytes = memorystream.toarray(); memorystream.close(); } for more info visit this
hope helpfull
Comments
Post a Comment