Print a Word document from WPF with print settings (generated by Aspose)

I have been struggling with the print problem for a long time, hope someone can help.

Background I create an Aspose.Words document from a word template and merge it by email, and then want to print it directly from the WPF application using the print dialog. When printing, I need to select all the printer options available for my printer (what paper is used, scaling, orientation, color, etc.). This seems to be the last thing that makes my Google searches succeed, since all the examples I find relate only to the name of the printer or the number of copies to print. A.

Test 1 - Assign Your Preferred Printing Method From Your 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); } 

Now it seems perfect! But I get two one problem (s).

  • The text doubles on the page, since it first prints in the default font, and the second in the second font of the second, on top of the first. See Screencap: Sorry for that, it was a hidden image inside a docx file that came to the fore when it was somehow converted (although hidden in Word).

The text is doubled on the page

  • This dog is slow ... it takes forever for document.Print, although it is only 2 pages for printing and no graphics.

Test 2 - Printing Using a Process (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(); } 

This would be a good solution, but it does not give me dialogue (I can use the word PrintTo and give some arguments, such as the name of the printer, etc. But not for my special requirements, right?)

Test 3 - Printing with Word Automation

  private void Print(Document document) { object nullobj = Missing.Value; var savePath = String.Format("C:\\temp\\a.docx"); document.Save(savePath, SaveFormat.Docx); var wordApp = new Microsoft.Office.Interop.Word.Application(); wordApp.DisplayAlerts = Microsoft.Office.Interop.Word.WdAlertLevel.wdAlertsNone; wordApp.Visible = false; Microsoft.Office.Interop.Word.Document doc = null; Microsoft.Office.Interop.Word.Documents docs = null; Microsoft.Office.Interop.Word.Dialog dialog = null; try { docs = wordApp.Documents; doc = docs.Open(savePath); doc.Activate(); dialog = wordApp.Dialogs[Microsoft.Office.Interop.Word.WdWordDialog.wdDialogFilePrint]; var dialogResult = dialog.Show(ref nullobj); if (dialogResult == 1) { doc.PrintOut(false); } }catch(Exception) { throw; }finally { Thread.Sleep(3000); if (dialog != null) Marshal.FinalReleaseComObject(dialog); if (doc != null) Marshal.FinalReleaseComObject(doc); if (docs != null) Marshal.FinalReleaseComObject(docs); GC.Collect(); GC.WaitForPendingFinalizers(); GC.Collect(); GC.WaitForPendingFinalizers(); doc = null; wordApp.Quit(false, ref nullobj, ref nullobj); } } 

Ok, so should I use Automation? It prints well, but when it comes to closing the word app and document, I get problems. For example, sometimes I get the "Fields outside the printable area" dialog box and woops, the code cannot exit the process and leave it. Do you see Thread.Sleep? If I do not have it, Word will be closed until printing is complete. A.

You see, I do not understand all my attempts. What is the best way to do this?

Thank you for your time!

+2
source share
1 answer

OK, I found a suitable WPF solution by converting the document to XPS and loading it into a DocumentViewer, from which I can use my own printing functions. A.

View.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); } } 

Note to self: ReachFramework DLL reference

+2
source

Source: https://habr.com/ru/post/1379100/


All Articles