Sha*_*der 38 .net c# itextsharp
如何使用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());
Run Code Online (Sandbox Code Playgroud)
我是否需要为单个单元格指定无边框,或者我可以为表格本身指定无边框.
谢谢
Bru*_*gie 67
虽然我对Martijn的回答表示赞同,但我想补充说明.
只有单元格在iText中有边框; 桌子没有边框.Martijn建议将默认单元格的边框设置NO_BORDER为正确:
table.DefaultCell.Border = Rectangle.NO_BORDER;
Run Code Online (Sandbox Code Playgroud)
但它不适用于问题中提供的代码段.仅当iText需要PdfPCell隐式创建实例时才使用默认单元格的属性(例如:如果使用addCell()传递Phraseas参数的方法).
在@Brown_Dynamite提供的代码段中,PdfPCell显式创建了对象.这意味着您需要将每个单元格的边框设置为NO_BORDER.
通常,我写一个工厂类来创建单元格.这样,我可以显着减少创建表的类中的代码量.
这应该做的伎俩:
table.DefaultCell.Border = Rectangle.NO_BORDER;
Run Code Online (Sandbox Code Playgroud)
要么
table.borderwidth= 0;
Run Code Online (Sandbox Code Playgroud)
首先,我们可以将所有单元格边框设置为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);
Run Code Online (Sandbox Code Playgroud)
在这里,我们可以将表添加到一个单元格,并可以设置边框,并再次将该单元格添加到另一个表格,我们可以根据我们的要求使用.
如果您的 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);
Run Code Online (Sandbox Code Playgroud)
我花了很长时间才弄清楚这一点。现在对我有用。
| 归档时间: |
|
| 查看次数: |
77206 次 |
| 最近记录: |