C# 在 iTextSharp 中隐藏表格边框
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17980291/
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
Hiding table border in iTextSharp
提问by SharpCoder
How can i hide the table border using iTextSharp. I am using following code to generate a file:
如何使用 iTextSharp 隐藏表格边框。我正在使用以下代码生成文件:
var document = new Document(PageSize.A4, 50, 50, 25, 25);
// Create a new PdfWriter object, specifying the output stream
var output = new MemoryStream();
var writer = PdfWriter.GetInstance(document, output);
document.Open();
PdfPTable table = new PdfPTable(3);
var bodyFont = FontFactory.GetFont("Arial", 10, Font.NORMAL);
PdfPCell cell = new PdfPCell(new Phrase("Header spanning 3 columns"));
cell.Colspan = 3;
cell.HorizontalAlignment = 1; //0=Left, 1=Centre, 2=Right
table.AddCell(cell);
Font arial = FontFactory.GetFont("Arial", 6, BaseColor.BLUE);
cell = new PdfPCell(new Phrase("Font test is here ", arial));
cell.PaddingLeft = 5f;
cell.Colspan = 1;
table.AddCell(cell);
cell = new PdfPCell(new Phrase("XYX"));
cell.Colspan = 2;
table.AddCell(cell);
cell = new PdfPCell(new Phrase("Hello World"));
cell.PaddingLeft = 5f;
cell.Colspan = 1;
table.AddCell(cell);
cell = new PdfPCell(new Phrase("XYX"));
cell.Colspan = 2;
table.AddCell(cell);
table.SpacingBefore = 5f;
document.Add(table);
document.Close();
Response.ContentType = "application/pdf";
Response.AddHeader("Content-Disposition", "attachment;filename=Receipt-test.pdf");
Response.BinaryWrite(output.ToArray());
Do I need to specify no borders for individual cells or I can specify no borders for table itself.
我是否需要为单个单元格指定没有边框,或者我可以为表格本身指定没有边框。
Thank you
谢谢
采纳答案by Bruno Lowagie
Although I upvoted the answer by Martijn, I want to add a clarification.
虽然我赞成 Martijn 的回答,但我想补充说明。
Only cells have borders in iText; tables don't have a border. Martijn's suggestion to set the border of the default cell to NO_BORDER
is correct:
iText 中只有单元格有边框;桌子没有边框。Martijn 建议将默认单元格的边框设置NO_BORDER
为正确:
table.DefaultCell.Border = Rectangle.NO_BORDER;
But it won't work for the code snippet provided in the question. The properties of the default cell are only used if iText needs to create a PdfPCell
instance implicitly (for instance: if you use the addCell()
method passing a Phrase
as parameter).
但它不适用于问题中提供的代码片段。默认单元格的属性仅在 iText 需要PdfPCell
隐式创建实例时使用(例如:如果您使用addCell()
传递 aPhrase
作为参数的方法)。
In the code snippet provided by @Brown_Dynamite, the PdfPCell
objects are created explicitly. This means that you need to set the border of each of these cells to NO_BORDER
.
在@Brown_Dynamite 提供的代码片段中,PdfPCell
对象是显式创建的。这意味着您需要将每个单元格的边框设置为NO_BORDER
.
Usually, I write a factory class to create cells. That way, I can significantly reduce the amount of code in the class that creates the table.
通常,我编写一个工厂类来创建单元格。这样,我可以显着减少创建表的类中的代码量。
回答by Martijn van Put
This should do the trick:
这应该可以解决问题:
table.DefaultCell.Border = Rectangle.NO_BORDER;
or
或者
table.borderwidth= 0;
回答by Jom George
First we can set all cell borders as 0 and After assigning all cell to table we can use the following code for for only pdfptable outer border.
首先,我们可以将所有单元格边框设置为 0,在将所有单元格分配给表格后,我们可以仅将以下代码用于 pdfptable 外边框。
PdfPCell cell = new PdfPCell();
cell.AddElement(t);
cell.BorderWidthBottom=1f;
cell.BorderWidthLeft=1f;
cell.BorderWidthTop=1f;
cell.BorderWidthRight = 1f;
PdfPTable t1 = new PdfPTable(1);
t1.AddCell(cell);
Here we can add table to one cell and can set border and again add that cell to another table and we can use as per our requirement.
在这里,我们可以将表格添加到一个单元格,可以设置边框,然后再次将该单元格添加到另一个表格,我们可以根据需要使用。
回答by Mehmet Taha Meral
PdfPTable table = new PdfPTable(4);
table.TotalWidth = 400f;
table.LockedWidth = true;
PdfPCell header = new PdfPCell(new Phrase("Header"));
header.Colspan = 4;
table.AddCell(header);
table.AddCell("Cell 1");
table.AddCell("Cell 2");
table.AddCell("Cell 3");
table.AddCell("Cell 4");
PdfPTable nested = new PdfPTable(1);
nested.AddCell("Nested Row 1");
nested.AddCell("Nested Row 2");
nested.AddCell("Nested Row 3");
PdfPCell nesthousing = new PdfPCell(nested);
nesthousing.Padding = 0f;
table.AddCell(nesthousing);
PdfPCell bottom = new PdfPCell(new Phrase("bottom"));
bottom.Colspan = 3;
table.AddCell(bottom);
doc.Add(table);
PdfPTable table = new PdfPTable(3);
table.TotalWidth = 144f;
table.LockedWidth = true;
table.HorizontalAlignment = 0;
PdfPCell left = new PdfPCell(new Paragraph("Rotated"));
left.Rotation = 90;
table.AddCell(left);
PdfPCell middle = new PdfPCell(new Paragraph("Rotated"));
middle.Rotation = -90;
table.AddCell(middle);
table.AddCell("Not Rotated");
doc.Add(table);
Check this linkpls
检查此链接请
回答by farhan farhan
try this code
试试这个代码
yourtable.DefaultCell.Border = 0;
回答by David Lee
If your PdfPTable is nested within another PdfPTable, the nested table will show table borders. The only way to get rid of the table borders is to put the nested PdfPTable into a PdfPCell of the main PdfPTable and set the border width of that cell to 0.
如果您的 PdfPTable 嵌套在另一个 PdfPTable 中,则嵌套表格将显示表格边框。摆脱表格边框的唯一方法是将嵌套的 PdfPTable 放入主 PdfPTable 的 PdfPCell 并将该单元格的边框宽度设置为 0。
iTextSharp.text.pdf.PdfPTable table = new iTextSharp.text.pdf.PdfPTable(1); //<-- Main table
table.TotalWidth = 540f;
table.LockedWidth = true;
float[] widths = new float[] { 540f };
table.SetWidths(widths);
table.HorizontalAlignment = 1; //0=Left, 1=Centre, 2=Right
table.SpacingAfter = 10;
PdfPTable bodyTable = new PdfPTable(1); //<--Nested Table
bodyTable.TotalWidth = 540f;
bodyTable.LockedWidth = true;
float[] bodyWidths = new float[] { 540f };
bodyTable.SetWidths(bodyWidths);
bodyTable.HorizontalAlignment = 0;
bodyTable.SpacingAfter = 10;
bodyTable.DefaultCell.Border = iTextSharp.text.Rectangle.NO_BORDER;
var para1 = new Paragraph("This is a long paragraph", blackNormal);
para1.SetLeading(3f, 1f);
PdfPCell bodyCell1 = new PdfPCell();
bodyCell1.AddElement(para1);
bodyCell1.Border = 0;
bodyTable.AddCell(bodyCell1);
iTextSharp.text.pdf.PdfPCell cellBody = new iTextSharp.text.pdf.PdfPCell(bodyTable);
cellBody.BorderWidth = 0; //<--- This is what sets the border for the nested table
table.AddCell(cellBody);
Took me a long time to figure this out. It works for me now.
我花了很长时间才弄清楚这一点。它现在对我有用。