-1

Can someone explain what are iTextEvents in ItextSharp.. I have found a bunch of codes with its use but i dont get how they works.. Im asking you if anyone can explain me these:

OnOpenDocument

OnEndPage

OnCloseDocument
Pradeep
  • 9,667
  • 13
  • 27
  • 34
  • "I have found a bunch of codes" sounds like you're trying to learn about iText from code samples instead of from tutorials. It's as if you didn't go to [the official web site](https://itextpdf.com) and didn't read the [free books](https://developers.itextpdf.com/books), such as the [iText 7 jump-start tutorial](https://developers.itextpdf.com/content/itext-7-jump-start-tutorial-net/itext-7-jump-start-tutorial-net-version). If you had read any of those books, you wouldn't be looking at an old iText version, and you wouldn't have to ask the question. Do you want to learn? Read the tutorials! – Bruno Lowagie Apr 16 '18 at 08:55
  • code snippet in code format – Pradeep Apr 16 '18 at 09:01

1 Answers1

0

If you are looking to start using iText, then your question is obsolete. You are referring to a concept that was used in versions 5 and earlier of iText for .NET (we abandoned the name iTextSharp a long time ago). If you want to start using iText for .NET, you should start with version 7, not with iText 5 or earlier, because we stopped development on those versions. Any release we make now is nothing more than a maintenance release (maintenance releases don't contain new functionality, they have bug fixes that are meant for paying users who can't migrate to iText 7 immediately).

The name page events was misleading because those events were initially used to allow developers to execute code when a new page was created or finalized, but as the code grew organically, we also started to use the page event functionality for other things, such as: to add special behavior of a Chunk (OnGenericTag()) or to execute code before or after adding a Paragraph. That was an example of bad design.

We fixed this bad design in iText 7, where we introduced renderers and event handlers. See chapter 3 of the jump-start tutorial, entitled Using renderers and event handlers.

In iText 7, we can create an event handler such as:

protected internal class MyEventHandler : IEventHandler {
    public virtual void HandleEvent(Event @event) {
        PdfDocumentEvent docEvent = (PdfDocumentEvent)@event;
        PdfDocument pdfDoc = docEvent.GetDocument();
        PdfPage page = docEvent.GetPage();
        int pageNumber = pdfDoc.GetPageNumber(page);
        Rectangle pageSize = page.GetPageSize();
        PdfCanvas pdfCanvas = new PdfCanvas(page.NewContentStreamBefore(), page.GetResources(), pdfDoc);
        //Set background
        Color limeColor = new DeviceCmyk(0.208f, 0, 0.584f, 0);
        Color blueColor = new DeviceCmyk(0.445f, 0.0546f, 0, 0.0667f);
        pdfCanvas.SaveState()
                    .SetFillColor(pageNumber % 2 == 1 ? limeColor : blueColor)
                    .Rectangle(pageSize.GetLeft(), pageSize.GetBottom(), pageSize.GetWidth(), pageSize.GetHeight())
                    .Fill()
                    .RestoreState();
        //Add header and footer
        pdfCanvas.BeginText()
                    .SetFontAndSize(C03E03_UFO.helvetica, 9)
                    .MoveText(pageSize.GetWidth() / 2 - 60, pageSize.GetTop() - 20)
                    .ShowText("THE TRUTH IS OUT THERE")
                    .MoveText(60, -pageSize.GetTop() + 30)
                    .ShowText(pageNumber.ToString())
                    .EndText();
        //Add watermark
        iText.Layout.Canvas canvas = new iText.Layout.Canvas(pdfCanvas, pdfDoc, page.GetPageSize());
        canvas.SetProperty(Property.FONT_COLOR, Color.WHITE);
        canvas.SetProperty(Property.FONT_SIZE, 60);
        canvas.SetProperty(Property.FONT, C03E03_UFO.helveticaBold);
        canvas.ShowTextAligned(new Paragraph("CONFIDENTIAL"), 298, 421, pdfDoc.GetPageNumber(page), TextAlignment.
            CENTER, VerticalAlignment.MIDDLE, 45);
        pdfCanvas.Release();
    }
}

The event handler is introduced in the code like this:

PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
pdf.AddEventHandler(PdfDocumentEvent.END_PAGE, new C03E03_UFO.MyEventHandler(this));
// Initialize document
Document document = new Document(pdf);
Paragraph p = new Paragraph("List of reported UFO sightings in 20th century").SetTextAlignment(TextAlignment
    .CENTER).SetFont(helveticaBold).SetFontSize(14);
document.Add(p);
Table table = new Table(new float[] { 3, 5, 7, 4 });
table.SetWidth(UnitValue.CreatePercentValue(100));
StreamReader sr = File.OpenText(DATA);
String line = sr.ReadLine();
Process(table, line, helveticaBold, true);
while ((line = sr.ReadLine()) != null) {
    Process(table, line, helvetica, false);
}
sr.Close();
document.Add(table);
document.Close();

This code adds a background, a watermark, a header, and a footer, as is shown in this figure:

enter image description here

Page events in "iTextSharp" had a similar purpose, but you shouldn't use them anymore. They are outdated. You should use iText 7 instead.

If you posted your question out of historical curiosity, you should search old questions on Stack Overflow, such as:

Bruno Lowagie
  • 75,994
  • 9
  • 109
  • 165