actions events in itext 7
In iText 7, the @event annotations you might be seeing are not global variables. Instead, they are typically referring to event handlers related to document processing, such as handling page events, table events, and paragraph events. These events allow customization of how a document is generated.
🔹 Where Do These @event Appear in iText 7?
In Java and C# (iText7 .NET), event handling in iText 7 is implemented using interfaces such as:
1. IEventHandler → The main event interface.
2. PdfDocumentEvent → Used to handle document-level events.
3. TableEvent → For handling table modifications.
4. ParagraphRenderer → For customizing paragraph rendering.
These events are not global, but rather tied to specific elements in the document.
---
🔹 Example: Handling Page Events in iText 7
1️⃣ Implementing a Custom Page Event Handler
In C# (iText 7 .NET), you can create a class that implements IEventHandler to modify the pages.
using System;
using iText.Kernel.Pdf;
using iText.Kernel.Events;
using iText.Kernel.Pdf.Canvas;
using iText.Kernel.Pdf.Canvas.Draw;
using iText.Kernel.Geom;
using iText.Kernel.Colors;
class CustomPageEventHandler : IEventHandler
{
public void HandleEvent(Event @event)
{
PdfDocumentEvent docEvent = (PdfDocumentEvent)@event;
PdfDocument pdfDoc = docEvent.GetDocument();
PdfPage page = docEvent.GetPage();
PdfCanvas canvas = new PdfCanvas(page.NewContentStreamBefore(), page.GetResources(), pdfDoc);
canvas.SetStrokeColor(ColorConstants.RED);
canvas.SetLineWidth(2);
canvas.MoveTo(50, 50);
canvas.LineTo(550, 50);
canvas.Stroke();
canvas.Release();
}
}
---
2️⃣ Applying the Event to a Document
using System;
using iText.Kernel.Pdf;
using iText.Layout;
using iText.Layout.Element;
class Program
{
static void Main()
{
string outputFile = "output.pdf";
PdfWriter writer = new PdfWriter(outputFile);
PdfDocument pdf = new PdfDocument(writer);
Document doc = new Document(pdf);
// Add the event handler
pdf.AddEventHandler(PdfDocumentEvent.END_PAGE, new CustomPageEventHandler());
// Add some content
doc.Add(new Paragraph("Hello, iText7 with Events!"));
doc.Close();
Console.WriteLine("PDF Created: " + outputFile);
}
}
✅ This adds a red line at the bottom of every page dynamically.
---
🔹 Other Types of Events in iText 7
1️⃣ Table Event Handling
Handles table-specific modifications, like coloring cells dynamically.
class CustomTableEvent : IEventHandler
{
public void HandleEvent(Event @event)
{
TableEvent tableEvent = (TableEvent)@event;
// Custom table modifications here
}
}
2️⃣ Document-Level Event
For handling document-specific changes like adding headers/footers.
pdf.AddEventHandler(PdfDocumentEvent.END_PAGE, new CustomPageEventHandler());
---
🔹 Key Takeaways
✔ @event is not a global variable but an instance of Event.
✔ It is used inside event handlers to process PDF elements dynamically.
✔ The main interface to implement is IEventHandler.
✔ Common events: Page Events, Table Events, Document Events.
Would you like an example of table event handling or header/footer customization?
In iText 7, the @ symbol is used mainly in C# attributes or Java annotations. However, its usage depends on the programming language:
1. In C# (@ for Reserved Keywords and Attributes)
@event is used when event is a reserved keyword in C#.
Other @ prefixed words in C# are used for escaping reserved keywords (e.g., @class, @namespace).
Used for .NET attributes (e.g., [Obsolete], [Serializable]).
2. In Java (@ for Annotations)
iText 7 uses Java annotations like @Override, @FunctionalInterface, etc.
iText-specific annotations:
@Tag (used for accessibility tagging in PDFs)
@Deprecated (marks deprecated iText methods)
@Test (for unit testing)
---
🔹 Other @ Usages in iText 7 (C# & Java)
---
🔹 Example: @Tag in Java (iText 7 Accessibility)
@Tag("H1")
public class CustomHeading extends Paragraph {
public CustomHeading(String text) {
super(text);
}
}
☑ This tags a paragraph as an H1 header for screen readers.
---
🔹 Example: [Obsolete] in C#
[Obsolete("Use NewMethod() instead")]
public void OldMethod() { }
public void NewMethod() { }
☑ This warns developers not to use OldMethod().
---
🔹 Key Takeaways
✔ C# uses @event to escape reserved keywords (not a special iText feature).
✔ Java uses @Tag, @Deprecated, @Override in iText.
✔ C# uses [Obsolete], [Serializable], [Fact] instead of Java annotations.
Would you like specific examples of any iText 7 @ features?
Comments
Post a Comment