C#利用word(模板)标签功能打印和form自带的打印功能

利用word标签功能打印实现

 public void DocumentPrint(Request request)
        {
            Microsoft.Office.Interop.Word.Application app = null;
            Microsoft.Office.Interop.Word.Document doc = null;

            object missing = System.Reflection.Missing.Value;
            object templateFile = @"D:\Downloads\试卷命题印刷流程单.doc";
            try
            {
                app = new Microsoft.Office.Interop.Word.ApplicationClass();                
                doc = app.Documents.Add(ref templateFile, ref missing, ref missing, ref missing);

               try
                {
                    #region 替换书签
                    object markName = "书签名";
                    Microsoft.Office.Interop.Word.Bookmark bm = doc.Bookmarks.get_Item(ref markName);
                       .
                       .
                       .
                    #endregion 

                }
                catch
                {
                    
                }      
                //打印
                app.Visible = false;
                //doc.PrintPreview();//要预览必须先把app.Visible设置成true
                doc.PrintOut(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, ref missing);
            }
            catch (Exception exp)
            {
                throw new Exception(exp.Message);
            }

            //销毁word进程
            finally
            {
                object saveChange = Microsoft.Office.Interop.Word.WdSaveOptions.wdDoNotSaveChanges;
                if (doc != null)
                    doc.Close(ref saveChange, ref missing, ref missing);

                if (app != null)
                    app.Quit(ref missing, ref missing, ref missing);
            }
        }

利用form自带的打印功能

public void DocumentPrint(PrintDocument docToPrint)
        {
            //打印开始前
            docToPrint.BeginPrint += new PrintEventHandler(docToPrint_BeginPrint);
            //打印输出(过程)
            docToPrint.PrintPage += new PrintPageEventHandler(docToPrint_PrintPage);
            //打印结束
            docToPrint.EndPrint += new PrintEventHandler(docToPrint_EndPrint);

       PrintDocument docToPrint
= new PrintDocument(); PrintDialog pd = new PrintDialog(); //string filepath = @"D:\Downloads\试卷命题印刷流程单.doc"; //fs = new FileStream(filepath, FileMode.Open, FileAccess.Read); pd.Document = docToPrint; if (DialogResult.OK == pd.ShowDialog()) //如果确认,将会覆盖所有的打印参数设置 { //页面设置对话框(可以不使用,其实PrintDialog对话框已提供页面设置) PageSetupDialog psd = new PageSetupDialog(); psd.Document = docToPrint; if (DialogResult.OK == psd.ShowDialog()) { //打印预览 PrintPreviewDialog ppd = new PrintPreviewDialog(); ppd.Document = docToPrint; if (DialogResult.OK == ppd.ShowDialog()) docToPrint.Print(); //打印 } } } private void docToPrint_BeginPrint(object sender, PrintEventArgs e) { //也可以把一些打印的参数放在此处设置 } private void docToPrint_PrintPage(object sender, PrintPageEventArgs e) { //打印啥东东就在这写了 Graphics g = e.Graphics; Brush b = new SolidBrush(Color.Black); Font titleFont = new Font("宋体", 16); string title ="";//这里是打印内容 g.DrawString(title, titleFont, b, new PointF((e.PageBounds.Width - g.MeasureString(title, titleFont).Width) / 2, 20)); //e.Cancel//获取或设置是否取消打印 //e.HasMorePages //为true时,该函数执行完毕后还会重新执行一遍(可用于动态分页) } private void docToPrint_EndPrint(object sender, PrintEventArgs e) { //打印结束后相关操作 }

两种方法都不是特别完美。第一种可以打印图片,打印文件,功能比较强大,但是如果想要模板的话很麻烦

第二种利用了word标签功能,这种方法用模板很方便,仅仅是简单打印。稍微复杂点就略显疲惫了

转载于:https://www.cnblogs.com/sunny-cheung/archive/2013/04/10/3012890.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
首先机子上安装有office,在COM中添加引用Microsoft.Word.11.0.Object.Library(或11.0以上) Microsoft.Office.Interop.Word.Application myWordApp = null; Microsoft.Office.Interop.Word.Document doc = null; object Filename = path + "\\" + TaskID + ".docx";//目的文件 object templateFile = System.Windows.Forms.Application.StartupPath + @"\Template.docx";//模板文件,有一个五列一行的表 System.IO.File.Copy(templateFile.ToString(), Filename.ToString(), true);//模板WORD中有一个五列的表头,分别是卡号,串口号,发送指令条数,接收指令条数,收发成功率 myWordApp = new Microsoft.Office.Interop.Word.Application(); doc = myWordApp.Documents.Open(ref Filename, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing); /////显示页码 object oAlignment = Microsoft.Office.Interop.Word.WdPageNumberAlignment.wdAlignPageNumberCenter; object oFirstPage = true; oAlignment = Microsoft.Office.Interop.Word.WdPageNumberAlignment.wdAlignPageNumberCenter; myWordApp.Selection.Sections[1].Footers[Microsoft.Office.Interop.Word.WdHeaderFooterIndex.wdHeaderFooterPrimary].PageNumbers.Add(ref oAlignment, ref oFirstPage); myWordApp.Selection.Sections[1].Footers[Microsoft.Office.Interop.Word.WdHeaderFooterIndex.wdHeaderFooterPrimary].PageNumbers.NumberStyle = Microsoft.Office.Interop.Word.WdPageNumberStyle.wdPageNumberStyleNumberInDash; for (int i = 2; i < 102; i++)//举例100台 { doc.Tables[1].Rows.Add(ref Nothing);//表格增加一行 doc.Tables[1].Cell(i, 1).Range.Text = "250297";//卡号 doc.Tables[1].Cell(i, 2).Range.Text = "COM12";//串口号 doc.Tables[1].Cell(i, 3).Range.Text = "100";//发送指令条数 doc.Tables[1].Cell(i, 4).Range.Text = "99";//接收指令条数 doc.Tables[1].Cell(i, 5).Range.Text = "99%";//收发成功率 } doc.SaveAs(ref Filename, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing); object savechanges = Microsoft.Office.Interop.Word.WdSaveOptions.wdDoNotSaveChanges;//不保存挂起的更改 ////下面是直接打印,文档不显示 //doc.PrintOut(ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing); object readOnly=false; object isVisable = true;////文档打开状态为可视 doc = myWordApp.Documents.Open(ref Filename, ref Nothing, ref readOnly, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref isVisable, ref Nothing, ref Nothing, ref Nothing, ref Nothing); doc.PrintPreview();//打印预览
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值