首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >作为iTextSharp中的标头的PdfPTable

作为iTextSharp中的标头的PdfPTable
EN

Stack Overflow用户
提问于 2010-02-24 04:37:46
回答 3查看 55.3K关注 0票数 5

我需要一个有大约12个单元格的表格作为标题显示。下面的代码无法做到这一点。我知道table2没有12个细胞。在第二个页面上,只显示"testing“。我遗漏了什么?

提前感谢!

代码语言:javascript
运行
复制
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();
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2010-02-24 21:15:08

对啰!PdfPageEventHandler类对我很有效。

我使用了覆盖OnEndPage方法的PdfPageEventHelper:

代码语言:javascript
运行
复制
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);
    }
}

然后,构建pdf。

代码语言:javascript
运行
复制
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();
票数 11
EN

Stack Overflow用户

发布于 2014-03-18 23:38:08

当创建一个包含'n‘行的PdfPTable时,我使用了一种非常简单的方法在每个页面上添加一个标题行,其中'n’可以是任意整数。我使用的是iTextSharp 4.0.4.0。

因此,您可以创建一个包含1000行、2行或5000行的表,但是iTextSharp会自动为您对输出进行分页。iTextSharp不会做的是在每个页面的开头自动添加一个标题。

为了给每一页添加一个标题行,我所做的就是在创建包含所有行和单元格的PdfPTable之后添加一行代码,然后iTextSharp自动为输出添加一个标题行。‘'table’是我在设置其HeaderRows属性之前在代码中创建的PdfPTable实例。

代码语言:javascript
运行
复制
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.
票数 2
EN

Stack Overflow用户

发布于 2011-08-17 04:31:08

如果您有多个页面,这将不起作用。如果有多个页面,则需要使用OnStartPage事件而不是onEndPage事件。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/2321526

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档