使用asp

时间:2016-11-24 14:17:27

标签: html pdf itext

我在asp.net的一个申请中以PDF格式生成发票(账单)。对于这件事,我使用的是itextsharp库,但我有一个问题: 它是如何知道我的页面的,我的意思是,它在pdf页面上写了多少? 它正在自动缩放或类似的东西?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Text;
using System.IO;
using iTextSharp.text;
using iTextSharp.text.html.simpleparser;
using iTextSharp.text.pdf;

namespace KdfFacturi
{
    public partial class test : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string companyName = "Nemboolisoft";
            int orderNo = 1234;
            DataTable dt = new DataTable();
            dt.Columns.AddRange(new DataColumn[5] {
                            new DataColumn("ProductId", typeof(string)),
                            new DataColumn("Product", typeof(string)),
                            new DataColumn("Price", typeof(int)),
                            new DataColumn("Quantity", typeof(int)),
                            new DataColumn("Total", typeof(int))});
            dt.Rows.Add(101, "Sun Glasses", 200, 5, 1000);
            dt.Rows.Add(102, "Jeans", 400, 2, 800);
            dt.Rows.Add(103, "Trousers", 300, 3, 900);
            dt.Rows.Add(104, "Shirts", 550, 2, 1100);

        using (StringWriter sw = new StringWriter())
         {
              using (HtmlTextWriter hw = new HtmlTextWriter(sw))
              {
                   StringBuilder sb = new StringBuilder();

                   //Generate Invoice (Bill) Header.
                   sb.Append("<table width='100%' cellspacing='0' cellpadding='2'>");
                   sb.Append("<tr><td align='center' style='background-color: #18B5F0' colspan = '2'><b>Order Sheet</b></td></tr>");
                   sb.Append("<tr><td colspan = '2'></td></tr>");
                   sb.Append("<tr><td><b>Order No: </b>");
                   sb.Append(orderNo);
                   sb.Append("</td><td align = 'right'><b>Date: </b>");
                   sb.Append(DateTime.Now);
                   sb.Append(" </td></tr>");
                   sb.Append("<tr><td colspan = '2'><b>Company Name: </b>");
                   sb.Append(companyName);
                   sb.Append("</td></tr>");
                   sb.Append("</table>");
                   sb.Append("<br />");

                   //Generate Invoice (Bill) Items Grid.
                   sb.Append("<table border = '1'>");
                   sb.Append("<tr>");
                   foreach (DataColumn column in dt.Columns)
                   {
                        sb.Append("<th style = 'background-color: #D20B0C;color:#ffffff'>");
                        sb.Append(column.ColumnName);
                        sb.Append("</th>");
                   }
                   sb.Append("</tr>");
                   foreach (DataRow row in dt.Rows)
                   {
                        sb.Append("<tr>");
                        foreach (DataColumn column in dt.Columns)
                        {
                             sb.Append("<td>");
                             sb.Append(row[column]);
                             sb.Append("</td>");
                        }
                        sb.Append("</tr>");
                   }
                   sb.Append("<tr><td align = 'right' colspan = '");
                   sb.Append(dt.Columns.Count - 1);
                   sb.Append("'>Total</td>");
                   sb.Append("<td>");
                   sb.Append(dt.Compute("sum(Total)", ""));
                   sb.Append("</td>");
                   sb.Append("</tr></table>");

                   //Export HTML String as PDF.
                    //facem documentul
                   Document pdfDoc = new Document();

                    //facem listener-ul care asculta documentul
                   PdfWriter writer = PdfWriter.GetInstance(pdfDoc, Response.OutputStream);

                    //deschide documentul
                   pdfDoc.Open();

                    //formam continutul 
                  StringReader sr = new StringReader(sb.ToString());



                   for (int i = 0; i < 3; i++)
                   {
                       pdfDoc.NewPage();
                       HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
                       htmlparser.Parse(sr);

                   }
                   pdfDoc.Close();
                   Response.ContentType = "application/pdf";
                   Response.AddHeader("content-disposition", "attachment;filename=Invoice_" + orderNo + ".pdf");
                   Response.Cache.SetCacheability(HttpCacheability.NoCache);
                   Response.Write(pdfDoc);
                   Response.End();
              }
         }
    }
    }
}

这就是我试图添加3个内容相同但不起作用的页面!

0 个答案:

没有答案