How to introduce multiple PdfPageEventHelper instances?

蓝咒 提交于 2019-12-13 07:22:06

问题


I have to generate a large amount of different types of documents using the itextsharp library, all have things in common, some have common headers, page counts, watermarks my initial tought was to have different PdfPageEventHelper subclasses for example WatermarkPdfPageEventHelper , OrderHeaderPdfPageEventHelper , PageNumberPdfPageEventHelper etc and apply them when necessary to compose the documents but PageEvent is not really an event but an instance of only one IPdfEvent , what is the correct way to implement this?


回答1:


I'm the author of the Java Version of iText. My answer may not be applicable to the C# port.

Page events can be cumulative:

writer.setPageEvent(watermarkevent);
writer.setPageEvent(headerevent);
writer.setPageEvent(footerevent);

Internally, a PdfPageEventForwarder is created. This object will make sure that each event is triggered in the order you added them.

When you want to remove the events, you just need to do this:

writer.setPageEvent(null);

In your case, you could create your own PdfPageEventForwarder instances, creating different combinations of page events.

I'm pretty sure you can do the same thing in iTextSharp although there may be slight differences in the class and method names.




回答2:


Here's what I did when having the need to create a custom footer that was common to different documents. This footer is just a multiline footer.

First, create your custom footer:

public class CustomFooter : PdfPageEventHelper
{
    private string[] _Phrases;
    private Font _CustomFooterFont;


    public string[] phrases
    {
        get { return _Phrases; }
        set { _Phrases = value; }
    }

    public Font customFont {
        get { return _CustomFooterFont; }
        set { _CustomFooterFont = value; }
    }



    public override void OnEndPage(PdfWriter writer, iTextSharp.text.Document document)
    {
        base.OnEndPage(writer, document);

        iTextSharp.text.Rectangle rect = writer.GetBoxSize("footer");

        for (int i = 0; i < this.phrases.Length; i++)
        {
            Phrase currentPhrase;
            currentPhrase = new Phrase(_Phrases[i], _CustomFooterFont);

            ColumnText.ShowTextAligned(writer.DirectContent,
                iTextSharp.text.Element.ALIGN_LEFT, currentPhrase,
                rect.Left, rect.Bottom +20 - (i * 9), 0);
        }
    }

    }

When needing one of these footers, i would call this function:

    public static CustomFooter getMultilineFooter(string[] Phrases)
    {
        CustomFooter result = new CustomFooter();

        result.phrases = Phrases;
        result.customFont = myCustomFooterFont;
        result.customBoldFont = myCustomFooterBoldFont;

        return result;        }

after that, just add to the document. Hope this helps.



来源:https://stackoverflow.com/questions/15250459/how-to-introduce-multiple-pdfpageeventhelper-instances

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!