C# PdfPTable 作为 iTextSharp 中的标题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2321526/
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
PdfPTable as a header in iTextSharp
提问by David Fox
I need a table with about 12 cells to display as a header. The following code fails to do this. I am aware table2 does not have 12 cells. On the second page, only "testing" is displayed. What am I missing?
我需要一个包含大约 12 个单元格的表格作为标题显示。以下代码无法执行此操作。我知道 table2 没有 12 个单元格。在第二页上,只显示“测试”。我错过了什么?
Thanks in advance!
提前致谢!
Document document = new Document();
try
{
PdfWriter.GetInstance(document, new FileStream("TableTest.pdf", FileMode.Create));
document.Open();
PdfPTable table = new PdfPTable(1);
table.WidthPercentage = 100;
PdfPTable table2 = new PdfPTable(2);
//logo
PdfPCell cell2 = new PdfPCell(Image.GetInstance(@"C:\path\to\file.gif"));
cell2.Colspan = 2;
table2.AddCell(cell2);
//title
cell2 = new PdfPCell(new Phrase("\nTITLE TEXT", new Font(Font.HELVETICA, 16, Font.BOLD | Font.UNDERLINE)));
cell2.HorizontalAlignment = Element.ALIGN_CENTER;
cell2.Colspan = 2;
table2.AddCell(cell2);
PdfPCell cell = new PdfPCell(table2);
table.HeaderRows = 1;
table.AddCell(cell);
table.AddCell(new PdfPCell(new Phrase("")));
document.Add(table);
document.Add(new Phrase("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\ntesting"));
}
catch (DocumentException de)
{
Console.Error.WriteLine(de.Message);
}
catch (IOException ioe)
{
Console.Error.WriteLine(ioe.Message);
}
document.Close();
采纳答案by David Fox
Bingo! PdfPageEventHandler class worked for me.
答对了!PdfPageEventHandler 类对我有用。
I used the PdfPageEventHelper overriding the OnEndPage method:
我使用 PdfPageEventHelper 覆盖 OnEndPage 方法:
class _events : PdfPageEventHelper
{
public override void OnEndPage(PdfWriter writer, Document document)
{
PdfPTable table = new PdfPTable(1);
//table.WidthPercentage = 100; //PdfPTable.writeselectedrows below didn't like this
table.TotalWidth = document.PageSize.Width - document.LeftMargin - document.RightMargin; //this centers [table]
PdfPTable table2 = new PdfPTable(2);
//logo
PdfPCell cell2 = new PdfPCell(Image.GetInstance(@"C:\path\to\file.gif"));
cell2.Colspan = 2;
table2.AddCell(cell2);
//title
cell2 = new PdfPCell(new Phrase("\nTITLE", new Font(Font.HELVETICA, 16, Font.BOLD | Font.UNDERLINE)));
cell2.HorizontalAlignment = Element.ALIGN_CENTER;
cell2.Colspan = 2;
table2.AddCell(cell2);
PdfPCell cell = new PdfPCell(table2);
table.AddCell(cell);
table.WriteSelectedRows(0, -1, document.LeftMargin, document.PageSize.Height - 36, writer.DirectContent);
}
}
then, build pdf
然后,构建pdf
Document document = new Document(PageSize.A4, 36, 36, 36 + <height of table>, 36); // note height should be set here
_events e = new _events();
PdfWriter pw = PdfWriter.GetInstance(document, new FileStream("TableTest.pdf", FileMode.Create));
pw.PageEvent = e;
document.Open();
for(int i=0;i<100;i++)
{
document.Add(new Phrase("TESTING\n"));
}
document.Close();
回答by Adam
This will not work if you have more than one page. If you have more than one page, then you need to use the OnStartPage event instead of the onEndPage event.
如果您有多于一页,这将不起作用。如果您有多个页面,则需要使用 OnStartPage 事件而不是 onEndPage 事件。
回答by Sunil
I used a a very simple approach to add a header row on each page, when creating a PdfPTable with 'n' rows, where 'n' could any integer. I am using iTextSharp 4.0.4.0.
我使用一种非常简单的方法在每个页面上添加一个标题行,当创建一个带有 'n' 行的 PdfPTable 时,其中 'n' 可以是任何整数。我正在使用 iTextSharp 4.0.4.0。
So, you could create a table with 1000 rows or 2 rows or 5000 rows, but iTextSharp will automatically page the output for you. What iTextSharp will not do is automatically add a header to start of every page.
因此,您可以创建一个 1000 行或 2 行或 5000 行的表格,但 iTextSharp 会自动为您分页输出。iTextSharp 不会自动在每个页面的开头添加标题。
In order to add a header row for every page, all I did was add the single line of code after creating the PdfPTable with all its rows and cells, and then iTextSharp automatically paged the output with a header row. 'table' is the instance of PdfPTable that I created in my code before setting its HeaderRows property.
为了为每个页面添加标题行,我所做的只是在创建包含所有行和单元格的 PdfPTable 后添加一行代码,然后 iTextSharp 自动使用标题行对输出进行分页。'table' 是我在设置其 HeaderRows 属性之前在代码中创建的 PdfPTable 实例。
table.HeaderRows = 1; //this will automatically insert a header row
//at start of every page. The first row you created
//in your code will be used as the header row in this case.