PDF for WinRT
DrawingElement Event
Example 


Occurs when the C1PdfDocument is about to render a Windows.UI.Xaml.FrameworkElement.
Syntax
'Declaration
 
Public Event DrawingElement As EventHandler(Of DrawingElementEventArgs)
'Usage
 
Dim instance As C1PdfDocument
Dim handler As EventHandler(Of DrawingElementEventArgs)
 
AddHandler instance.DrawingElement, handler
public event EventHandler<DrawingElementEventArgs> DrawingElement
Event Data

The event handler receives an argument of type DrawingElementEventArgs containing data related to this event. The following DrawingElementEventArgs properties provide information specific to this event.

PropertyDescription
BoundsGets the coordinates, in logical pixels, of the element being rendered.  
DocumentTransformGets the Windows.UI.Xaml.Media.Transform used to convert coordinates between logical pixels and PDF page units (points).  
ElementGets a reference to the element being rendered.  
HandledGets or sets a value that determines whether the event handler has rendered the element.  
RootGets a reference to the root of the element being rendered.  
Remarks

This event fires once for each Windows.UI.Xaml.FrameworkElement rendered after calls to the DrawElement(FrameworkElement,Rect,Rect) method. The event allows you to override the built-in rendering behavior and to render custom content for specific elements.

If you handle this event, then remember to set the DrawingElementEventArgs.Handled property to true so the control will not render the element over your custom content.

Example

The example below shows how you can use the DrawingElement event to provide custom rendering for elements of type 'RichTextBox'. The code checks the type of element being rendered. If the element is a 'RichTextBox', the code creates a Windows.UI.Xaml.Media.Imaging.WriteableBitmap to represent the object, then renders the image into the PDF document.

This example is quite general. You can use it to render any elements that do not expose their child elements as primitives such as Windows.UI.Xaml.Controls.TextBlock, Windows.UI.Xaml.Controls.Border, Windows.UI.Xaml.Shapes.Rectangle, etc.

The drawback associated with rendering elements as images is that the output will contain raster images, which can be quite large and may look rough when you zoom in on the PDF.

// create C1PdfDocument
var pdf = new C1PdfDocument(PaperKind.Letter);
            
// use DrawingElement event to render RichTextBox elements
pdf.DrawingElement += (s, e) =>
  {
    if (e.Element is RichTextBox)
    {
      // get element image
      #if SILVERLIGHT
      var bmp = new WriteableBitmap(e.Element, e.DocumentTransform);
      #else // WPF
      var sz = e.Element.RenderSize;
      var rtBmp = new RenderTargetBitmap((int)sz.Width, (int)sz.Height, 96, 96, PixelFormats.Pbgra32);
      rtBmp.Render(e.Element);
      var bmp = new WriteableBitmap(rtBmp);
      #endif
      
      // render it into the document
      pdf.DrawImage(bmp, e.Bounds);
      
      // done rendering this element
      e.Handled = true;
    }
  };
  
// render LayoutRoot element into C1PdfDocument
var rc = pdf.PageRectangle;
rc.Inflate(-20, -20);
pdf.DrawElement(LayoutRoot, rc, ContentAlignment.TopLeft, Stretch.None);
Requirements

Target Platforms: Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

See Also

Reference

C1PdfDocument Class
C1PdfDocument Members

 

 


Copyright (c) GrapeCity, inc. All rights reserved.

Product Support Forum  |  Documentation Feedback