[]
Occurs when the C1WordDocument is about to render a FrameworkElement.
public event EventHandler<DrawingElementEventArgs> DrawingElement
Type | Description |
---|---|
EventHandler<DrawingElementEventArgs> | Occurs when the is about to render a . |
<p>This event fires once for each <xref href="System.Windows.FrameworkElement" data-throw-if-not-resolved="false"></xref> 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 Handled property to true so the control will not render the element over your custom content.
<p>The example below shows how you can use the <xref href="C1.WPF.Word.C1WordDocument.DrawingElement" data-throw-if-not-resolved="false"></xref> 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 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 TextBlock, Border, 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.
<pre><code class="lang-csharp">// create C1WordDocument
var rtf = new C1WordDocument(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
rtf.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);</code></pre>