pdf输出上的重复Stringreader

时间:2017-11-09 18:21:53

标签: c# asp.net pdf

我正在ASP.NET MVC上制作af凭证生成器,当用户在文本字段上书写然后使用这些值进行PDF输出时,我已设法获得生成凭证。

我希望可以选择在制作此PDF文件时编写多少张优惠券。

我的代码如下:

public FileResult Edit(SpiseKopunTable model)
    {
        bool b = repository.PutSpiseKopun(model);
        DateTime d = new DateTime(1980, 1, 1);
        DateTime.TryParse(model.Dato.ToString(), out d);
        int c = new int();
        int.TryParse(model.ID.ToString(), out c);
        using (MemoryStream stream = new System.IO.MemoryStream())
        {
            StringReader sr = new StringReader("<table><tbody><tr><td>GL:" + model.GL + 
                "</td></ tr >< tr >" + 
                "< td > Service:" + model.Service + 
                "</ td ></ tr >< tr >" +
                "< td > Dato:" + d.ToString("dd-MM-yyyy") +
                "</ td ></ tr >< tr >" +
                "< td > Pris:" + model.Pris +
                "</ td ></ tr >< tr >" +
                "< td > Kommentar:" + model.Kommentar +
                "</ td ></ tr><tr>" + 
                "<td> ID:" + c.ToString() + 
                "</ td ></ tr ></ tbody ></ table >");
            Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 100f, 0f);
            PdfWriter writer = PdfWriter.GetInstance(pdfDoc, stream);
            pdfDoc.Open();
            XMLWorkerHelper.GetInstance().ParseXHtml(writer, pdfDoc, sr);
            pdfDoc.Close();
            return File(stream.ToArray(), "application/pdf", "Airgreenland-vouchers.pdf");
        }
    }

如果用户想要3张优惠券,它将输出3“sr”表格,可以打印。

此致 Erneeraq

2 个答案:

答案 0 :(得分:0)

使用StringBuilder并多次插入相同的字符串,然后在StringReader中使用它:

var numberOfVouchers = 3;
string voucherTable = String.Format(@"<table><tbody><tr><td>GL:{0}
                </td></ tr >< tr >
                < td > Service:{1}
                </ td ></ tr >< tr >
                < td > Dato:{2}
                </ td ></ tr >< tr >
                < td > Pris:{3}
                </ td ></ tr >< tr >
                < td > Kommentar:{4}
                </ td ></ tr><tr>
                <td> ID:{5}
                </ td ></ tr ></ tbody ></ table >", 
                model.GL, 
                model.Service, 
                d.ToString("dd-MM-yyyy"),
                model.Pris, 
                model.Kommentar, 
                c.ToString()
                );


StringReader sr = new StringReader(
     new StringBuilder().Insert(0, voucherTable, numberOfVouchers).ToString()
);

答案 1 :(得分:0)

喜欢这个?

public FileResult Edit(SpiseKopunTable model)
    {
        bool b = repository.PutSpiseKopun(model);
        DateTime d = new DateTime(1980, 1, 1);
        DateTime.TryParse(model.Dato.ToString(), out d);
        int c = new int();
        int.TryParse(model.ID.ToString(), out c);
        using (MemoryStream stream = new System.IO.MemoryStream())
        {
            var numberOfVouchers = 3;
            string voucherTable = String.Format(@"<table><tbody><tr><td>GL:{0}
            </td></ tr >< tr >
            < td > Service:{1}
            </ td ></ tr >< tr >
            < td > Dato:{2}
            </ td ></ tr >< tr >
            < td > Pris:{3}
            </ td ></ tr >< tr >
            < td > Kommentar:{4}
            </ td ></ tr><tr>
            <td> ID:{5}
            </ td ></ tr ></ tbody ></ table >",
                            model.GL,
                            model.Service,
                            d.ToString("dd-MM-yyyy"),
                            model.Pris,
                            model.Kommentar,
                            c.ToString()
                            );
            StringReader sr = new StringReader(
            new StringBuilder().Insert(0, voucherTable, numberOfVouchers).ToString());
            Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 100f, 0f);
            PdfWriter writer = PdfWriter.GetInstance(pdfDoc, stream);
            pdfDoc.Open();
            XMLWorkerHelper.GetInstance().ParseXHtml(writer, pdfDoc, sr);
            pdfDoc.Close();
            return File(stream.ToArray(), "application/pdf", "Airgreenland-vouchers.pdf");
        }

我得到“无法找到类型或命名空间名称'StringBuilder'”错误。

此致 Erneeraq