# C1.WPF.Word.C1WordDocument.DrawingElement

## Content

<div class="doc-site-dotnet-api-container">




<h1 id="C1_WPF_Word_C1WordDocument_DrawingElement" data-uid="C1.WPF.Word.C1WordDocument.DrawingElement" class="text-break">DrawingElement Event
</h1>
<div class="markdown level0 summary"><p>Occurs when the <a class="xref" href="C1.WPF.Word.C1WordDocument.html">C1WordDocument</a> is about to render a <a class="xref" href="https://learn.microsoft.com/dotnet/api/system.windows.frameworkelement">FrameworkElement</a>.</p>
</div>
<div class="markdown level0 conceptual"></div>
<h6><strong>Namespace</strong>: <a class="xref" href="C1.WPF.Word.html">C1.WPF.Word</a></h6>
<h6><strong>Assembly</strong>: C1.WPF.Word.4.6.2.dll</h6>
<h5 id="C1_WPF_Word_C1WordDocument_DrawingElement_syntax">Syntax</h5>
<div class="codewrapper">
  <pre><code class="lang-csharp hljs">public event EventHandler&lt;DrawingElementEventArgs&gt; DrawingElement</code></pre>
</div>
<h5 class="returns">Returns</h5>
<table class="table table-bordered table-condensed">
  <thead>
    <tr>
      <th>Type</th>
      <th>Description</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td><a class="xref" href="https://learn.microsoft.com/dotnet/api/system.eventhandler-1">EventHandler</a>&lt;<a class="xref" href="C1.WPF.Word.DrawingElementEventArgs.html">DrawingElementEventArgs</a>&gt;</td>
      <td>Occurs when the  is about to render a .</td>
    </tr>
  </tbody>
</table>
<h5 id="C1_WPF_Word_C1WordDocument_DrawingElement_remarks"><strong>Remarks</strong></h5>
<div class="markdown level0 remarks"><pre><code>&lt;p&gt;This event fires once for each &lt;xref href=&quot;System.Windows.FrameworkElement&quot; data-throw-if-not-resolved=&quot;false&quot;&gt;&lt;/xref&gt; rendered after calls to the
</code></pre>
<p><a class="xref" href="C1.WPF.Word.C1WordDocument.DrawElement.html#C1_WPF_Word_C1WordDocument_DrawElement_System_Windows_FrameworkElement_System_Windows_Rect_System_Windows_Rect_">DrawElement(FrameworkElement, Rect, Rect)</a> method. The event allows you to override
the built-in rendering behavior and to render custom content for specific elements.</p>
<p>If you handle this event, then remember to set the <a class="xref" href="C1.WPF.Word.DrawingElementEventArgs.Handled.html#C1_WPF_Word_DrawingElementEventArgs_Handled">Handled</a>
property to true so the control will not render the element over your custom content.</p>
</div>
<h5 id="C1_WPF_Word_C1WordDocument_DrawingElement_examples"><strong>Examples</strong></h5>
<pre><code>&lt;p&gt;The example below shows how you can use the &lt;xref href=&quot;C1.WPF.Word.C1WordDocument.DrawingElement&quot; data-throw-if-not-resolved=&quot;false&quot;&gt;&lt;/xref&gt; event to provide custom
</code></pre>
<p>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 <a class="xref" href="https://learn.microsoft.com/dotnet/api/system.windows.media.imaging.writeablebitmap">WriteableBitmap</a> to represent
the object, then renders the image into the PDF document.</p>
<p>This example is quite general. You can use it to render any elements that do not expose their
child elements as primitives such as <a class="xref" href="https://learn.microsoft.com/dotnet/api/system.windows.controls.textblock">TextBlock</a>, <a class="xref" href="https://learn.microsoft.com/dotnet/api/system.windows.controls.border">Border</a>,
rectangle, etc.</p>
<p>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.</p>
<pre><code>&lt;pre&gt;&lt;code class=&quot;lang-csharp&quot;&gt;// create C1WordDocument
var rtf = new C1WordDocument(PaperKind.Letter);

// use DrawingElement event to render RichTextBox elements
pdf.DrawingElement += (s, e) =&gt;
  {
    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);&lt;/code&gt;&lt;/pre&gt;
</code></pre>

</div>
