/// <summary> /// Renders the current visual as a FixedPage and save it as XPS file. /// </summary> public void SaveXps() { var zipFile = Package.Open(Path.Combine(OutputDirectory, Name + ".xps"), FileMode.Create); XpsDocument xpsDocument = new XpsDocument(zipFile); PageContent pageContent = new PageContent(); FixedPage fixedPage = new FixedPage(); fixedPage.Width = WidthInPU; fixedPage.Height = HeightInPU; fixedPage.Background = Brushes.Transparent; // Visuals needs a UIElement as drawing container VisualPresenter presenter = new VisualPresenter(); presenter.AddVisual(this.visual); FixedPage.SetLeft(presenter, 0); FixedPage.SetTop(presenter, 0); fixedPage.Children.Add(presenter); // Perform layout Size size = new Size(WidthInPU, HeightInPU); fixedPage.Measure(size); fixedPage.Arrange(new Rect(new Point(), size)); fixedPage.UpdateLayout(); ((IAddChild)pageContent).AddChild(fixedPage); FixedDocument fixedDocument = new FixedDocument(); fixedDocument.DocumentPaginator.PageSize = size; fixedDocument.Pages.Add(pageContent); // Save as XPS file XpsDocumentWriter xpsWriter = XpsDocument.CreateXpsDocumentWriter(xpsDocument); xpsWriter.Write(fixedDocument); xpsDocument.Close(); zipFile.Close(); // Must call at least two times GC.Collect this to get access to the xps file even I write synchronously. This is a bug in WPF. // Vista 64 .NET 3.5 SP1 installed xpsDocument = null; xpsWriter = null; GC.Collect(10, GCCollectionMode.Forced); GC.Collect(10, GCCollectionMode.Forced); //GC.Collect(10, GCCollectionMode.Forced); //GC.Collect(10, GCCollectionMode.Forced); }
/// <summary> /// Renders the current visual as a FixedPage and save it as XPS file. /// </summary> public void SaveXps() { XpsDocument xpsDocument = new XpsDocument(Path.Combine(OutputDirectory, Name + ".xps"), FileAccess.ReadWrite); PageContent pageContent = new PageContent(); FixedPage fixedPage = new FixedPage(); fixedPage.Width = WidthInPU; fixedPage.Height = HeightInPU; fixedPage.Background = Brushes.Transparent; // Visuals needs a UIElement as drawing container VisualPresenter presenter = new VisualPresenter(); presenter.AddVisual(this.visual); FixedPage.SetLeft(presenter, 0); FixedPage.SetTop(presenter, 0); fixedPage.Children.Add(presenter); // Perform layout Size size = new Size(WidthInPU, HeightInPU); fixedPage.Measure(size); fixedPage.Arrange(new Rect(new Point(), size)); fixedPage.UpdateLayout(); ((IAddChild)pageContent).AddChild(fixedPage); FixedDocument fixedDocument = new FixedDocument(); fixedDocument.DocumentPaginator.PageSize = size; fixedDocument.Pages.Add(pageContent); // Save as XPS file XpsDocumentWriter xpsWriter = XpsDocument.CreateXpsDocumentWriter(xpsDocument); xpsWriter.Write(fixedDocument); xpsDocument.Close(); // Must call at least two times GC.Collect this to get access to the xps file even I write synchronously. This is a bug in WPF. // Vista 64 .NET 3.5 SP1 installed xpsDocument = null; xpsWriter = null; GC.Collect(10, GCCollectionMode.Forced); GC.Collect(10, GCCollectionMode.Forced); //GC.Collect(10, GCCollectionMode.Forced); //GC.Collect(10, GCCollectionMode.Forced); }