0

In asp.net web form I'm using itextsharp to save a Panel contains Gridview to PDF file.
I have 3 lines before the Gridview to display (the company logo, address and title of the gridview).
The PDF is created without the logo + all CSS (colors, fonts .. etc is not applied).
I have updated itextsharp to the recent version with no success

here is my aspx code:

<asp:Panel runat="server" ID="PaidOrdersPanel">
<!-- to be shown on PDF-->
<img src="http://dev.123.com/img/logo.png" style="width: 249px; height: auto; display: none;" />
<span style="color: #10598B; font-weight: normal; font-size: 12px !important; display: none; font-family: Corbel;" id="h9">123 Inc.<br />
123 St.<br />
city<br />
postal code</span>
<h5 style="color: #10598B; display: none; text-align: center;">Paid orders report</h5>
<asp:GridView runat="server" ID="PaidOrdersGV" RowStyle-Wrap="false" AllowPaging="true" PageSize="10" Width="100%" CssClass="Grid" AlternatingRowStyle-CssClass="alt" AutoGenerateColumns="false"
    PagerStyle-CssClass="pgr" HeaderStyle-ForeColor="White" PagerStyle-HorizontalAlign="Center" HeaderStyle-HorizontalAlign="Center" RowStyle-HorizontalAlign="Center" DataKeyNames="Document#"
     OnPageIndexChanging="PaidOrdersGV_PageIndexChanging" OnRowDataBound="PaidOrdersGV_RowDataBound" OnRowCommand="PaidOrdersGV_RowCommand">
    <EmptyDataTemplate>
     <div style="text-align: center;">no records found</div>
     </EmptyDataTemplate>
     <Columns>
     <asp:ButtonField CommandName="PaidOrders_Details" DataTextField="Document#" HeaderText="Document #" SortExpression="Document#" ItemStyle-ForeColor="Black" ItemStyle-Font-Underline="true" />
    <asp:BoundField DataField="Order#" HeaderText="order #" SortExpression="Order#" />
     <asp:BoundField DataField="Order Date" HeaderText="Order Date" SortExpression="Order Date" DataFormatString="{0:d}"></asp:BoundField>
    <asp:BoundField DataField="Status" HeaderText="Status" SortExpression="Status"></asp:BoundField>
     <asp:BoundField DataField="Amount" HeaderText="Amount" SortExpression="Amount" DataFormatString="{0:C2}"></asp:BoundField>
     </Columns>
</asp:GridView>
</asp:Panel>

and C# code:

if (PaidOrdersGV.Rows.Count > 0)
{
    //to allow paging=false & change style.
    PaidOrdersGV.HeaderStyle.ForeColor = System.Drawing.Color.Black;
    PaidOrdersGV.BorderColor = Color.Gray;
    PaidOrdersGV.Font.Name = "Tahoma";
    PaidOrdersGV.DataSource = clsBP.get_PaidOrders(lbl_BP_Id.Text);
    PaidOrdersGV.AllowPaging = false;
    PaidOrdersGV.Columns[0].Visible = false;
    PaidOrdersGV.DataBind();

    //to PDF code 
    string attachment = "attachment; filename=myFile.pdf";
    Response.ClearContent();
    Response.AddHeader("content-disposition", attachment);
    Response.ContentType = "application/pdf";
    StringWriter stw = new StringWriter();
    HtmlTextWriter htextw = new HtmlTextWriter(stw);
    htextw.AddStyleAttribute("font-size", "8pt");
    htextw.AddStyleAttribute("color", "Grey");

    PaidOrdersPanel.RenderControl(htextw);//Name of the Panel
    Document document = new Document();
    document = new Document(PageSize.A4, 5, 5, 15, 5);
    FontFactory.GetFont("Tahoma", 50, iTextSharp.text.BaseColor.BLUE);
    PdfWriter.GetInstance(document, Response.OutputStream);
    document.Open();

    StringReader str = new StringReader(stw.ToString());
    HTMLWorker htmlworker = new HTMLWorker(document);
    htmlworker.Parse(str);

    document.Close();
    Response.Write(document);
}

Please advise

Community
  • 1
  • 1
Sam
  • 406
  • 1
  • 11
  • 24
  • 1
    XML Worker parses XHTML. You seem to be feeding it ASP, but ASP is not XHTML. You need to render your ASP file to XHTML first. – Bruno Lowagie Mar 19 '15 at 16:45
  • 1
    See this post, [How to convert HTML to PDF using iTextSharp](http://stackoverflow.com/questions/25164257/how-to-convert-html-to-pdf-using-itextsharp), specifically the fourth paragraph for what @BrunoLowagie is talking about. – Chris Haas Mar 19 '15 at 17:23
  • Great answer @ChrisHaas Now we can also close this question: http://stackoverflow.com/questions/29124374/how-to-get-aspx-page-div-data-in-itext-pdf – Bruno Lowagie Mar 19 '15 at 17:30
  • @ChrisHaas in your example (Example #2 ) is very well explained. you have used a file, i need to convert only a panel inside aspx file. could you give a hint :) – Sam Mar 19 '15 at 18:53
  • 1
    You need to render your _whatever_ to HTML and the important part to remember is that task is 100% unrelated to iTextSharp and up to you to figure out. You can [try this](http://stackoverflow.com/a/58931/231316) for a start but if you have any problems, ask a new question one here. Once you get your HTML rendered, you **must** look at it, don't trust the system to have done it correctly. – Chris Haas Mar 19 '15 at 20:01

0 Answers0