views:

46

answers:

2

I have the same problem as the question stated in "Printing in Silverlight 4".
To get around the problem, I have tried to scale transform root of my visual tree before printing.

    void document_PrintPage(object sender, PrintPageEventArgs e)
    {
        var renderScale = 1.0D;
        if (LayoutRoot.ActualWidth > e.PrintableArea.Width)
            renderScale = e.PrintableArea.Width/LayoutRoot.ActualWidth;

        var scaleTransform = new ScaleTransform();
        scaleTransform.ScaleX *= renderScale;
        scaleTransform.ScaleY *= renderScale;

        e.PageVisual = LayoutRoot;
        e.PageVisual.RenderTransform = scaleTransform;
    }

Now above code correctly prints out with silverlight visuals fit on a piece of paper.

The problem now is that LayoutRoot itself is now scaled down on the screen.
The question is, is there a way for me to create a clone of LayoutRoot before applying scale transform?

My walk-around is to applying the scale tranformation again after printing but I'd like to know if there is a way to clone visual tree

+1  A: 

Check out this link for details on silverlight object clone.

also just another idea would using xamlreader/writer to read the xaml string and creating an in-memory copy of the visual tree work.

for ex

If your xaml has button called originalbutton, using the code below you will have a copy of the button in readerLoadButton

// Save the Button to a string.
string savedButton = XamlWriter.Save(originalButton);

// Load the button
StringReader stringReader = new StringReader(savedButton);
XmlReader xmlReader = XmlReader.Create(stringReader);
Button readerLoadButton = (Button)XamlReader.Load(xmlReader);
Vinay B R
XamlWriter/Reader are available only in WPF.
Sung Meister