PdfPTable as a header in iTextSharp(PdfPTable 作为 iTextSharp 中的标题)
问题描述
我需要一个包含大约 12 个单元格的表格来显示为标题.以下代码无法做到这一点.我知道 table2 没有 12 个单元格.在第二页上,只显示测试".我错过了什么?
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?
提前致谢!
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 ofile.gif"));
cell2.Colspan = 2;
table2.AddCell(cell2);
//title
cell2 = new PdfPCell(new Phrase("
TITLE 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("
testing"));
}
catch (DocumentException de)
{
Console.Error.WriteLine(de.Message);
}
catch (IOException ioe)
{
Console.Error.WriteLine(ioe.Message);
}
document.Close();
推荐答案
Bingo!PdfPageEventHandler 类对我有用.
Bingo! PdfPageEventHandler class worked for me.
我使用 PdfPageEventHelper 覆盖 OnEndPage 方法:
I used the PdfPageEventHelper overriding the OnEndPage method:
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 ofile.gif"));
cell2.Colspan = 2;
table2.AddCell(cell2);
//title
cell2 = new PdfPCell(new Phrase("
TITLE", 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);
}
}
然后,构建 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
"));
}
document.Close();
这篇关于PdfPTable 作为 iTextSharp 中的标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:PdfPTable 作为 iTextSharp 中的标题


- 为什么 C# 中的堆栈大小正好是 1 MB? 2022-01-01
- Windows 喜欢在 LINUX 中使用 MONO 进行服务开发? 2022-01-01
- 使用 rss + c# 2022-01-01
- CanBeNull和ReSharper-将其用于异步任务? 2022-01-01
- Azure Active Directory 与 MVC,客户端和资源标识同一 2022-01-01
- 在 C# 中异步处理项目队列 2022-01-01
- 是否可以在 .Net 3.5 中进行通用控件? 2022-01-01
- C# 通过连接字符串检索正确的 DbConnection 对象 2022-01-01
- 在 LINQ to SQL 中使用 contains() 2022-01-01
- 带问号的 nvarchar 列结果 2022-01-01