为什么表的第二行不会被写入? [英] Why the 2nd row of table won't be written?

查看:113
本文介绍了为什么表的第二行不会被写入?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想无国界创建表4列和两行对PDF文档的顶部。问题是,在第2行不会被写入。这是我的代码:

I'm trying to create table without borders with 4 columns and 2 rows on top of my PDF document. The problem is that the 2nd row won't be written. This is my code:

float[] columnWidths = { 2, 1, 1, 1};
PdfPTable table = new PdfPTable(columnWidths);
table.WidthPercentage = 100;
if (...) //true
{
       if (...) //true
       {
                PdfPCell p = new PdfPCell(new Phrase("AAA:_______________",infoFont));    
                p.BorderWidth = 0;  
                table.AddCell(p);  // fixed pos. 1st col,1st row
       }
       if (...) //true
       {
                PdfPCell p = new PdfPCell(new Phrase("BBB:_____", infoFont));   
                p.BorderWidth = 0;
                table.AddCell(p);   // fixed pos. 2nd col,1st row
       }
       if (...) //true
       {
                PdfPCell p = new PdfPCell(new Phrase("CCC:_____", infoFont));    
                p.BorderWidth = 0;
                table.AddCell(p);   // fixed pos. 3rd col,1st row
       }
       if (...) //true
       {
                PdfPCell p = new PdfPCell(new Phrase("DDD:_____", infoFont));      
                p.BorderWidth = 0;
                table.AddCell(p);   // fixed pos. 4th col,1st row
       }
}
if (...) //true
{
       if (...) //true
       {
                PdfPCell p = new PdfPCell(new Phrase("EEE: " + eee));
                p.BorderWidth = 0;
                table.AddCell(p);   // fixed pos. 1st col,2nd row
       }
       if (...) //true
       {
                PdfPCell p = new PdfPCell(new Phrase("FFF: " + fff));
                p.BorderWidth = 0;
                table.AddCell(p);   // fixed pos. 2nd col,2nd row
       }
       if (...) //true
       {
                PdfPCell p = new PdfPCell(new Phrase("GGG: " + ggg));
                p.BorderWidth = 0;
                table.AddCell(p);   // fixed pos. 3rd col,2nd row
       }
       if (...) //true
       {
                PdfPCell p = new PdfPCell(new Phrase("HHH:___________________"));
                p.BorderWidth = 0;
                table.AddCell(p);   // fixed pos. 4th col,2nd row
       }
}

document.Add(table);



我如何面对呢?而第二个问题:我能有固定的位置,如果每一个条件(检查代码中的注释),所以当第一排一个,如果条件不是真的那么细胞sholud是空的?
谢谢你。

How can I deal with this? And the 2nd question: can I have fixed position for every if condition (check the comments in the code) so when one if-condition in first row is not true then that cell sholud be empty? Thank you.

推荐答案

我假设你已经简化的程度你的代码,您共享的代码片断是不再用自己的代码是一致的。您正在创建4个列的表。如果添加了4个单元,一排会被渲染。如果添加了8个单元,两行被渲染。但是:如果只添加7个细胞,然后单行将被添加。 3细胞不完整的行会被省略,因为iText的只呈现完整的行。

I assume that you've simplified your code to the extent that the snippet you share is no longer consistent with your own code. You are creating a table with 4 columns. If you add 4 cells, one row will be rendered. If you add 8 cells, two rows will be rendered. However: if you only add 7 cells, then a single row will be added. The 3 cells in the incomplete row will be omitted because iText only renders complete rows.

又见的如何生成PDF,如果我们的列小于decalred表列 iTextSharp的,细胞不受长度可分割的数量该行奇数细胞不会被添加到全文并的 PdfTable:最后一个单元格是不可见的并...

See also How to generate pdf if our column less than the decalred table column and ItextSharp, number of Cells not dividable by the length of the row and Odd Numbered Cell Not Added To Pdf and PdfTable: last cell is not visible and ...

这解释了为什么不会显示在第二行。添加以下行,看看是否能解决问题:

This explains why the second row isn't shown. Add the following line to see if this fixes the problem:

table.CompleteRow();



至于你的其他问题:你可以随时添加一个空单元格是这样的:

As for your other question: you can always add an empty cell like this:

PdfPCell cell = new PdfPCell();
if (someCondition) {
    cell.addElement(new Paragraph("AAA"));
} 
table.addCell(cell);



最后,在你的代码中的另一个错误。这没有任何意义:

Finally, there's another error in your code. This doesn't make any sense:

p.BorderWidth = 0;



0边框宽度并不意味着无边框将被显示。如前所述多次ISO-32000-1定义了0宽度的线,其中所述宽度等于可由设备显示的最小宽度的线。如果你不希望任何边框使用:

A border width of 0 doesn't mean that no border will be shown. As explained many times before ISO-32000-1 defines a line with 0 width as a line of which the width is equal to the minimal width that can be displayed by the device. If you don't want any border use:

p.Border = PdfPCell.NO_BORDER;



最后,我要问你一个忙:我们已经重新设计了iText的网站,我们发布它在感恩节。现在,我们注意到当我们用来改变之前,我们没有得到尽可能多的访问。鉴于所有你所需要的信息可以在在线文档中找到,因为你还需要问的问题的事实,我们想知道什么是错的网站。有什么我们可以做的改进内容?可能是什么驱使人们从我们的网站走的原因是什么?你为什么要问这个是官方文档中已经回答了这么多问题?难道我们现在有太多的内容?

Finally, I need to ask you a favor: we've redesigned the iText web site and we released it on Thanksgiving. We now notice that we don't get as many visits as we used to before the change. Given the fact that all the information you needed can be found on the online documentation and given the fact that you still needed to ask the question, we'd like to know what is wrong with the web site. Is there something we can do to improve the content? What could be the reason that drives people away from our web site? Why are you asking so many questions that are already answered in the official documentation? Do we have too much content now?

这篇关于为什么表的第二行不会被写入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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