如何设置在MVC4剃刀iTextSharp的PDF创建单元格的宽度 [英] How to set the cell width in itextsharp pdf creation in MVC4 Razor

查看:185
本文介绍了如何设置在MVC4剃刀iTextSharp的PDF创建单元格的宽度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面IM尝试使用iTextSharp的PDF创建设置表单元格的宽度,但面临的麻烦即时通讯使用HTML表作为字符串,使其因为即时通讯

Here im try to set a table cell width using itextsharp pdf creation but im facing trouble to make it since im using html table as string

我的code:

  public ActionResult FormSixteen(EmployeeFormSixteenModel objEmployeeFormSixteenModel)
    {
        string htmlTable = string.Empty;
        htmlTable = htmlTable + "<table><tr><td>S.no</td><td>Head Name</td>Amount<td></td></tr><tr><td>1</td><td>Gross Salary</td>xxxx<td></td></tr></table>";
        Document document = new Document();
        Response.ContentType = "application/pdf";
        Response.AddHeader("content-disposition", "attachment;filename=" + objEmployeeFormSixteenModel.EmployeeName + ".pdf");
        Response.Cache.SetCacheability(HttpCacheability.NoCache);
        document.Open();

        iTextSharp.text.html.simpleparser.StyleSheet styles = new iTextSharp.text.html.simpleparser.StyleSheet();
        iTextSharp.text.html.simpleparser.HTMLWorker hw = new iTextSharp.text.html.simpleparser.HTMLWorker(document);
        htmlTable = htmlTable.ToString().Replace("'", "\"");
        htmlTable = htmlTable.Replace("px", "");
        StringReader sr = new StringReader(htmlTable.ToString());
        Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 0f);
        HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
        PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
        pdfDoc.Open();``
        htmlparser.Parse(sr);
        pdfDoc.Close();
        return View("FormSixteen", objEmployeeFormSixteenModel);
    }

我应该怎么办?..设置宽度即S.no减小宽度尺寸,请帮助我。

What should I do?.. to set width i.e. S.no to decrease the width size , please help me.

推荐答案

基本的回答你的问题是,你只需要提供一些实际的宽度到您的HTML。

The basic answer to your question is that you just need to give some actual widths to your HTML.

htmlTable = @"<table>
               <tr>
                <td width=""25%"">S.no</td>
                <td width=""50%"">Head Name</td>
                <td width=""25%"">Amount</td>
               </tr>
               <tr>
                <td>1</td>
                <td>Gross Salary</td>
                <td>xxxx</td>
               </tr>
              </table>";

不过您code有一堆的其他问题。首先,你所使用的,虽然它仍然存在,它一直是德$ P $赞成 XMLWorker HTMLWorker $ C>。这往往是因为在4.1.6的相关性,如果你使用的是其中做的,我强烈建议您升级到5.x的 HTMLWorker 处理几乎为零CSS尽管类,看起来像它可能做到这一点,但是如果你有它的工作原理pretty好还是非常基本的HTML。我的code以下假设你已经有了最新的iTextSharp的,但我仍然使用 HTMLWorker 因为你的HTML是非常基本的。

However your code has a bunch of other problems. First, you are using HTMLWorker which although it still exists it has been deprecated a long time ago in favor of XMLWorker. This is often done because of a dependency upon 4.1.6 which if you are using I strongly recommend you upgrade to 5.x. HTMLWorker handles almost zero CSS despite a class that looks like it might do it, however if you've got very basic HTML it works pretty good still. My code below assumes you've got the latest iTextSharp but I'm still using HTMLWorker since your HTML is very basic.

二,你这是对象的多个实例,如文件 HTMLWorker ,但没有使用的全部它们。

Second, you are making multiple instances of objects such as Document and HTMLWorker but aren't using all of them.

三,你的HTML有之间的内容&LT; / TD&GT;&LT; TD&GT; 在一些地方,但也许这只是一个抄写错误

Third, your HTML has content between </td><td> in some places, however maybe that was just a transcription error.

四,我强烈推荐的从不直接写入 Response.OutputStream 。这可能会导致调试问题,如果任何异常抛出,特别是因为iTextSharp的是100%不知道ASP.Net,因此它不能处理任何特殊情况。我也是直到你有信心,你有再presents为同一上述原因一个PDF一些建议修改HTTP标头。 (它真的有趣,当你的异常开始一个PDF文件名下载,但真正的HTML文件!的),相反,我建议写入的MemoryStream ,抢夺字节,然后做与他们一些东西,我的code一样。

Fourth, I strongly recommend NEVER writing directly to the Response.OutputStream. This can cause debugging problems if any exceptions are thrown, especially since iTextSharp is 100% unaware of ASP.Net so it can't handle any special cases. I also recommend not changing the HTTP headers until you are confident that you have something that represents a PDF for the same above reasons. (Its really fun when your exceptions start downloading with a PDF file name but are really HTML files!) Instead I recommend writing to a MemoryStream, grabbing the bytes and then doing something with them which my code does.

最后,这是不完全的,但许多iTextSharp的5.x的支持的主要对象的IDisposable 因此鼓励您使用<$ C的一个问题$ C>对其使用模式。

Lastly, and this isn't a problem exactly, but many of the main objects in iTextSharp 5.x support IDisposable so you are encouraged to use the using pattern on them.

下面示出上述关闭。我不能帮你实际返回查看... 的一部分,但我可以给你一个字节数组,你应该能够做一些事情。在Web表单世界中,我们通常会做一个 Response.BinaryWrite(字节); (与所有的HTTP标头的工作,太)

Below shows the above off. I can't help you with the actual return View... part but I can get you a byte array that you should be able to do something with. In web form world we would normally do a Response.BinaryWrite(bytes); (with all of your HTTP header work, too).

//HTML Work
string htmlTable = @"<table>
                      <tr>
                       <td width=""25%"">S.no</td>
                       <td width=""50%"">Head Name</td>
                       <td width=""25%"">Amount</td>
                      </tr>
                      <tr>
                       <td>1</td>
                       <td>Gross Salary</td>
                       <td>xxxx</td>
                      </tr>
                     </table>";

//Our final PDF will be stores in this later
byte[] bytes;

//Create our PDF
using (var ms = new MemoryStream()) {
    using (var document = new Document()) {
        using (var writer = PdfWriter.GetInstance(document, ms)) {
            document.Open();

            //Bind a stream to our string
            using (var sr = new StringReader(htmlTable)) {
                //Bind a parser to our document
                using (var htmlparser = new HTMLWorker(document)) {
                    //Parse the HTML into iTextSharp objects and place them directly into the documenht
                    htmlparser.Parse(sr);
                }
            }

            document.Close();
        }
    }

    //Grab our finished PDF as a byte array
    bytes = ms.ToArray();
}

//At this point, the variable bytes holds an array of bytes that represents a valid PDF

这篇关于如何设置在MVC4剃刀iTextSharp的PDF创建单元格的宽度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆