views:

291

answers:

2

Hello,

We have an application structured roughly like this:

<Grid x:Name="LayoutRoot">
  <ScrollViewer>
<Canvas x:Name="canvas">
  <StackPanel> < Button /><Slider /><Button /></StackPanel>
  <custom:Blob />
  <custom:Blob />
  <custom:Blob />
</Canvas>
  </ScrollViewer>
</Grid>

Each Blob consists of 1 or more rectangles, lines, and text boxes; they are positioned anywhere on the canvas.

If I print the document using the LayoutRoot:

PrintDocument pd = new PrintDocument();
pd += (s, pe) => { pe.PageVisual = LayoutRoot; };
pd.Print("Blobs");

... it is like a print-screen -- the scrollbars, the sliders, the blobs that are visible -- are printed.

If I set PageVisual = canvas, nothing is printed.

How can I get all the blob objects, and just those objects, to print? Do I need to copy them into another container, and give that container to PageVisual? Can I use a ViewBox to make sure they all fit on one page?

Thanks for any pointers....

+2  A: 

First idea that came to mind while reading your post was the size of your canvas that groups your Blob objects. So found some interesting fragments that might help you:

In addition to specifying the UIElement, you can get the physical size of the print area with the PrintPageEventArgs..::.PrintableArea property. If the UIElement exceeds the PrintableArea, the content will be clipped at the bounds of the PrintableArea. The dimensions of the printable area are in screen-based pixels.

and

You use the PrintPageEventArgs..::.HasMorePages property to print a document with multiple pages. The default for PrintPageEventArgs..::.HasMorePages is false, so it does not need to be set for one-page documents. However, if there are multiple pages to print, you set the PrintPageEventArgs..::.HasMorePages property to true to indicate that there are additional pages to print. You set PrintPageEventArgs..::.HasMorePages back to false in the PrintPage event handler, when the last page is being printed.

Documentation article

texmex5
Helpful, thanks. There is probably a way to specify the region (a rectangle) of the Canvas to be printed... ?Or, similarly, in the StartPrint event, resize the Canvas to the minimum bounding rectangle for all the objects, then restore its size in the EndPrint event.Finally, can a ViewBox transform a Canvas to fit on one page?
Number8
I am not sure, I haven't had a chanche to try this out, but it must do something when you specify that you have more than 1 page and the content doesn't fit.But if it indeed does nothing then you might maybe need to scale the grouping canvas before printing and then later scale it back, when done printing.
texmex5
A: 

You can apply a scale tranform on the LayoutRoot before printing.

But I have run across a problem where LayoutRoot stays scaled down/up after printing. (Question asked here)

Sung Meister