简体   繁体   English

使用htm将aspx网页导出为pdf

[英]Export aspx web page to pdf using htm

I've got a htm page to arrange the position for me to export the aspx web page as pdf. 我有一个htm页面,可以安排我将aspx网页导出为pdf的位置。 Here is the code: 这是代码:

<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>

The [ITEM] I should replace with grid view later on. 我稍后将用网格视图替换的[ITEM]。 And here is the code for me to export as pdf: 这是我导出为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());

However, for the [ITEM] part, I have no idea how to loop it. 但是,对于[ITEM]部分,我不知道如何循环它。 Can somebody posts some example? 有人可以举一些例子吗? Thanks in advance. 提前致谢。

For [Items] You need to generate HTML Table for Code Behind based on Item data and Columns and Replace it this TAG 对于[Items],您需要根据Item数据和列为“隐藏代码”生成HTML表格,并将其替换为该TAG

            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); 

You just need to replace "data" variable with [ITEMS] Tag. 您只需要用[ITEMS]标记替换“数据”变量即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM