如何使用iTextSharp在页面开头的绝对位置添加表格?

时间:2014-06-06 13:45:28

标签: c# pdf pdf-generation itextsharp itext

我有一个名为 PdfHeaderFooter 的类,它扩展了 PdfPageEventHelper 类。

在本课程中,我以这种方式实现了 OnEndPage()方法。如何看待此方法会为文档的每个页面创建标题页脚

现在我必须在页面开头的绝对位置添加 PdfPTable tabHead

我该怎么办?我疯狂地试图去做它

    // write on end of each page
    public override void OnEndPage(PdfWriter writer, Document document)
    {
        base.OnEndPage(writer, document);

        // HEADER:
        PdfPTable tabHead = new PdfPTable(3);
        tabHead.SetWidths(new int[] { 165, 205, 125 });


        //tabHead.TotalWidth = 460F;
        tabHead.TotalWidth = document.Right - document.Left;        // TotalWidth = 495
        tabHead.WidthPercentage = 98;


        PdfPCell cell1 = new PdfPCell(iTextSharp.text.Image.GetInstance(folderImages + "logoEarlyWarning.png"), true) { Border = PdfPCell.BOTTOM_BORDER };
        tabHead.AddCell(cell1);
        //tabHead.AddCell(new PdfPCell(new Phrase("CELL 1:")) { Border = PdfPCell.BOTTOM_BORDER, Padding = 5, MinimumHeight = 50, PaddingTop = 15, });

        tabHead.AddCell(new PdfPCell(new Phrase("")) { Border = PdfPCell.BOTTOM_BORDER, Padding = 5, MinimumHeight = 50, PaddingTop = 15 });

        if (_sourceId == "NVD")
        {
            iTextSharp.text.Image logo = iTextSharp.text.Image.GetInstance(folderImages + "nvdLogo.png");
            logo.ScalePercent(48f);
            //PdfPCell cell3 = new PdfPCell(iTextSharp.text.Image.GetInstance(folderImages + "nvdLogo.png"), true) { Border = PdfPCell.BOTTOM_BORDER, PaddingBottom = 25 };
            PdfPCell cell3 = new PdfPCell(logo) { Border = PdfPCell.BOTTOM_BORDER, PaddingLeft = 50 };
            tabHead.AddCell(cell3);
        }
        else if (_sourceId == "DeepSight")
        {
            PdfPCell cell3 = new PdfPCell(iTextSharp.text.Image.GetInstance(folderImages + "DSLogo.jpg"), true) { Border = PdfPCell.BOTTOM_BORDER };
            tabHead.AddCell(cell3);
        }
        //tabHead.AddCell(new PdfPCell(new Phrase("CELL 3:")) { Border = PdfPCell.BOTTOM_BORDER, Padding = 5, MinimumHeight = 50, PaddingTop = 15 });


        tabHead.WriteSelectedRows(0, -1, document.Left, document.Top, writer.DirectContent);

        float headerHeight = tabHead.CalculateHeights();


        // FOOTER:
        int pageN = writer.PageNumber;      // numero della pagina corrente OK
        String text = "Page " + pageN + " of ";
        float len = bf.GetWidthPoint(text, 8);

        Rectangle pageSize = document.PageSize;

        cb.SetRGBColorFill(100, 100, 100);

        cb.BeginText();
        cb.SetFontAndSize(bf, 8);
        cb.SetTextMatrix(pageSize.GetLeft(40), pageSize.GetBottom(30));
        cb.ShowText(text);
        cb.EndText();

        cb.AddTemplate(template, pageSize.GetLeft(40) + len, pageSize.GetBottom(30));

        cb.BeginText();
        cb.SetFontAndSize(bf, 8);
        cb.ShowTextAligned(PdfContentByte.ALIGN_RIGHT,
            "Printed On " + PrintTime,
            pageSize.GetRight(40),
            pageSize.GetBottom(30), 0);
        cb.EndText();

    }

TNX

1 个答案:

答案 0 :(得分:0)

此问题已在此处得到解答:How to handle the case in wich an iText\iTextSharp table is splitted in two pages?

您正在页面开头添加标题,并考虑了保证金。您想在边距内添加表格。因此,您需要将表格的高度添加到上边距的Y坐标。

例如:如果表的高度为h,则需要:

tabHead.WriteSelectedRows(0, -1, document.Left, document.Top + h, writer.DirectContent);

如果你不读人的答案,提问的重点是什么?你还在生成一个臃肿的PDF文件!解决了!