【问题标题】:WPF to XPS Very SlowWPF 到 XPS 非常慢
【发布时间】:2015-05-07 06:34:33
【问题描述】:

您好,我们正在尝试创建一个基于 WPF 页眉和页脚元素的自定义模板系统,以及用于导出到 PDF 的 2D 绘图的画布。问题是 XpsWriter 需要大约 7 秒来编写 XPS 文档,另外需要 3 秒才能使用 PDFSharp 转换为 pdf。当用户等待 PDF 时,我们需要把它记下来。我首先怀疑是因为FrameworkElements的数量,但只有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。

希望尽可能保留 VECTOR

【问题讨论】:

  • 我刚刚发现在某些情况下使用透明画笔会导致 PDFSharp 崩溃。我改用了一个空的视觉画笔。仅此一项就导致额外的 5 秒
  • 事实证明 SolidColorBrush("Color") 使用的资源比“Brush.Color”多。使用 Brush.Color 转换为 XPS 需要额外 5 秒

标签: c# wpf pdfsharp xps xpsdocument


【解决方案1】:

有几种方法可以加快从 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。

【讨论】:

  • 如何冻结画笔?
猜你喜欢
  • 2011-06-11
  • 2018-09-13
  • 1970-01-01
  • 2011-01-30
  • 2017-11-04
  • 2013-11-06
  • 1970-01-01
  • 2020-05-10
  • 1970-01-01
相关资源
最近更新 更多