我正在尝试使用c#从div制作pdf。要完成此对象,我使用iTextShap。我在互联网上找到了解决方案,从给定的HTML代码创建pdf。
<asp:Panel ID="Panel1" runat="server">
<table style="border:1px solid">
<tr style="background-color:green;">
<td style="width:10%; color:white; font-weight:bold">Sr.</td>
<td style="width:60%; color:white; font-weight:bold">Particular</td>
<td style="width:25%; color:white; font-weight:bold">Price</td>
</tr>
<tr style="background-color:#ffffff">
<td style="width:10%">01.</td>
<td style="width:60%">3 keywords and printers</td>
<td style="width:25%">2000 INR</td>
</tr>
<tr style="background-color:#e1c5ed">
<td style="width:10%">02.</td>
<td style="width:60%">3 acer laptops</td>
<td style="width:25%">90000 INR</td>
</tr>
<tr style="background-color:#ffffff">
<td style="width:10%">03.</td>
<td style="width:60%">2 dell laptops</td>
<td style="width:25%">60000 INR</td>
</tr>
</table>
</asp:Panel>
我的浏览器输出看起来像这样 html output image
我的按钮点击事件的代码如下所示
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=pdffile.pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
Panel1.RenderControl(hw);
StringReader sr = new StringReader(sw.ToString());
Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 100f, 0f);
HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
pdfDoc.Open();
htmlparser.Parse(sr);
pdfDoc.Close();
Response.Write(pdfDoc);
Response.End();
上面的代码创建了没有任何CSS样式的pdf。我想问一下如何应用css并像网页一样创建pdf?当我创建pdf时,我的表看起来像这样 PDF output 有谁能够帮我。提前谢谢。