简体   繁体   English

WPF到XPS的速度很慢

[英]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. 您好,我们正在尝试创建一个自定义模板系统,例如WPF页眉和页脚元素,以及用于2D工程图的画布以导出到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. 问题是XpsWriter大约需要7秒钟来编写XPS文档,而用PDFSharp转换到pdf则需要3秒钟。 We need to get this down as the user waits for the PDF. 当用户等待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. 我首先怀疑它是由于中的FrameworkElements数量引起的,但是只有5000个。Framework元素主要是带有填充,笔触和画笔的PATH数据。

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. 也许是某种类型的Visual Object,或者某种程度上是“ Flatten”的Canvas或任何想法。 When it does work the PDF is over 5MB. 当它工作时,PDF超过5MB。

Would like to keep it VECTOR as much as possible 希望尽可能将其保留为VECTOR

There are several ways to speed up the conversion from WPF to XPS to PDF:- 有几种方法可以加快从WPF到XPS到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). 在后台渲染(创建后台UI线程)。

  3. Do not save the interim XPS document to disk but use a MemoryStream. 不要将临时XPS文档保存到磁盘,而要使用MemoryStream。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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