ROWSPAN不能在iTextSharp的工作?能在、工作、ROWSPAN、iTextSharp

由网友(深情何须显摆)分享简介:我想将HTML转换为PDF。我使用iTextSharp的。我发现,iTextSharp的不支持CSS很好。逸岸,我认为HtmlWorker的主题不支持这一切。要复合我的问题iTextSharp的似乎并不支持ROWSPAN无论是。I am trying to convert Html to Pdf. I am usin...

我想将HTML转换为PDF。我使用iTextSharp的。我发现,iTextSharp的不支持CSS很好。逸岸,我认为HtmlWorker的主题不支持这一切。要复合我的问题iTextSharp的似乎并不支持ROWSPAN无论是。

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.

这是什么,我试图生成: http://jsbin.com/jovugohuju / 1 /编辑?HTML,输出

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(不必要的):

推荐答案

请看看下面的屏幕截图:

Please take a look at the following screen shot:

要在左边,你会看到在浏览器中呈现的HTML文件。在右边,可以看到HTML文件利用iText(Java版本)呈现为PDF。需要注意的是iTextSharp的关于HTML到PDF的功能等同于Java的,因此你不应该张贴问题说的中不iTextSharp的工作的,因为这听起来好像iTextSharp的不能达到你想要的做的(这是不正确的说法),而实际的问题是由你写你的code时,提出了一些个人的错误。这是不友善的指责的工具为自己的错误; - )

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:

您的HTML是没有意义的。我不得不把它清理干净(变化&LT; BR&GT; &LT; BR /&GT; ,介绍正确的CSS ,纠正列数了一些行,...),并把它变成XHTML之前,在浏览器中正确呈现。你可以发现,在这里的截图中使用的HTML: table2_css.html 您正在使用 HTMLWorker 而不是XML的工作人员,你是对的: HTMLWorker 已经对CSS的支持。说CSS不能在iTextSharp的工作是错误的。当您使用它不能正常工作 HTMLWorker ,但是这证明:CSS的,你需要在XML中,工人的作品 您可能正在使用旧版本的iTextSharp的,而你是对的:CSS和表支持是不如旧版本iTextSharp的时相比,最新版本的。 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 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. 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.

请参见 XML工人官方iText的网站获取更多信息。除了iTextSharp的,你还需要下载 XML工人。这些例子都用Java编写的,但你应该没有问题,将其转换为C#。这个例子我用来做PDF的屏幕截图( html_table_4.pdf )可以在这里找到: ParseHtmlTable4

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();
}
阅读全文

相关推荐

最新文章