我想实现这样一个分页器:
public class MyPaginator : DocumentPaginator{
// ommitting details...
public override DocumentPage GetPage(int pageNumber) {
DocumentPage page = new DocumentPage(canvas);
return page;
}
}
它编译,它运行,但是页面是空白的(白色)。canvas是System.Windows.Controls.Canvas的实例。
当我把它放在屏幕上的容器,如ScrollViewer,它呈现完美。
XpsDocument _xpsDocument = CreateXpsDoc(myPaginatorInstance);
唯一有效的是页面的大小被设置为画布的大小。我错过了什么?
我来回答我自己的问题:
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);
}