生成并打印Microsoft Word文件

时间:2018-09-08 13:05:44

标签: c# .net printing ms-word office-interop

基本上,我想生成一个Word文件,然后打印它。

这是我到目前为止所拥有的:

private void CreateDocument()
{
    try
    {
        // Create an instance for word app
        Microsoft.Office.Interop.Word.Application winword = new Microsoft.Office.Interop.Word.Application();

        // Set status for word application is to be visible or not.
        winword.Visible = false;

        // Create a missing variable for missing value
        object missing = System.Reflection.Missing.Value;

        // Create a new document
        Microsoft.Office.Interop.Word.Document document = winword.Documents.Add(ref missing, ref missing, ref missing, ref missing);

        // Add paragraph with Heading 1 style
        Microsoft.Office.Interop.Word.Paragraph para1 = document.Content.Paragraphs.Add(ref missing);
        object styleHeading1 = "Heading 1";
        para1.Range.set_Style(ref styleHeading1);
        para1.Range.Text = "BRGY. BOLO WATER SERVICE COOPERATIVE";
        para1.Range.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphCenter;
        para1.Range.InsertParagraphAfter();

        // Save the document
        filename = Application.StartupPath + @"\Disconnection\temp1.docx";
        document.SaveAs(ref filename);
        document.Close(ref missing, ref missing, ref missing);
        document = null;

        MessageBox.Show("Document created successfully !");

        if (File.Exists(filename.ToString()))
        {
            PrintDocument printDoc = new PrintDocument();
            PrinterSettings prnsetting = new PrinterSettings();

            prnsetting.PrintFileName = Application.StartupPath + @"\Disconnection\.do";
            printDoc.DocumentName = "temp1";

            printPreviewDialog1.Document = printDoc;
            printPreviewDialog1.ShowDialog(); 
        }
    }
    catch (Exception ex)
    {
        //debug purposes
        MessageBox.Show(ex.Message +"\n"+filename.ToString());
    }

}

问题是当打印预览显示时,它只有空白页。有适当的方法吗?是否需要设置打印机的文件路径?

1 个答案:

答案 0 :(得分:0)

有许多将Word文档打印到打印机的示例。下面是另一个。希望这会有所帮助吗?

        public void PrintWord(string strFileName)
    {
        object oMissing = System.Reflection.Missing.Value;
        string strCurrentActivePrinter;

        //Start Word and open the test document.
        Microsoft.Office.Interop.Word._Application oWord = new Microsoft.Office.Interop.Word.ApplicationClass();

        Microsoft.Office.Interop.Word._Document oDoc;
        oWord.Visible = false;

        object oPath = strFileName;

        //Get and store the current activeprinter
        strCurrentActivePrinter = oWord.ActivePrinter;

        //Assign new activeprinter to Word
        oWord.ActivePrinter = PDFprinter;

        oDoc = oWord.Documents.Open(ref oPath,
            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);

        //print it
        object xcopies = 1;
        object xpt = false;
        object oFalse = false;

        oDoc.PrintOut(ref xpt, ref oMissing, ref oMissing,
            ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref 
            xcopies, ref oMissing, ref oMissing, ref oMissing, ref 
            oMissing, ref oMissing, ref oMissing, ref oMissing, ref 
            oMissing, ref oMissing, ref oMissing);

        //close the document
        oDoc.Close(ref oFalse, ref oMissing, ref oMissing);

        oWord.Options.SaveNormalPrompt = false;
        oWord.Options.SavePropertiesPrompt = false;

        //Reset activerprinter
        oWord.ActivePrinter = strCurrentActivePrinter;

        //close word
        oWord.Quit(ref oFalse, ref oMissing, ref oMissing);

        //Drop our reference to the COM object
        //Marshal.ReleaseComObject(oWord);
        //Marshal.ReleaseThreadCache();

        oDoc = null;
        oWord = null;
    }