Microsoft office interop word下打印機的運用

今天使用.net做一個票據打印模塊時,學習幾點,分享一下。

第一點,在.net下使用基於word模板生出新的word文檔

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using Microsoft.Office.Interop.Word;

namespace DzWordUtil
{
    public class WordUtil
    {
        private ApplicationClass WordApp;
        private Document WordDoc;
        private object missing = Type.Missing;
        private static bool isOpened = false;//判斷word模版是否被佔用 

        /// <summary>
        /// 將Word另存爲。strFileName爲另存爲文件名(含地址),isReplace是否刪除已經存在的文件。
        /// </summary>
        /// <param name="strFileName">另存爲的文件名</param>
        /// <param name="isReplace">是否刪除</param>
        public void SaveAs(string strFileName, bool isReplace)
        {
            if (isReplace && File.Exists(strFileName))
            {
                File.Delete(strFileName);
            }
            object fileName = strFileName;
            try
            {
                WordDoc.SaveAs(ref fileName, 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);
            }
            catch (Exception Ex)
            {

                throw new Exception(Ex.Message);
            }
        }


        /// <summary>
        /// 定義一個Word.Application 對象 
        /// </summary>
        private void activeWordApp()
        {
            WordApp = new ApplicationClass();
        }


        /// <summary>
        /// 退出模板編輯,釋放相應資源。
        /// </summary>
        public void Quit()
        {
            try
            {
                WordDoc.Close(ref missing, ref missing, ref missing);
                WordApp.Application.Quit(ref missing, ref missing, ref missing);
                isOpened = false;
            }
            catch (Exception Ex)
            {
                
                throw new Exception(Ex.Message);
            }
        }
        public void PrintDialog(string strFileName,string printerName = null)
        {
            if (File.Exists(strFileName))
            {
                activeWordApp();
                WordApp.Visible = false;
                object oPrintFile = (object)strFileName;
                try
                {
                    while (isOpened)
                    {
                        System.Threading.Thread.Sleep(500);
                    }
                    WordDoc = WordApp.Documents.Add(ref oPrintFile, ref missing, ref missing, ref missing);
                    isOpened = true;
                    WordDoc.Activate();

                    //WordDoc.PrintPreview();
                    if (printerName != null)
                    {
                        WordApp.ActivePrinter = printerName;
                    }
                    WordDoc.PrintOut();
                    this.Quit();
                }
                catch (Exception Ex)
                {
                    Quit();
                    isOpened = false;
                    throw new Exception(Ex.Message);
                }
            }
        }


        /// <summary>
        /// 基於模版新建Word文件。strTemppath爲模版地址。
        /// </summary>
        /// <param name="strTemppath">模版地址</param> 
        public void OpenTempelte(string strTemppath)
        {
            activeWordApp();
            WordApp.Visible = false;
            object oTemplate = (object)strTemppath;
            try
            {
                while (isOpened)
                {
                    System.Threading.Thread.Sleep(500);
                }
                WordDoc = WordApp.Documents.Add(ref oTemplate, ref missing, ref missing, ref missing);
                isOpened = true;
                WordDoc.Activate();
            }
            catch (Exception Ex)
            {
                Quit();
                isOpened = false;
                throw new Exception(Ex.Message);
            }
        }


        /// <summary>
        /// 添加文本內容到指定位置。LabelId爲書籤名,Content爲文本內容。
        /// </summary>
        /// <param name="LabelId">書籤名</param>
        /// <param name="Content">圖片地址</param>
        public void FillLable(string LabelId, string Content)
        {
            object bkmC = LabelId;
            if (WordApp.ActiveDocument.Bookmarks.Exists(LabelId) == true)
            {
                WordApp.ActiveDocument.Bookmarks.get_Item(ref bkmC).Select();
            }
            WordApp.Selection.TypeText(Content);
        }


        /// <summary>
        /// 添加圖片到指定位置。LabelId爲書籤名,imgUrl爲圖片地址。
        /// </summary>
        /// <param name="LabelId">書籤名</param>
        /// <param name="imgUrl">圖片地址</param>
        public void addPic(String LabelId, String imgUrl)
        {
            if (LabelId != null && LabelId.Length > 0 && imgUrl != null && imgUrl.Length > 0)
            {
                object LinkToFile = false;
                object SaveWithDocument = true;
                object bkmC = LabelId;
                if (WordApp.ActiveDocument.Bookmarks.Exists(LabelId) == true)
                {
                    WordApp.ActiveDocument.Bookmarks.get_Item(ref bkmC).Select();
                }
                WordApp.Selection.InlineShapes.AddPicture(imgUrl, ref LinkToFile, ref SaveWithDocument, ref missing);
            }
        }

    }
}

 上面的代碼,有部分爲轉載了網絡上。

  string templatePath = templetUrl;

            //新建Word工具對象
            WordUtil wordUtil = new WordUtil();
            //導入模版
            wordUtil.OpenTempelte(templatePath);
            //添加數據到書籤
            wordUtil.FillLable("Define_ID","08-1-502");
            wordUtil.FillLable("Operat_Time", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
          
            //因爲要保留模板,所以另存爲。
            wordUtil.SaveAs(System.Windows.Forms.Application.StartupPath +"\\testResult.doc", true);
            

            //操作完成,退出Word工具系統。
            wordUtil.Quit();

 使用上面代碼就可以生成一個新的word文檔

第二點就是利用Microsoft.Office.Interop.Word中PrintOut功能。

因爲.net下打印功能和.word下打印功能有點出入

轉載

Background 如果爲 true,則可以讓自定義項代碼在打印文檔時繼續工作。
Append 如果爲 true,則會將文檔追加到 OutputFileName 參數指定的文件;如果爲 false,則會改寫 OutputFileName 的內容。
Range 頁面範圍。可以是任何 WdPrintOutRange 值。
OutputFileName 如果 PrintToFile 爲 true,則此參數指定輸出文件的路徑和文件名。
From 當 Range 設置爲 wdPrintFromTo 時的起始頁碼。
To 當 Range 設置爲 wdPrintFromTo 時的結束頁碼。
Item 要打印的項。可以是任何 WdPrintOutItem 值。
Copies 要打印的份數。
Pages 要打印的頁碼和頁碼範圍,由逗號分隔。例如,“2, 6-8”意爲打印第 2 和第 6、7、8頁。
PageType 要打印的頁面的類型。可以是任何 WdPrintOutPages 值。
PrintToFile 如果爲 true,則將打印機指令發送到文件。請確保使用 OutputFileName 指定一個文件名。
Collate 在打印多份文檔時,如果爲 true,則先打印該文檔的所有頁,然後再打印下一份。
ActivePrinterMacGX 此參數僅在 Microsoft Office Macintosh Edition 中可用。
ManualDuplexPrint 如果爲 true,則在沒有雙面打印裝置的打印機上打印雙面文檔。如果此參數爲 true,則忽略 PrintBackground 和 PrintReverse 屬性。
PrintZoomColumn 希望 Word 在一頁上水平佈置的頁數。可以爲 1、2、3 或 4。PrintZoomRow 希望 Word 在一頁上垂直佈置的頁數。可以爲 1、2 或 4。
PrintZoomPaperWidth 希望 Word 將打印頁縮放到的寬度(以緹表示,20 緹 = 1 磅)。

PrintZoomPaperHeight 希望 Word 將打印頁縮放到的高度(以緹表示)。

第三點,分享一個鏈接

http://technet.microsoft.com/zh-cn/library/microsoft.office.interop.word%28v=office.11%29.aspx

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章