Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You can print Visual objects by hosting them in a FrameworkElement and then adding the FrameworkElement to a FixedDocument as content of a FixedPage. The Visual host looks like this:</p> <pre><code>/// &lt;summary&gt; /// Implements a FrameworkElement host for a Visual /// &lt;/summary&gt; public class VisualHost : FrameworkElement { Visual _visual; /// &lt;summary&gt; /// Gets the number of visual children (always 1) /// &lt;/summary&gt; protected override int VisualChildrenCount { get { return 1; } } /// &lt;summary&gt; /// Constructor /// &lt;/summary&gt; /// &lt;param name="visual"&gt;The visual to host&lt;/param&gt; public VisualHost(Visual visual) { _visual = visual; AddVisualChild(_visual); AddLogicalChild(_visual); } /// &lt;summary&gt; /// Get the specified visual child (always /// &lt;/summary&gt; /// &lt;param name="index"&gt;Index of visual (should always be 0)&lt;/param&gt; /// &lt;returns&gt;The visual&lt;/returns&gt; protected override Visual GetVisualChild(int index) { if (index != 0) throw new ArgumentOutOfRangeException("index out of range"); return _visual; } } </code></pre> <p>Then you can add them and print them like this:</p> <pre><code> // Start the fixed document FixedDocument fixedDoc = new FixedDocument(); Point margin = new Point(96/2, 96/2); // Half inch margins // Add the visuals foreach (Visual nextVisual in visualCollection) { // Host the visual VisualHost host = new VisualHost(nextVisual); Canvas canvas = new Canvas(); Canvas.SetLeft(host, margin.X); Canvas.SetTop(host, margin.Y); canvas.Children.Add(host); // Make a FixedPage out of the canvas and add it to the document PageContent pageContent = new PageContent(); FixedPage fixedPage = new FixedPage(); fixedPage.Children.Add(canvas); ((System.Windows.Markup.IAddChild)pageContent).AddChild(fixedPage); fixedDoc.Pages.Add(pageContent); } // Write the finished FixedDocument to the print queue XpsDocumentWriter xpsDocumentWriter = PrintQueue.CreateXpsDocumentWriter(queue); xpsDocumentWriter.Write(fixedDoc); </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload