如何处理至极的iText\iTextSharp表在两页分裂的情况下? [英] How to handle the case in wich an iText\iTextSharp table is splitted in two pages?

查看:1019
本文介绍了如何处理至极的iText\iTextSharp表在两页分裂的情况下?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用iTextSharp的以下问题。

I have the following problem using iTextSharp.

我把一些表到我的文档中。问题是,如果一个表中的内容不适合在页面上,进入另一个页面我有它覆盖第二页头,所以我有以下情况:

I am putting some tables into my document. The problem is that if a table content not fit on a page and go into another page I have that it overwrite the second page header, so I have the following situation:

正如你可以看到我我插入一个表格在页面的结束,这在两页分裂,第二页头是由表的内容覆盖。

As you can see I am inserting a table at the end of a page and this is splitted in two pages and the second page header is overwrite by the table content.

我想避免这种情况但是我不知道怎么有我做

I want to avoid this situation but I don't know how have I to do.

我想,也许我可以做以下的事情之一:

I am thinking that maybe I can do one of the following things:

1),也许我可以检查元素是否complitly进入一个页面。如果没有创建一个新的页面,并把它放到这个新页面(但是这可能是一个问题,如果一个表需要多于一个页面的情况下,如果我有一个非常大的表)

1) Maybe I can check if an element complitly enter into a page. If not create a new page and put it into this new page (but this can be a problem if a single table need more then one page, in the case if I have a very big table)

2)我允许一个表2页分裂但在这种情况下,我在上的第二页的留有一定余量间距所以头被正确地显示。

2) I allow that a table is splitted in 2 pages but in this case I left some margin spacing in the upper of the second page so the header is correctly shown.

或者,我可以做些什么来解决这个情况是什么

Or what can I do to solve this situation?

TNX

编辑:

我在下面的方法添加页眉:

I have add the header in the following way:

1)我有实现创建一个名为类<强> PdfHeaderFooter 扩展在 PdfPageEventHelper 类和实现它的方法。

1) I have implement create a class named PdfHeaderFooter that extends the PdfPageEventHelper class and that implements its methods.

这时它的类包含以下方法:

At this time its class contains the following methods:

    // write on start of each page
    public override void OnStartPage(PdfWriter writer, Document document)
    {
        base.OnStartPage(writer, document);
        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);
    }

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

        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();
    }



因此​​广告可以看到,在的OnStartPage()方法在每个页面,并在的OnEndPage()是在每个页面的末尾添加页脚。

So ad you can see, the OnStartPage() method add the header at the beginning of each pages and the OnEndPage() add the footer at the end of each pages.

因此,从一开始添加页眉我从你的答复明白我必须做以下步骤:

So from what I understand from your response I have to do the followings steps:

1)Moove头插入的OnStartPage()的OnEndPage()

1) Moove the header insertion from OnStartPage() to OnEndPage()

2)使用绝对位置将其放置在页面的上半部分。

2) Use an absolut position for place it in the upper part of the pages.

3)在创建文档使用的标题heigt设置上边距。

3) In the document creation use the header heigt to set the top margin.

是不是?

编辑2:

我tryied做,因为你对我说,现在我有以下几种情况:

I tryied to do as you say to me and now I have the following situations:

1)在我的 PdfHeaderFooter 扩展 PdfPageEventHelper 我删除了的OnStartPage()方式

1) Into my PdfHeaderFooter that extends PdfPageEventHelper I have deleted the OnStartPage() method

2)我有moove头表创建到的OnEndPage()方式,现在就这一个:

2) I have moove the header table creation into the OnEndPage() method that now it this one:

    // 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();
    }



正如你现在可以看到的OnEndPage() 。方法包含页眉和页脚创建

As you can see now the OnEndPage() method contains the header and footer creation.

在我看来,那 tabHead (我的头表)使用绝对定位,因为我有:

It seems to me that tabHead (my header table) use the absolute positioning because I have:

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

在这里我创建文档,其他类我有这行:

In the other class where I create the document I have this line:

document = new iTextSharp.text.Document(iTextSharp.text.PageSize.A4, 50, 50, 30, 65);



所以页面是A4和已经30像素的上缘。

so the pages are A4 and have 30px as upper margin.

但我仍然有同样的问题。

But I still have the same problem.

如果我更改了价值30与80这只是向下移动头部,留下白色顶部,但不解决问题。

If I change the 30 value with 80 it simply moves down the header leaving a white top but does not solve the problem.

我在想什么?什么是错的?

What am I missing? What is wrong?

推荐答案

我假设你正确添加页眉。那就是:你已经实现了在一个页面事件的OnEndPage()法(的的的的OnStartPage()法),你在一个绝对位置添加标题。当你在一个绝对位置添加标题,你知道它究竟占用多少空间。

I assume that you are adding page headers correctly. That is: you have implemented the onEndPage() method in a page event (not the onStartPage() method) and you're adding the header at an absolute position. As you are adding the header at an absolute position, you know exactly how much space it takes.

当你创建一个文档对象,你定义页面大小。如果不这样做,将页面大小为A4(595点¯x842用户单位)。还可以定义利润。如果你不这样做,利润将是半寸两边(36个用户单位)。

When you create a document object, you define a page size. If you don't, the page size will be A4 (595 x 842 user units). You also define margins. If you don't, the margins will be half an inch (36 user units) on either side.

当一个表拆分,页面大小和利润率都得到尊重:iText的会不会把在这方面的表的任何部分。

When a table splits, the page size and its margins are respected: iText won't put any part of the table in that area.

因此,解决方法很简单:你也知道了头所需的空间,所有你所,做的是定义在头适合保证金的方式保证金

Hence the solution is simple: as you know the space needed by the header, all you have to do is to define the margin in a way that the header fits into the margin.

的问题后更新进行了更新:

1

这是错误的:

// write on start of each page
public override void OnStartPage(PdfWriter writer, Document document)
{
    ...
    tabHead.WriteSelectedRows(0, -1, document.Left, document.Top, writer.DirectContent);
}

您永远不应该添加任何内容的OnStartPage( )方法。移动写入标题到的OnEndPage()方法的代码。

You should never add any content in the OnStartPage() method. Move the code that writes the header to the OnEndPage() method.

2

您已经在使用一个绝对位置添加 tabHead :你是在坐标 X =文件添加。剩下; Y = document.Top

You are already using an absolute position to add tabHead: you are adding it at the coordinate x = document.left; y = document.Top.

您定义为在绝对位置添加一个文件的宽度百分比。这没有任何意义,删除以下行:

You define a width percentage for a file that is added at an absolute position. That doesn't make any sense, remove the following line:

tabHead.WidthPercentage = 98;



不过,你是在浪费资源。例如:在新建页面事件的标志: iTextSharp.text.Image.GetInstance(folderImages +nvdLogo.png)。这意味着你要添加的图像字节的多次,你的页面的,从而在PDF文件中的冗余信息(你有一个臃肿的文件)。

However, you are wasting resources. For instance: you create the logo in the page event: iTextSharp.text.Image.GetInstance(folderImages + "nvdLogo.png"). This means that you are adding the image bytes as many times as you have pages, resulting in redundant info in the PDF file (you have a bloated file).

您可以通过创建的OnEndPage()方法外的图像优化处理。你甚至可以创建方法表之外。例如:创建在事件类的成员变量 tabHead 键,无论是在你的事件实现的构造或 OnOpenDocument()创​​建表方法(该方法只被调用一次)。重用在的OnEndPage()方法表(和图像)。

You can optimize the process by creating the images outside the onEndPage() method. You can even create the table outside that method. For instance: create a member variable tabHead in the event class and create the table either in the constructor of your event implementation or in the OnOpenDocument() method (that method only gets called once). Reuse the table (and the images) in the onEndPage() method.

现在的形象字节将出席在PDF文件中只有一次(你会获得大量的千字节的),你就只需要创建表一次(更少的CPU时间浪费)。

Now the image bytes will be present in the PDF file only once (you'll gain plenty of KBytes) and you'll only have to create the table once (less CPU-time wasted).

3

这是更好的解决方案是创建 tabHead 对象的 的外部页面事件和的的打开文档。当你定义一个总宽度,你可以问表的高度:

An even better solution is to create the tabHead object outside the page event and before you open the document. As you define a total width, you can ask the table for its height:

float h = tabHead.TotalHeight;

现在你可以使用 ^ h 来定义上边距:

Now you can use h to define the top margin:

document.SetMargins(36f, 36f, h, 36f);

请注意,它设置的打开文档前边缘的是非常重要的。你也得适应坐标其中添加表。例如:

Note that it is important to set the margins before you open your document. You'll also have to adapt the coordinate at which you add the table. For instance:

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

您对标题的位置评论透露了一个严重缺乏洞察力。

Your comment regarding the position of the header revealed a serious lack of insight.

这篇关于如何处理至极的iText\iTextSharp表在两页分裂的情况下?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆