RowSpan 在 iTextSharp 中不起作用? [英] RowSpan does not work in iTextSharp?

查看:27
本文介绍了RowSpan 在 iTextSharp 中不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将 Html 转换为 PDF.我正在使用 iTextSharp.我发现 iTextSharp 不能很好地支持 CSS.事实上,我认为 HtmlWorker 线程并不支持这一切.为了使我的问题更加复杂,iTextSharp 似乎也不支持 RowSpan.

这是我想要生成的:

  • 您使用的是 HTMLWorker 而不是 XML Worker,您是对的:HTMLWorker 不支持 CSS.说 CSS 在 iTextSharp 中不起作用是错误的.当您使用 HTMLWorker 时它不起作用,但已记录在案:您需要的 CSS 在 XML Worker 中有效.
  • 您可能使用的是旧版本的 iTextSharp,您是对的:与最新版本相比,CSS 和表格支持不如旧版本的 iTextSharp.
  • 有关详细信息,请参阅官方 iText 站点上的 XML Worker 页面.除了 iTextSharp,您还需要下载 XML Worker.这些示例是用Java 编写,但是将它们转换为C# 应该没有问题.我用来在屏幕截图中制作 PDF 的示例 (html_table_4.pdf) 可以在这里找到:ParseHtmlTable4

    public void createPdf(String file) 抛出 IOException, DocumentException {//步骤1文档文档 = 新文档();//第2步PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(file));//第 3 步文档.open();//步骤4XMLWorkerHelper.getInstance().parseXHtml(writer, document,新的 FileInputStream(HTML));//第 5 步文档.close();}

    I am trying to convert Html to PDF. I am using iTextSharp. I found that iTextSharp does not support CSS well. Infact I think HtmlWorker thread does not support it all. To compound my problem iTextSharp does not seem to support RowSpan either.

    This is what I am trying to generate: http://jsbin.com/jovugohuju/1/edit?html,output

    <table border="1" width="700">
        <tr>
                <td colspan="5" align="center" bgcolor="lightblue">INVOICE</td>
        </tr>
        <tr>
                <td colspan="2" rowspan="4" bgcolor="white"><b>AIRNET NETWORKS</b>
                        <br>asdadadadaada asd asd a ads adsadsadsadasd</td>
                <td>INVOICE</td>
                <td>DATE</td>
                <td>aDATEsd</td>
        </tr>
        <tr>
                <td>Order</td>
                <td>XXXX</td>
                <td>Ref XXXXXX</td>
        </tr>
        <tr>
                <td>Delivery</td>
                <td>XXXX</td>
                <td>Ref XXXXXX</td>
        </tr>
        <tr>
                <td>Due Date</td>
                <td>XXXX</td>
                <td>Ref XXXXXX</td>
        </tr>
        <tr>
                <td colspan="2" rowspan="4" bgcolor="white">
                        <p><b>CUSTOMER NAME</b>
                        </p>asd asd adadaadadadada adadaadsasdad ada asd adad</td>
        </tr>
        <tr>
                <td>Customer Care No:</td>
                <td colspan="2">544646454,88877978975</td>
        </tr>
        <tr>
                <td>Email Id</td>
                <td colspan="2">airnet@gmail.com</td>
        </tr>
        <tr>
                <td>Account Details</td>
                <td colspan="2">5522245125545455 IFSC 323hasd</br>SBI India</td>
        </tr>
    </table>
    
    <table border="1" width="700">
        <tr>
                <td bgcolor="lightblue" height="15">Srno</td>
                <td bgcolor="lightblue">Particulars</td>
                <td bgcolor="lightblue">Quantity</td>
                <td bgcolor="lightblue">Rate/Month</td>
                <td bgcolor="lightblue">Total Rupees</td>
                <tr>
                        <td valign="top">1</td>
                        <td valign="top">1 MBPS Plan</td>
                        <td valign="top">1</td>
                        <td valign="top">600</td>
                        <td valign="top">692</td>
                </tr>
        </tr>
        <tr>
                <td height="300" valign="top">1</td>
                <td valign="top">1 MBPS Plan</td>
                <td valign="top">1</td>
                <td valign="top">600</td>
                <td valign="top">692</td>
        </tr>
        <tr>
                <td colspan="3" rowspan="3" valign="top">asdasdasd</td>
                <td colspan="1">Total</td>
                <td colspan="1">692</td>
        </tr>
        <tr>
                <td>Service Tax</td>
                <td>692</td>
        </tr>
        <tr>
                <td>Grand Total</td>
                <td>692</td>
        </tr>
    </table>
    

    C# CODE:

    Document document = new Document();
    document.SetPageSize(iTextSharp.text.PageSize.A4);
    iTextSharp.text.pdf.draw.LineSeparator line1 = new iTextSharp.text.pdf.draw.LineSeparator(0f, 100f, iTextSharp.text.Color.BLACK, Element.ALIGN_LEFT, 1);    
    string NEWhtmlText="<table border='1' width='500' > <tr> <td bgcolor='lightblue' height='15' >Srno</td><td bgcolor='lightblue'>Particulars</td><td bgcolor='lightblue' >Quantity</td><td bgcolor='lightblue'>Rate/Month</td><td bgcolor='lightblue'>Total Rupees</td> </tr> <tr> <td valign='top' >1</td><td valign='top' >1 MBPS Plan</td><td valign='top'>1</td><td valign='top'>600</td><td valign='top'>692</td> </tr> <tr> <td height='300' valign='top' >1</td><td valign='top' >1 MBPS Plan</td><td valign='top'>1</td><td valign='top'>600</td><td valign='top'>692</td> </tr> <tr><td colspan='3' rowspan='3' valign='top'>asdasdasd</td><td colspan='1'>Total</td><td colspan='1'>692</td></tr> <tr><td>Service Tax</td><td>692</td></tr> <tr><td>Grand Total</td><td>692</td></tr> </table>";
    PdfWriter.GetInstance(document, new FileStream(saveFileDialog1.FileName, FileMode.Create));
    document.Open();
    iTextSharp.text.html.simpleparser.HTMLWorker hw = new iTextSharp.text.html.simpleparser.HTMLWorker(document);
    hw.Parse(new StringReader(NEWhtmlText));
    document.Close();
    

    OUTPUT(unwanted):

    解决方案

    Please take a look at the following screen shot:

    To the left, you see an HTML file rendered in a browser. To the right, you see that HTML file rendered to PDF using iText (the Java version). Note that the functionality of iTextSharp regarding HTML to PDF is identical to Java, hence you shouldn't post questions saying "does not work in iTextSharp" because that sounds as if iTextSharp can't achieve what you want to do (which is an incorrect allegation), whereas the actual problem is caused by some individual errors you made when writing your code. It is not friendly to blame a tool for your own errors ;-)

    There are three reasons why your application doesn't work:

    1. Your HTML doesn't make sense. I had to clean it up (change <br> into <br />, introduce the correct CSS, correct the column-count for some rows,...) and make it XHTML before it rendered correctly in a browser. You can find the HTML that was used in the screenshot here: table2_css.html
    2. You are using HTMLWorker instead of XML Worker, and you are right: HTMLWorker has no support for CSS. Saying CSS doesn't work in iTextSharp is wrong. It doesn't work when you use HTMLWorker, but that's documented: the CSS you need works in XML Worker.
    3. You are probably using an old version of iTextSharp, and you are right: CSS and table support wasn't as good as in older versions of iTextSharp when compared to the most recent version.

    See the XML Worker page on the official iText site for more info. Apart from iTextSharp, you also need to download XML Worker. The examples are written in Java, but you should have no problem converting them to C#. The example I used to make the PDF in the screen shot (html_table_4.pdf) can be found here: ParseHtmlTable4

    public void createPdf(String file) throws IOException, DocumentException {
        // step 1
        Document document = new Document();
        // step 2
        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(file));
        // step 3
        document.open();
        // step 4
        XMLWorkerHelper.getInstance().parseXHtml(writer, document,
                new FileInputStream(HTML));
        // step 5
        document.close();
    }
    

    这篇关于RowSpan 在 iTextSharp 中不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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