使用itextsharp的图像 [英] images using itextsharp
问题描述
大家好,
我正在使用 itextsharp 将Gridview保存为PDF文件。
我在Gridview之前有3行显示(gridview的标识,地址和标题)。这是我的aspx代码:
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: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>
和C#代码:
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);
}
PDF已创建,但图片未显示在文件中+所有CSS(颜色,字体..等等不适用)。
我错过了一些东西吗?
请指教
提前感谢
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
推荐答案
我解决了图像和CSS样式的问题。
itextSharp喜欢旧的HTML标签,例如
I have solved the problem of images and CSS style.
itextSharp likes the old HTML tags like
<table></table>
当我将所有HTML代码放在一个表格中时,一切都像魅力
请把你所有的CSS 内联
PS我使用过最新的dll 5.5
谢谢大家的帮助:)!
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 :) !
尝试使用其他的不同的.dll来源。
有很多版本的itextsharp dll有些有这个bug。
try with diffrent .dll from other sources.
there are many versions of itextsharp dll some having that bug.
这篇关于使用itextsharp的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!