0

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

Tim Davis
  • 524
  • 6
  • 17
  • I have just found that using transparent brushes was causing PDFSharp to crash in some cases. I used an empty visual brush instead. This alone is causing 5 seconds extra – Tim Davis Mar 05 '15 at 19:15
  • It turns out that SolidColorBrush("Color") uses more resources than "Brush.Color". Using Brush.Color took an additional 5 seconds off conversion to XPS – Tim Davis Mar 05 '15 at 23:05

1 Answers1

1

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 Lonely Coder
  • 613
  • 9
  • 12