//打印文档 public void printWord() { object background = false; object missing = System.Reflection.Missing.Value; wordDoc.PrintOut(ref background, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing); }
/// <summary> /// Use this method to simulate background printing in word /// by using a task then await it unil it return. /// </summary> /// <param name="word"></param> /// <param name="selectedPrinter"></param> /// <returns></returns> private async Threading.Task PrintOut(_Document word, string selectedPrinter) { Threading.Task tsk = Threading.Task.Run(() => { _wordApplication.ActivePrinter = selectedPrinter; word.PrintOut(); } ); await tsk; return; }
/// <summary> /// Use this method to simulate background printing in word /// by using a task then await it unil it return. /// </summary> /// <param name="app"></param> /// <param name="word"></param> /// <param name="selectedPrinter"></param> /// <returns></returns> private async System.Threading.Tasks.Task PrintOut(_Application app, _Document word, string selectedPrinter) { System.Threading.Tasks.Task tsk = System.Threading.Tasks.Task.Run(() => { //app.ActivePrinter = selectedPrinter; //Note: This will change the Windows deafault printer. //Therefore, we use this function to print in word without affecting the Windows default printer. app.WordBasic.FilePrintSetup(Printer: selectedPrinter, DoNotSetAsSysDefault: 1); word.PrintOut(false); } ); await tsk; }
//打印文档 public void printWord() { object background = false; object missing = System.Reflection.Missing.Value; wordDoc.PrintOut(ref background, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing); object SaveChanges = WdSaveOptions.wdDoNotSaveChanges;//不保存模板的修改 object OriginalFormat = WdOriginalFormat.wdOriginalDocumentFormat; object RouteDocument = false; wordDoc.Close(ref SaveChanges, ref OriginalFormat, ref RouteDocument); wordApp.Quit(ref SaveChanges, ref OriginalFormat, ref RouteDocument); }
public void Print(string filepathandname) { WdSaveFormat format = WdSaveFormat.wdFormatPDF; // Create an instance of Word.exe _Application oWord = new Application { // Make this instance of word invisible (Can still see it in the taskmgr). Visible = false }; // Interop requires objects. object oMissing = System.Reflection.Missing.Value; object isVisible = true; object readOnly = true; // Does not cause any word dialog to show up //object readOnly = false; // Causes a word object dialog to show at the end of the conversion object oInput = filepathandname; object oOutput = filepathandname.Substring(0, filepathandname.Length - 4) + "pdf"; object oFormat = format; object oPrintToFile = true; // Load a document into our instance of word.exe _Document oDoc = oWord.Documents.Open( ref oInput, ref oMissing, ref readOnly, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref isVisible, ref oMissing, ref oMissing, ref oMissing, ref oMissing ); // Make this document the active document. oDoc.Activate(); // Save this document using Word //oDoc.SaveAs(ref oOutput, ref oFormat, ref oMissing, ref oMissing, // ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, // ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing // ); oWord.Visible = false; PrintDialog pDialog = new PrintDialog(); oWord.ActivePrinter = GlobalVariables.PDF_PRINTER; oDoc.PrintOut(ref oMissing, ref oMissing, ref oMissing, ref oOutput, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oPrintToFile, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing); oDoc.Close(SaveChanges: false); oDoc = null; }
public void Print(string printerName) { _Document doc = null; try { doc = OpenDoc(_templatePath, printerName); PrintHeaderField(doc); var docFields = doc.Fields; FillFields(docFields); doc.PrintOut(false); //False to wait for word to finish printing } catch { throw; } finally { _word.Quit(false); ReleaseResources(_word, doc); } }
public static void PrintDocument(List <string> listFilePath) { if (listFilePath.Count == 0) { return; } _Application WordApp = new Microsoft.Office.Interop.Word.Application(); object oMissing = System.Reflection.Missing.Value; var pdfViewer = new PdfViewer(); foreach (string sFilePath in listFilePath) { string sFileExtension = Path.GetExtension(sFilePath).ToLower(); if (sFileExtension.Contains(".doc")) { object oFilePath = sFilePath; _Document WordDoc = WordApp.Documents.Open( ref oFilePath, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing); WordDoc.PrintOut(); WordDoc.Close(WdSaveOptions.wdDoNotSaveChanges, ref oMissing, ref oMissing); } if (sFileExtension == ".pdf") { pdfViewer.LoadDocument(sFilePath);//打开文件 var printerSettings = new PrinterSettings(); printerSettings.Copies = 1; pdfViewer.Print(printerSettings); pdfViewer.CloseDocument(); } } WordApp.Quit(); pdfViewer.Dispose(); }