troubleshooting Question

Problem in generating pdf docoments using itextsharp

Avatar of dotsandcoms
dotsandcoms asked on
Web Languages and Standards.NET ProgrammingASP.NETVisual C++.NET
2 Comments1 Solution1163 ViewsLast Modified:
Hello experts,
i m generating pdf document but the problem is all the thing is is displayed except server controls

Code of Default2.aspx
<form id="form1" runat="server">
      <asp:PlaceHolder ID="PlaceholderPdf" runat="server"></asp:PlaceHolder>
    <div>
        <table border="1">
            <tr>
                <td colspan="2">
                    aspdotnetcodebook
                </td>
            </tr>
            <tr>
                <td>
                    cell1
                </td>
                <td>
                    cell2
                </td>
            </tr>
            <tr>
                <td colspan="2">
                    <asp:Label ID="lblLabel" runat="server" Text="Label Test"></asp:Label>
                </td>
            </tr>
            <tr>
            <td>
            <asp:Repeater ID="Repeater1" runat="server">
        <ItemTemplate>
            <asp:Label ID="lbl1" runat="server" Text="This is label1"></asp:Label>
        </ItemTemplate>
        <ItemTemplate>
            <asp:Label ID="lbl2" runat="server" Text="This is label2"></asp:Label>
        </ItemTemplate>
        <ItemTemplate>
            <asp:Label ID="lbl3" runat="server" Text="This is label3"></asp:Label>
        </ItemTemplate>
        </asp:Repeater>
        </td>
            </tr>
        </table>
       
    </div>
    </form>


Code of default2.aspx.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Text.RegularExpressions;
using iTextSharp.text;
using iTextSharp.text.pdf;
using iTextSharp.text.html;
using iTextSharp.text.xml;
using System.Xml;
using System.Net;
using iTextSharp.text.html.simpleparser;
public partial class Default2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string TheUrl = Server.MapPath("~/default2.aspx");
        string response = GetHtmlPage(TheUrl);
        CreatePDFDocument(response);
    }
 static string GetHtmlPage(string strURL)
    {

        String strResult;
        WebResponse objResponse;
        WebRequest objRequest = HttpWebRequest.Create(strURL);
        objResponse = objRequest.GetResponse();
        using (StreamReader sr = new StreamReader(objResponse.GetResponseStream()))
        {
            strResult = sr.ReadToEnd();
            sr.Close();
        }
        return strResult;
    }
    public void CreatePDFDocument(string strHtml)
    {

        string strFileName = HttpContext.Current.Server.MapPath("test.pdf");
        // step 1: creation of a document-object
        Document document = new Document();
        // step 2:
        // we create a writer that listens to the document
        PdfWriter.GetInstance(document, new FileStream(strFileName, FileMode.Create));
        StringReader se = new StringReader(strHtml);
        HTMLWorker obj = new HTMLWorker(document);
        document.Open();
        obj.Parse(se);
        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();
    }
}
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 2 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 2 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros