Click here to Skip to main content
15,889,281 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi all,
I'm using itextsharp to save Gridview to PDF file.
I have 3 lines before the Gridview to display (the logo, address and title of the gridview). here is my aspx code:
ASP.NET
<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:
C#
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);
            }


the PDF is created but the image is not shown in the file + all CSS (colors, fonts .. etc is not applied).

am I missing some thing?

please advise

thanks in advance
Posted
Comments
Arkadeep De 18-Mar-15 14:11pm    
I had stuck into this kind of problem once. I just changed the dll, you may took the dll from http://www.aspsnippets.com/ from where I had taken. I don't know why, may it was for an older version dll. Try this once.

I have solved the problem of images and CSS style.
itextSharp likes the old HTML tags like
HTML
<table></table>


when i placed all my HTML code inside a table, everything worked like charm
please put all your CSS inline

P.S. I have used the latest dll 5.5

thank you all for your help :) !
 
Share this answer
 
v2
try with diffrent .dll from other sources.
there are many versions of itextsharp dll some having that bug.
 
Share this answer
 
Comments
Samira Radwan 19-Mar-15 12:12pm    
tried Version 5 the latest one, still have the same!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900