0

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?

Bruno Lowagie
  • 75,994
  • 9
  • 109
  • 165
Marc
  • 2,023
  • 4
  • 16
  • 30

2 Answers2

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.

Bruno Lowagie
  • 75,994
  • 9
  • 109
  • 165
  • That is the solution I was looking for, thanks. The PageEvent thing is not very intuitive, In the c# version of the library it's a property and not a method so one expects to be holding a reference of a IPdfPageEvent instance – Marc Mar 12 '13 at 01:08
0

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.

Th0rndike
  • 3,406
  • 3
  • 22
  • 42
  • Thats exacly what I did the problem is that If I want to apply a generic watermark I would like to apply CustomFooter and CustomWatermark to the same document, both clases ideally would be applied to other documents as well – Marc Mar 06 '13 at 15:21
  • Why can't you create a method for each... lets call it document part? – Th0rndike Mar 06 '13 at 15:40