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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值