2012-03-15 231 views
2

我一直在打印很长一段时间的打印问题,希望有人可以帮助。从打印设置打印Word文档(Aspose生成)

背景 我创建从文字模板的使用Aspose.Words文档和邮件合并,然后想直接从打印对话框中的WPF应用程序打印。 打印时,我需要能够选择可用于我的打印机的所有不同的打印机设置(使用什么纸张,缩放,方向,颜色等)。最后一件事似乎是让我的Google搜索成功,因为我发现的所有例子只是给出打印机名称或要打印的副本数量。

测试1 - 阅读Aspose的首选印刷 From their forum

private void Print(Document document) 
    { 
     var printDialog = new System.Windows.Forms.PrintDialog 
     { 
      AllowSomePages = true, 
      PrinterSettings = new PrinterSettings 
      { 
       MinimumPage = 1, 
       MaximumPage = document.PageCount, 
       FromPage = 1, 
       ToPage = document.PageCount 
      }, 
      UseEXDialog = true 
     }; 

     var result = printDialog.ShowDialog(); 
     if (result.Equals(DialogResult.OK)) 
      document.Print(printDialog.PrinterSettings); 
    } 

的方式现在,这似乎是完美的!但我得到两个一个问题。

  • 该文本在页面上加倍,因为它似乎先用默认字体打印,其次是我的特殊字体第二个,在第一个之上。请参阅screencap:对不起,这是一个隐藏在docx-file内部的图像,当它以某种方式转换(即使隐藏在Word中)时,它就位于前面。

The text is doubled on the page

  • 这是狗缓慢......它永远为document.Print,即使它只有2页进行打印,而没有图片。

测试2 - 与过程打印(PDF)

private void Print(Document document) 
    { 
     var savePath = String.Format("C:\\temp\\a.pdf"); 
     document.Save(savePath, SaveFormat.Pdf); 

     var myProcess = new Process(); 
     myProcess.StartInfo.FileName = savePath; 
     myProcess.StartInfo.Verb = "Print"; 
     //myProcess.StartInfo.CreateNoWindow = true; 
     myProcess.Start(); 
     myProcess.WaitForExit(); 
    } 

这将是一个很好的解决方案,但它并没有给我的对话框(我可以用一句话PrintTo并给予一定的论据?像打印机名称等,但不适合我的特殊要求,右)

测试3 - 与Word自动化

打印

好的,我应该使用自动化吗?它打印的很好,但是当涉及到关闭word-app和文档时,我会遇到麻烦。例如,我有时候会看到“可打印区域以外的边距”对话框,而且,代码无法退出进程并保留。 你看到Thread.Sleep?如果我没有它,Word将在打印完成之前退出。

你看,我在所有的尝试中都落空了。什么是最好的方式去做这件事?

谢谢你的时间!

回答

2

好吧,我找到了一个合适的WPF解决方案,将文档转换为XPS并将其加载到DocumentViewer中,从中我可以使用本机打印功能。

查看。XAML

<DocumentViewer Document="{Binding XpsFixedDocumentSequence}"/> 

ViewModel.cs

using System.Windows.Xps.Packaging; 
... 

private void PrepareDocument(Document document) 
{ 
    var xpsDoc = GetDocumentAsXps(document); 
    XpsFixedDocumentSequence = xpsDoc.GetFixedDocumentSequence(); 
} 

private XpsDocument GetDocumentAsXps(Document document) 
{ 
    var savePath = "C:\\temp\\doc.xps"; 
    document.Save(savePath, SaveFormat.Xps); 
    var xpsDoc = new XpsDocument(savePath, FileAccess.Read); 
    return xpsDoc; 
} 

/* Property XpsFixedDocumentSequence */ 
public const string XpsFixedDocumentSequencePropertyName = "XpsFixedDocumentSequence"; 
private FixedDocumentSequence _xpsFixedDocumentSequence; 
public FixedDocumentSequence XpsFixedDocumentSequence 
{ 
    get { return _xpsFixedDocumentSequence; } 

    set 
    { 
     if (_xpsFixedDocumentSequence == value) return; 
     _xpsFixedDocumentSequence = value; 
     RaisePropertyChanged(XpsFixedDocumentSequencePropertyName); 
    } 
} 

自我提醒:引用ReachFramework DLL