java iText 5 getDefaultCell().setBorder(PdfPCell.NO_BORDER) 没有效果
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27212695/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
iText 5 getDefaultCell().setBorder(PdfPCell.NO_BORDER) has no effect
提问by Mathias Mahlknecht
I'm new with iTextand I'm trying to build a table.
But for some reason table.getDefaultCell().setBorder(PdfPCell.NO_BORDER)
has no effect, my table has still borders.
我是iText 的新手,我正在尝试构建一个表格。但是由于某种原因table.getDefaultCell().setBorder(PdfPCell.NO_BORDER)
没有效果,我的桌子仍然有边框。
Here is my code:
这是我的代码:
PdfPTable table = new PdfPTable(new float[] { 1, 1, 1, 1, 1 });
table.getDefaultCell().setBorder(PdfPCell.NO_BORDER);
Font tfont = new Font(Font.FontFamily.UNDEFINED, 10, Font.BOLD);
table.setWidthPercentage(100);
PdfPCell cell;
cell = new PdfPCell(new Phrase("Menge", tfont));
table.addCell(cell);
cell = new PdfPCell(new Phrase("Beschreibung", tfont));
table.addCell(cell);
cell = new PdfPCell(new Phrase("Einzelpreis", tfont));
table.addCell(cell);
cell = new PdfPCell(new Phrase("Gesamtpreis", tfont));
table.addCell(cell);
cell = new PdfPCell(new Phrase("MwSt", tfont));
table.addCell(cell);
document.add(table);
Do you have any idea what I am doing wrong?
你知道我做错了什么吗?
回答by Bruno Lowagie
You are mixing two different concepts.
您正在混合两个不同的概念。
Concept 1:you define every PdfPCell
manually, for instance:
概念 1:您PdfPCell
手动定义每个,例如:
PdfPCell cell = new PdfPCell(new Phrase("Menge", tfont));
cell.setBorder(Rectangle.NO_BORDER);
table.addCell(cell);
In this case, you define every aspect, every property of the cell on the cell itself.
在这种情况下,您可以在单元格本身上定义单元格的每个方面、每个属性。
Concept 2:you allow iText to create the PdfPCell
implicitly, for instance:
概念 2:您允许 iTextPdfPCell
隐式创建,例如:
table.addCell("Adding a String");
table.addCell(new Phrase("Adding a phrase"));
In this case, you can define properties at the level of the default cell. These properties will be used internally when iText creates a PdfPCell
in your place.
在这种情况下,您可以在默认单元格级别定义属性。当 iTextPdfPCell
在您的位置创建 a 时,这些属性将在内部使用。
Conclusion:
结论:
Either you define the border for all the PdfPCell
instances separately, or you let iText create the PdfPCell
instances in which case you can define the border at the level of the default cell.
您可以PdfPCell
分别为所有实例定义边框,或者让 iText 创建PdfPCell
实例,在这种情况下,您可以在默认单元格的级别定义边框。
If you choose the second option, you can adapt your code like this:
如果您选择第二个选项,您可以像这样调整您的代码:
PdfPTable table = new PdfPTable(new float[] { 1, 1, 1, 1, 1 });
table.getDefaultCell().setBorder(PdfPCell.NO_BORDER);
Font tfont = new Font(Font.FontFamily.UNDEFINED, 10, Font.BOLD);
table.setWidthPercentage(100);
table.addCell(new Phrase("Menge", tfont));
table.addCell(new Phrase("Beschreibung", tfont));
table.addCell(new Phrase("Einzelpreis", tfont));
table.addCell(new Phrase("Gesamtpreis", tfont));
table.addCell(new Phrase("MwSt", tfont));
document.add(table);
This decision was made by design, based on experience: it offers the most flexible to work with cells and properties.
这个决定是根据经验设计的:它提供了最灵活的处理单元和属性的方法。
回答by Priya
cell.setBorder(Rectangle.NO_BORDER);
cell.setBorder(Rectangle.NO_BORDER);
you have to set NO_BORDER
on cell.
你必须NO_BORDER
在单元格上设置。
e.g.
例如
cell = new PdfPCell(new Phrase("Menge", tfont));
cell.setBorder(Rectangle.NO_BORDER);
table.addCell(cell);
cell = new PdfPCell(new Phrase("Menge", tfont));
cell.setBorder(Rectangle.NO_BORDER);
table.addCell(cell);
回答by Kseniya Yudina
For all cell in Table:
对于表中的所有单元格:
table.AddCell(new Phrase(DepInfo.ElementAt(0), font));
table.AddCell(iText.Image.GetInstance(t));
table.AddCell(third);
foreach (var c in table.Rows.SelectMany(r => r.GetCells()))
c.Border = PdfPCell.NO_BORDER;
回答by ErmannoS
If you want to create a new PdfPCell inheriting all the settings of the default table cell, you should use code like this:
如果你想创建一个新的 PdfPCell 继承默认表格单元格的所有设置,你应该使用这样的代码:
PdfPTable table = new PdfPTable(new float[] { 1, 1, 1, 1, 1 });
table.getDefaultCell().setBorder(PdfPCell.NO_BORDER);
Font tfont = new Font(Font.FontFamily.UNDEFINED, 10, Font.BOLD);
table.setWidthPercentage(100);
PdfPCell cell = new PdfPCell(table.getDefaultCell());
cell.setPhrase(new PdfPCell(new Phrase("Menge", tfont)));
table.addCell(cell);
document.add(table);