0

I'm trying to implement a Paginator like this:

public class MyPaginator : DocumentPaginator{

  // ommitting details...

  public override DocumentPage GetPage(int pageNumber) {
    DocumentPage page = new DocumentPage(canvas);
    return page;
  }
}

It compiles, it runs, but the page is blank (white). the 'canvas' is an instance of System.Windows.Controls.Canvas.

When I put it in a on-screen container like ScrollViewer it renders perfectly.

XpsDocument _xpsDocument = CreateXpsDoc(myPaginatorInstance);

The only thing that is working is that the page's size is set to the size of the canvas. What am I missing?

Louis Somers
  • 2,560
  • 3
  • 27
  • 57

1 Answers1

4

I'll answer my own tumbleweed (again):

public override DocumentPage GetPage(int pageNumber) {
  Canvas container = new Canvas();
  container.Children.Add(canvas);
  double scaleX = pageSize.Width / canvas.Width;
  double scaleY = pageSize.Height / canvas.Height;
  container.RenderTransform = new ScaleTransform(scaleX, scaleY);

  container.Width = PageSize.Width;
  container.Height = PageSize.Height;
  container.Measure(PageSize);
  container.Arrange(new Rect(new Point(0, 0), PageSize));

  Rect contentBox = new Rect(PageSize);

  return new DocumentPage(container, PageSize, contentBox, contentBox);
}
Louis Somers
  • 2,560
  • 3
  • 27
  • 57
  • 1
    Thanks Louis! I had routines for rendering pages to pdf using pdfsharp and for creating a wpf based preview of a specific page but how to get the same code which generated the wpf preview of a page to also create the actual output for a printer with proper scaling was far from clear. – Wonderbird Oct 31 '14 at 16:40
  • @Louis Somers what is `canvas` in this example? – theycallmemorty Oct 07 '15 at 17:00
  • @theycallmemorty As stated in my question (above my answer), it is an instance of [System.Windows.Controls.Canvas](https://msdn.microsoft.com/en-us/library/system.windows.controls.canvas(v=vs.100).aspx#Anchor_9), which is of course populated with child controls that together form a page layout. The use of Canvas is convenient in the context of a fixed page markup, with design elements in fixed positions. It features "absolute positioning of child content" and thus will not try to rescale, reposition or re-flow the content to fit any constraints. Child elements remain exactly where put. – Louis Somers Oct 07 '15 at 22:36