java itextsharp pdfpcell 标题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1491577/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 16:44:29  来源:igfitidea点击:

itextsharp pdfpcell header

c#javapdfpdf-generationitextsharp

提问by Trimack

I'd like to have a table of cells of class PdfPCell each with a tiny header, main string and tiny footer. I can't find a way to insert them since HeaderandFooter is not allowed element to add to the cell, one paragraph overwrites another and so on. Any ideas?

我想要一个 PdfPCell 类的单元格表,每个单元格都有一个小标题、主字符串和小页脚。我找不到插入它们的方法,因为 HeaderandFooter 不允许元素添加到单元格中,一个段落覆盖另一个段落等等。有任何想法吗?

Thanks in advance

提前致谢

回答by manji

You can use nested tables.
Instead of a PdfPCell, insert a 1x1 table with a tiny header & a tiny footer.

您可以使用嵌套表。
插入一个带有小页眉和小页脚的 1x1 表格,而不是 PdfPCell。

EDIT:

编辑:

let's forget about table footer and header feature of iTextSharp because it's useful when a table spans over multiple pages and then you have footer & header repeated. In our case, header & footer will belong to the inner table that will contain only 3 cells, so let's use PdfPCell for all of them.

让我们忘记 iTextSharp 的表格页脚和页眉功能,因为当表格跨越多个页面然后重复页脚和页眉时,它很有用。在我们的例子中,页眉和页脚将属于只包含 3 个单元格的内表,所以让我们对所有这些单元格使用 PdfPCell。

Then main function is GetHFCellthat will return a PdfPTable containing a customized header cell (height, font, text,..), a customized footer cell & a middle cell containing the main text. This function is called whenever we want to add a cell to our main table (example of how to use this function in GeneratePDF).

然后主要功能是GetHFCell将返回一个 PdfPTable 包含自定义标题单元格(高度,字体,文本,..),自定义页脚单元格和包含主要文本的中间单元格。每当我们想向主表格添加单元格时都会调用此函数(如何在 中使用此函数的示例GeneratePDF)。

    private static PdfPTable GetHFCell(string header, string footer, string text)
    {
        PdfPTable pdft;
        PdfPCell hc;
        PdfPCell fc;

        pdft = new PdfPTable(1);
        pdft.WidthPercentage = 100f;
        pdft.DefaultCell.Border = 0;

        hc = new PdfPCell(new Phrase(header));
        hc.Top = 0f;
        hc.FixedHeight = 7f;
        hc.HorizontalAlignment = 1;
        hc.BackgroundColor = iTextSharp.text.Color.ORANGE;
        ((Chunk)(hc.Phrase[0])).Font = new iTextSharp.text.Font(((Chunk)(hc.Phrase[0])).Font.Family, 5f);

        fc = new PdfPCell(new Phrase(footer));
        hc.Top = 0f;
        fc.FixedHeight = 7f;
        hc.HorizontalAlignment = 1;
        fc.BackgroundColor = iTextSharp.text.Color.YELLOW;
        ((Chunk)(fc.Phrase[0])).Font = new iTextSharp.text.Font(((Chunk)(fc.Phrase[0])).Font.Family, 5f);

        pdft.AddCell(hc);
        pdft.AddCell(text);
        pdft.AddCell(fc);

        return pdft;
    }

    public void GeneratePDF()
    {
        Document document = new Document();
        try
        {            
            PdfWriter.GetInstance(document, new FileStream("File1.pdf", FileMode.Create));

            document.Open();

            PdfPTable table = new PdfPTable(5);
            table.DefaultCell.Padding = 0;
            table.DefaultCell.BorderWidth = 2f;
            for (int j = 1; j < 6; j++)
            {
                for (int i = 1; i < 6; i++)
                {
                    //calling GetHFCell
                    table.AddCell(
                        GetHFCell("header " + ((int)(i + 5 * (j - 1))).ToString(), 
                                  "footer " + ((int)(i + 5 * (j - 1))).ToString(), 
                                  "z" + j.ToString() + i.ToString()));
                }
            }

            document.Add(table);
        }
        catch (DocumentException de)
        {
            //...
        }
        catch (IOException ioe)
        {
            //...
        }
        document.Close();
    }