Reputation: 113
Using itextSharp and sharepoint visual webpart coding I am converting current aspx page to pdf but I am not to able convert. I have found that issue is in this line "this.Page.RenderControl(hw)" If I comment this line I get "The document has no pages" exception and If I uncomment this line I get "A page can have only one server-side Form tag" error. How can I handle this? Pls somebody help me pls..
protected void BtnSubmit_Click(object sender, EventArgs e)
{
try
{
HttpContext.Current.Response.ContentType = "application/pdf";
HttpContext.Current.Response.AddHeader("content-disposition", "attachment;filename=UserDetails.pdf");
HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.NoCache);
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
//this.Page.RenderControl(hw);
StringReader sr = new StringReader(sw.ToString());
Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 100f, 0.0f);
HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
PdfWriter.GetInstance(pdfDoc, HttpContext.Current.Response.OutputStream);
pdfDoc.Open();
htmlparser.Parse(sr);
pdfDoc.Close();
HttpContext.Current.Response.Write(pdfDoc);
HttpContext.Current.Response.End();
}
catch (Exception exp)
{
}
}
protected override void Render(HtmlTextWriter writer)
{
// Ensure that the control is nested in a server form.
if (Page != null)
{
Page.VerifyRenderingInServerForm(this);
}
base.Render(writer);
}
Upvotes: 0
Views: 1405
Reputation: 192
You need to add following lines:
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
printableArea.RenderControl(hw); // Here printable area is nothing but div on aspx page with id and runaat="server" tag.
StringReader sr = new StringReader(sw.ToString());
string strHtml = sr.ReadToEnd();
sr.Close();
Upvotes: 0
Reputation: 9372
Because of the way ASP.NET web forms page lifecycle works (sharepoint very similar), you need to move the PDF generation code into the Render
stage. IIRC that's why you see the "A page can have only one server-side Form tag" Exception - your Page.RenderControl()
call in the button click event handler comes before the Render()
stage, and basically results in rendering the page contents twice.
Anyway, here's a simple working example using XMLWorker
to get you started:
first make sure to include these namespaces:
using iTextSharp.text;
using iTextSharp.text.pdf;
using iTextSharp.text.xml;
using iTextSharp.tool.xml;
and then:
// flag when page renders normally / when you're sending PDF
private bool _convertToPdf;
// set _convertToPdf in your BtnSubmit_Click() - I never use AutoEventWireup
public void ProcessPage(object sender, CommandEventArgs e) {
_convertToPdf = true;
}
protected override void Render(HtmlTextWriter writer) {
if (!_convertToPdf) { base.Render(writer); }
else {
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=thisPage.pdf");
var sb = new StringBuilder();
using (var sw = new StringWriter(sb)) {
using (var htmlTextWriter = new HtmlTextWriter(sw)) {
base.Render(htmlTextWriter);
using (var document = new Document()) {
var pdfWriter = PdfWriter.GetInstance(document, Response.OutputStream);
document.Open();
using (var stringReader = new StringReader(sb.ToString())) {
XMLWorkerHelper.GetInstance().ParseXHtml(
pdfWriter, document, stringReader
);
}
}
}
}
Response.End();
}
Don't have high expectations regarding how well the PDF displays compared to your aspx
page. iTextSharp isn't meant to be a full-blown HTML to PDF converter.
Upvotes: 1