WPF到XPS非常慢



你好,我们正在尝试创建一个基于WPF页眉和页脚元素的自定义模板系统,其中包含一个用于导出为PDF的2D绘图的画布。问题是XpsWriter编写XPS文档大约需要7秒,使用PDFSharp转换为pdf需要3秒。我们需要在用户等待PDF时将其删除。我最初怀疑这是由于中FrameworkElement的数量,但只有5000个。框架元素主要是带有填充、笔划和笔刷的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();

有人知道加快速度的方法吗?也许是某种类型的视觉对象,或者以某种方式"压平"画布或任何想法。当它工作时,PDF超过5MB。

希望尽可能多地保持它的矢量

有几种方法可以加快从WPF到XPS到PDF的转换:-

  1. 冻结任何笔或笔刷,因为这将加快渲染速度:-

        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. 在背景中渲染(创建背景UI线程)。

  3. 不要将临时XPS文档保存到磁盘,而是使用MemoryStream

最新更新