简体   繁体   中英

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. 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. And here is the code for me to export as 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. 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

            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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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