简体   繁体   中英

WPF to XPS Very Slow

Hello We are trying to create a custom template system based such as WPF Header and Footer elements, with a canvas for 2D Drawings for export to PDF. The problem is the XpsWriter takes about 7 seconds to write the XPS Document, and another 3 Seconds to convert to pdf with PDFSharp. We need to get this down as the user waits for the PDF. I first suspected its due to the number of FrameworkElements in the, but there are only 5000. The framework elements are mostly PATH data with fills, strokes, and brushes.

Canvas ComplexCanvas = new Canvas();
ComplexCanvas.Children.Add(5000Elements);

        System.Windows.Documents.FixedDocument fixedDoc = new System.Windows.Documents.FixedDocument();
        System.Windows.Documents.PageContent pageContent = new System.Windows.Documents.PageContent();
        System.Windows.Documents.FixedPage fixedPage = new System.Windows.Documents.FixedPage();

        //Create first page of document
        fixedPage.Children.Add(ComplexCanvas);
        fixedPage.Width = PageWidth;
        fixedPage.Height = PageHeight;
        ((System.Windows.Markup.IAddChild)pageContent).AddChild(fixedPage);


        fixedDoc.Pages.Add(pageContent);


        System.Windows.Xps.Packaging.XpsDocument xpsd = new XpsDocument(Path, System.IO.FileAccess.Write);
        System.Windows.Xps.XpsDocumentWriter xw = XpsDocument.CreateXpsDocumentWriter(xpsd);
        xw.Write(fixedDoc);
        xpsd.Close();

Does anyone know a way to speed this up? Perhaps some type Visual Object, or "Flatten" the Canvas somehow or any ideas. When it does work the PDF is over 5MB.

Would like to keep it VECTOR as much as possible

There are several ways to speed up the conversion from WPF to XPS to PDF:-

  1. Freeze any pens or brushes as this will speed up rendering:-

      SolidColorBrush brush = new SolidColorBrush(Colors.PaleGreen); brush.Opacity = .25d; brush.Freeze(); Pen paleGreenPen = new Pen(brush, 1); paleGreenPen.Freeze(); Pen linePen = new Pen(Brushes.Red, 1); linePen.Freeze(); 
  2. Render in the background (create a background UI thread).

  3. Do not save the interim XPS document to disk but use a MemoryStream.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM