隐藏表格边框的iTextSharp的 [英] Hiding table border in iTextSharp

查看:745
本文介绍了隐藏表格边框的iTextSharp的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用iTextSharp的如何隐藏表格边框。我使用下面的code生成文件:

How can i hide the table border using iTextSharp. I am using following code to generate a file:

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.

感谢您

推荐答案

虽然我upvoted通过的Martijn答案,我想补充说明。

Although I upvoted the answer by Martijn, I want to add a clarification.

只有细胞有边界的iText的;表不具有边框。马亭的建议,默认的单元格的边框设置为 NO_BORDER 是正确的:

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:

table.DefaultCell.Border = Rectangle.NO_BORDER;

但它不会为这个问题提供的code段工作。默认的单元格的属性只能应用在iText的需要隐式地创建一个 PdfPCell 实例(例如:如果您使用 addCell()方法传递短语作为参数)。

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).

在由@Brown_Dynamite,提供的code段的 PdfPCell 对象明确创建的。这意味着,你需要每个细胞的边界设置为 NO_BORDER

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.

通常情况下,我写了一个工厂类来创建细胞。这样一来,我可以显著减少创建表之类的code量。

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.

这篇关于隐藏表格边框的iTextSharp的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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