Force render Viewbox in order to draw it on page
<p>I'm using a FlowDocument to create a report. Now, I created a paginator in order to be able to repeat the header on every page, however, it does not get rendered on each page. I suspect the problem is that the Viewbox is not rendered/created untill you try to display it.</p>

<p>This is my GetPage method:</p>

<pre><code>public override DocumentPage GetPage(int pageNumber) {
        DocumentPage page = m_Paginator.GetPage(pageNumber);

        ContainerVisual newpage = new ContainerVisual();
        DrawingVisual title = new DrawingVisual();
        using (DrawingContext ctx = title.RenderOpen())
        {
            var header = getHeader();
            RenderTargetBitmap bmp = new RenderTargetBitmap(165, 32, 96, 96,
                PixelFormats.Pbgra32);
            bmp.Render(header);
            ctx.DrawImage(bmp,new Rect(new Point(0,0),new Size(166, 33)));
        }
        ContainerVisual smallerPage = new ContainerVisual();
        title.Children.Add(getHeader());

        newpage.Children.Add(title);
        smallerPage.Children.Add(page.Visual);
        smallerPage.Transform = new MatrixTransform(0.95, 0, 0, 0.95, 0.025 * page.ContentBox.Width, 0.025 * page.ContentBox.Height);

        newpage.Children.Add(smallerPage);
        newpage.Transform = new TranslateTransform(m_Margin.Width, m_Margin.Height);
        return new DocumentPage(newpage, m_PageSize, Move(page.BleedBox), Move(page.ContentBox));
    }
</code></pre>

<p>Here's the Move method:</p>

<pre><code>Rect Move(Rect rect) {

        if (rect.IsEmpty) {
            return rect;
        }

        else {
            return new Rect(rect.Left + m_Margin.Width, rect.Top + m_Margin.Height,
                            rect.Width, rect.Height);
        }
    }
</code></pre>

<p>And here is getHeader() (Yes, I know, it should be GetHeader() - them conventions)</p>

<pre><code>private Viewbox getHeader() {
        Grid gr = new Grid();
        var sr = Application.GetResourceStream(new Uri("Propuestas;component/img/log.xaml", UriKind.Relative));

        var img = (Canvas)XamlReader.Load(new XmlTextReader(sr.Stream));

        var logo = new Viewbox {
            Child = img,
            HorizontalAlignment = HorizontalAlignment.Left,
            VerticalAlignment = VerticalAlignment.Center,
            Width = 165
        };
        var detalles = new TextBlock {
            FontSize = 10,
            FontFamily = new FontFamily("Verdana"),
            Padding = new Thickness(logo.Width + 15, 0, 0, 0)
        };
        App.Comando.CommandText = "SELECT RazEmp, DirEmp, CpEmp, PobEmp, ProEmp, TelEmp, CifEmp FROM META4.Empresa";
        using (var reader = App.Comando.ExecuteReader())
            while (reader.Read())
                detalles.Text = "" + reader.GetString(0).Trim() + "\n" + reader.GetString(1).Trim() + "\n" +
                                reader.GetDecimal(2) + " - " + reader.GetString(3).Trim() + "(" +
                                reader.GetString(4).Trim() + ")\n" + "Tlf: " + reader.GetString(5).Trim() +
                                "\nCIF: " + reader.GetString(6).Trim();

        var pd = new TextBox {
            Text = "PEDIDO DE COMPRA",
            TextAlignment = TextAlignment.Left,
            FontSize = 19,
            FontFamily = new FontFamily("Verdana"),
            FontWeight = FontWeights.Bold,
            Background = new SolidColorBrush(Color.FromRgb(192, 192, 192)),
            Margin = new Thickness(logo.Width + 15, 10, 0, 20),
            BorderThickness = new Thickness(0)
        };
        gr.ColumnDefinitions.Add(new ColumnDefinition());
        gr.ColumnDefinitions.Add(new ColumnDefinition());
        gr.RowDefinitions.Add(new RowDefinition());
        gr.RowDefinitions.Add(new RowDefinition());
        Grid.SetRow(logo, 0);
        Grid.SetRow(detalles, 0);
        Grid.SetRow(pd, 1);
        Grid.SetColumn(pd, 0);
        Grid.SetColumnSpan(pd, 2);
        Grid.SetColumnSpan(detalles, 2);

        gr.Children.Add(logo);
        gr.Children.Add(detalles);
        gr.Children.Add(pd);
        Viewbox vb = new Viewbox();
        vb.Child = gr;
        return vb;
    }
</code></pre>

<p>However, when I hit print, it prints it normally, without repeating the header. I can see the query running up in debug, so addHeader() gets executed. The width and height are predetermined and fixed. Both header.Width/header.Height and header.ActualWidth/header.ActualHeight give me either 0 or NaN, which makes me believe that the viewbox isn't rendered in the background. Is there any way I would be able to repeat this on each page?</p>

<p>The problem is that my header contains one image and two parts text. I already had it created to be put on the first page only, but now the requirements have changed and I have to repeat it on every page. </p>

<p>Any help is greatly appreciated.</p>

<p>Later Edit: Also tried this, didn't work either.</p>

<pre><code>private static BitmapSource CaptureScreen(Visual target, double dpiX, double dpiY) {
        if (target == null)
            return null;
        Size size = new Size(165, 31.536);
        RenderTargetBitmap rtb = new RenderTargetBitmap((int)(size.Width * dpiX / 96.0),
                                                    (int)(size.Height * dpiY / 96.0),
                                                    dpiX,
                                                    dpiY,
                                                    PixelFormats.Pbgra32);
        DrawingVisual dv = new DrawingVisual();
        using (DrawingContext ctx = dv.RenderOpen()) {
            VisualBrush vb = new VisualBrush(target);
            ctx.DrawRectangle(vb, null, new Rect(new Point(), size));
        }

        rtb.Render(dv);
        return rtb;
    }
</code></pre>