Using Events
Events can be used on Layout Elements when specialized behavior is required. Two common examples of this are using a placeholder to add a barcode or image to a detail section of a report based on the current layout data. This is done by retrieving a layout element from the document layout and attaching an event handler to its LayingOut or LaidOut event.
The following code example demonstrates how to specify and code an event handler for the LaidOut event in order to dynamically place a barcode in a PlaceHolder:
public void ReportExample()
{
// Create the document's layout from a DLEX template
DocumentLayout layout = new DocumentLayout("Placeholder.dlex");
// Retrieve the place holder and attach the event handler
PlaceHolder barcodePlaceHolder = (PlaceHolder)layout.GetElementById("BarcodePlaceholder");
barcodePlaceHolder.LaidOut += new PlaceHolderLaidOutEventHandler(BarcodePlaceHolder_LaidOut);
// Specify the data to be used
NameValueLayoutData layoutData = new NameValueLayoutData();
layoutData.Add("PreparedFor", "Alex Smith");
layoutData.Add("Orders", listOfOrders);
// Layout the document and save the PDF
Document document = layout.Layout(layoutData);
document.Draw(outputFilePath);
}
public void BarcodePlaceHolder_LaidOut(object sender, PlaceHolderLaidOutEventArgs e)
{
Code128 barcode = new Code128(e.LayoutWriter.Data["OrderID"].ToString(), 0, 0, e.ContentArea.Height);
barcode.X += (e.ContentArea.Width - barcode.GetSymbolWidth()) / 2;
barcode.ShowText = false;
e.ContentArea.Add(barcode);
}
Public Sub void PlaceHolderExample()
' Create the document's layout from a DLEX template
Dim MyLayout As DocumentLayout = New DocumentLayout("Placeholder.dlex")
' Retrieve the place holder And attach the event handler
Dim MyBarcodePlaceHolder As PlaceHolder = MyLayout.GetElementById("BarcodePlaceholder")
AddHandler MyBarcodePlaceHolder.LaidOut, AddressOf BarcodePlaceholder_LaidOut
' Specify the data to be used
Dim MyLayoutData As NameValueLayoutData = New NameValueLayoutData()
MyLayoutData.Add("PreparedFor", "Alex Smith")
MyLayoutData.Add("Orders", MyListOfOrders)
' Layout the document And save the PDF
Dim MyDocument As Document = MyLayout.Layout(MyLayoutData)
MyDocument.Draw(outputFilePath)
End Sub
Private Sub BarcodePlaceholder_LaidOut(ByVal sender As Object, ByVal e As PlaceHolderLaidOutEventArgs)
Dim MyBarcode As Code128 = New Code128(e.LayoutWriter.Data("OrderID").ToString(), 0, 0, e.ContentArea.Height)
MyBarcode.X += (e.ContentArea.Width - MyBarcode.GetSymbolWidth()) / 2
MyBarcode.ShowText = False
e.ContentArea.Add(MyBarcode)
End Sub