static void Main(string[] args) { string strPath = System.AppDomain.CurrentDomain.BaseDirectory.Replace("\\", "/"); // Instantiate Object APServer.Server server = new APServer.Server(); // Path and filename of output server.NewDocumentName = "Server.WordPrinting.pdf"; server.OutputDirectory = strPath; // Start the print job ServerDK.Results.ServerResult result = server.BeginPrintToPDF(); if (result.ServerStatus == ServerDK.Results.ServerStatus.Success) { // Automate Word to print a document to Server // NOTE: You must add the 'Microsoft.Office.Interop.Word' // reference Microsoft.Office.Interop.Word._Application oWORD = new Microsoft.Office.Interop.Word.Application(); oWORD.ActivePrinter = server.NewPrinterName; oWORD.DisplayAlerts = Microsoft.Office.Interop.Word.WdAlertLevel.wdAlertsNone; oWORD.Visible = false; Microsoft.Office.Interop.Word.Document oDOC = oWORD.Documents.Open($"{strPath}Server.Word.Input.doc"); oDOC.Activate(); oWORD.PrintOut(); oWORD.Documents.Close(); oWORD.Quit(); // Wait(seconds) for job to complete result = server.EndPrintToPDF(waitTime: 30); } // Output result WriteResult(result); // Process Complete Console.WriteLine("Done!"); Console.WriteLine("Press any key to exit."); Console.ReadKey(); }
public static void exportWord(Dictionary <string, string> dataPrint, string fileName) { Microsoft.Office.Interop.Word.Application objWord = null; Microsoft.Office.Interop.Word.Document objDoc = null; object oBookMark = null; try { _wait = new WaitDialogForm("Đang xuất dữ liệu...", "Xin vui lòng chờ giây lát"); object oMissing = System.Reflection.Missing.Value; object oFalse = false; //init object oEndOfDoc = "\\endofdoc"; /* \endofdoc is a predefined bookmark */ object oTemplate = Application.StartupPath + "\\Template\\" + fileName; Object beforeRow = Type.Missing; objWord = new Microsoft.Office.Interop.Word.Application(); objDoc = objWord.Documents.Add(ref oTemplate, ref oMissing, ref oMissing, ref oMissing); objWord.Visible = false; foreach (KeyValuePair <string, string> pair in dataPrint) { objDoc.Bookmarks[pair.Key].Range.Text = pair.Value; } if (false) { objWord.DisplayAlerts = Microsoft.Office.Interop.Word.WdAlertLevel.wdAlertsNone; objWord.Options.PrintBackground = false; PrintDialog printer = new PrintDialog(); if (!printer.PrinterSettings.IsValid) { if (printer.ShowDialog() == DialogResult.OK) { objWord.ActivePrinter = printer.PrinterSettings.PrinterName; objWord.PrintOut(); } } else { objWord.PrintOut(); } objWord.Quit(SaveChanges: false); } else { objWord.Visible = true; } //objWord.Visible = true; } catch (Exception ex) { string exMsg = "Xuất Word không thành công!\n"; XtraMessageBox.Show(exMsg, "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information); clsMessage.MessageExclamation(exMsg + ex.Message); } finally { if (!_wait.IsDisposed) { _wait.Close(); } releaseObject(objDoc); releaseObject(objWord); } }