private void SaveXpsPage(string filename) { var clone = VisualClone(); if (!XpsDocs.ContainsKey(filename)) { var d = new FixedDocument(); d.DocumentPaginator.PageSize = GetSize(clone); XpsDocs.Add(filename, d); } var doc = XpsDocs[filename]; XpsSnapshots[filename] = this; var size = doc.DocumentPaginator.PageSize; var cont = new PageContent(); var pg = new FixedPage(); var canvas = new Canvas(); canvas.Children.Add(clone); canvas.Width = size.Width; canvas.Height = size.Height; pg.Width = size.Width; pg.Height = size.Height; pg.Background = Brushes.White; pg.Children.Add(canvas); FixedPage.SetLeft(canvas, 0); FixedPage.SetRight(canvas, 0); ((IAddChild)cont).AddChild(pg); //clone.MeasureAndArrange(new Size(clone.Width, clone.Height)); var width = clone.Width; var height = clone.Height; double zoom = 1; if (FitToPage) { zoom = Math.Min(size.Width / width, size.Height / height); } if (zoom != 1) { clone.LayoutTransform = new MatrixTransform(zoom, 0, 0, zoom, 0, 0); width *= zoom; height *= zoom; clone.MeasureAndArrange(new Size(width, height)); } Canvas.SetLeft(clone, (size.Width - width) / 2); Canvas.SetTop(clone, (size.Height - height) / 2); cont.Measure(size); cont.Arrange(new Rect(new Point(), size)); cont.UpdateLayout(); doc.Pages.Add(cont); }