我有一个htm页面来安排我将aspx网页导出为pdf的位置。这是代码:
<form id="form1" runat="server">
<div>
<h1>Packaging Slip</h1>
<table>
<tr>
<td>Packing Date</td>
<td>[PACKINGDATE]</td>
</tr>
<tr>
<td>Delivery Date</td>
<td>[DELIVERYDATE]</td>
</tr>
<tr>
<td>Beneficiary Name</td>
<td>[BENEFICIARYNAME]</td>
</tr>
<tr>
<td colspan ="2">[ADDRESS]</td>
</tr>
<tr>
<td>Mobile</td>
<td>[MOBILE]</td>
</tr>
<tr>
<td>Contact(Work)</td>
<td>[CONTACTWORK]</td>
</tr>
<tr>
<td>Fax</td>
<td>[FAX]</td>
</tr>
</table>
[ITEMS]
</div>
</form>
[ITEM]我稍后应该用网格视图替换。以下是我作为pdf导出的代码:
// Create a Document object
var document = new Document(PageSize.A4, 50, 50, 25, 25);
// Create a new PdfWriter object, specifying the output stream
var output = new MemoryStream();
var writer = PdfWriter.GetInstance(document, output);
// Open the Document for writing
document.Open();
string contents = File.ReadAllText(Server.MapPath("~/FoodBankSystem/Distributions/Packing/PackagingSlip.htm"));
// Replace the placeholders with the user-specified text
contents = contents.Replace("[PACKINGDATE]", lblPackDate.Text);
contents = contents.Replace("[DELIVERYDATE]", lblDeliveryDate.Text);
contents = contents.Replace("[BENEFICIARYNAME]", lblBeneficiaryName.Text);
contents = contents.Replace("[ADDRESS]", lblAddress.Text);
contents = contents.Replace("[MOBILE]", lblContactMobile.Text);
contents = contents.Replace("[CONTACTWORK]", lblWorkContact.Text);
contents = contents.Replace("[FAX]", lblFax.Text);
// Step 4: Parse the HTML string into a collection of elements...
var parsedHtmlElements = HTMLWorker.ParseToList(new StringReader(contents), null);
// Enumerate the elements, adding each one to the Document...
foreach (var htmlElement in parsedHtmlElements)
document.Add(htmlElement as IElement);
document.Close();
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "inline;filename=UserDetails.pdf");
Response.BinaryWrite(output.ToArray());
然而,对于[ITEM]部分,我不知道如何循环它。有人可以发布一些例子吗?提前谢谢。
答案 0 :(得分:0)
对于[Items]您需要根据项目数据和列生成代码隐藏的HTML表格并将其替换为此标记
DataTable dt = new DataTable();
dt.Columns.Add("Item");
dt.Columns.Add("Description");
dt.Columns.Add("QTY");
for(int i=0;i<10;i++)
{
DataRow dr = dt.NewRow();
dr["Item"] = "Item" + i.ToString();
dr["Description"] = "Test" + i.ToString();
dr["QTY"] = i.ToString();
dt.Rows.Add(dr);
}
string data = "<table border=1>";
data += "<TR>";
data += "<TD>Item</TD>";
data += "<TD>Description</TD>";
data += "<TD>QTY</TD>";
data += "</TR>";
foreach(DataRow dr1 in dt.Rows)
{
data += "<TR>";
data += "<TD>" + dr1["Item"].ToString() + "</TD>";
data += "<TD>" + dr1["Description"].ToString() + "</TD>";
data += "<TD>" + dr1["QTY"].ToString() + "</TD>";
data += "</TR>";
}
data += "</table>";
contents = contents.Replace("[ITEMS]", data);
您只需要将[data]变量替换为[ITEMS]标记。