我测试了几种打印文档的方案,第一个方案测试过程中发现打印的都是乱码,后来我发现,word文档好像不能以流的方式读取,这个还有待研究。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Printing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Microsoft.Office.Interop.Word;
using Spire.Doc;namespace PrintTestPro
{public partial class Form1 : Form{public Form1(){InitializeComponent();}private System.Drawing.Font printFont;private StreamReader streamToPrint;private void button1_Click(object sender, EventArgs e){//这种方式打印,打印不了word文件,我不确定word能不能以流的方式读取try{//streamToPrint = new StreamReader("D:\\Git\\wordTemplateTest\\wordTemplateTest\\Template\\myfile.doc", Encoding.Default);streamToPrint = new StreamReader("E:\\MyFile.txt", Encoding.Default);try{printFont = new System.Drawing.Font("Arial", 10);PrintDocument pd = new PrintDocument();pd.PrintPage += new PrintPageEventHandler(this.pd_PrintPage);跳出打印对话框,提供打印参数可视化设置,如选择哪个打印机打印此文档等//PrintDialog pdg = new PrintDialog();//pdg.Document = pd;//if (DialogResult.OK == pdg.ShowDialog()) //如果确认,将会覆盖所有的打印参数设置//{//    //打印预览//    PrintPreviewDialog ppd = new PrintPreviewDialog();//    ppd.Document = pd;//    if (DialogResult.OK == ppd.ShowDialog())//    {//        pd.Print(); //打印//    }//}pd.Print(); //打印}finally{streamToPrint.Close();}}catch (Exception ex){MessageBox.Show(ex.Message);}}// The PrintPage event is raised for each page to be printed.private void pd_PrintPage(object sender, PrintPageEventArgs ev){float linesPerPage = 0;float yPos = 0;int count = 0;float leftMargin = ev.MarginBounds.Left;float topMargin = ev.MarginBounds.Top;string line = null;// Calculate the number of lines per page.linesPerPage = ev.MarginBounds.Height /printFont.GetHeight(ev.Graphics);// Print each line of the file.while (count < linesPerPage &&((line = streamToPrint.ReadLine()) != null)){//Console.WriteLine(line);yPos = topMargin + (count *printFont.GetHeight(ev.Graphics));ev.Graphics.DrawString(line, printFont, Brushes.Black,leftMargin, yPos, new StringFormat());count++;}// If more lines exist, print another page.if (line != null)ev.HasMorePages = true;elseev.HasMorePages = false;}private void button2_Click(object sender, EventArgs e){wordOperate();}public static void wordOperate(){Object oMissing = System.Reflection.Missing.Value;Microsoft.Office.Interop.Word.Application wordApp = new Microsoft.Office.Interop.Word.Application();Microsoft.Office.Interop.Word.Document wordDoc = new Microsoft.Office.Interop.Word.Document();try{//Set no alerts as printing in background.wordApp.DisplayAlerts = Microsoft.Office.Interop.Word.WdAlertLevel.wdAlertsNone;//Set the printer.默认打印机//wordApp.ActivePrinter = printerName;wordDoc = wordApp.Documents.Open("D:\\Git\\wordTemplateTest\\wordTemplateTest\\Template\\myfile.doc");wordDoc.Activate();//设置打印纸张wordDoc.PageSetup.PaperSize = WdPaperSize.wdPaperA5;//wordDoc.PageSetup.PaperSize = WdPaperSize.wdPaperA4;wordDoc.PrintOut(); //打印}catch (Exception ex){}finally{wordDoc.Close(SaveChanges: false);wordApp.Application.Quit();}}private void button3_Click(object sender, EventArgs e){//第三方库封装的word操作库// 创建一个Documnet类对象Spire.Doc.Document doc = new Spire.Doc.Document();// 加载需要打印的Word文档doc.LoadFromFile(@"D:\\Git\\wordTemplateTest\\wordTemplateTest\\Template\\myfile.doc");// 实例化System.Windows.Forms.PrintDialog对象PrintDialog dialog = new PrintDialog();dialog.AllowPrintToFile = true;dialog.AllowCurrentPage = true;dialog.AllowSomePages = true;dialog.UseEXDialog = true;// 关联doc.PrintDialog属性和PrintDialog对象 doc.PrintDialog = dialog;// 后台打印PrintDocument printDoc = doc.PrintDocument;        // printDoc.Print();// 显示打印对话框并打印if (dialog.ShowDialog() == DialogResult.OK){printDoc.Print();}}private void button4_Click(object sender, EventArgs e){//这种方式也可以实现打印word文档using (PrintDialog pd = new PrintDialog()){pd.ShowDialog();ProcessStartInfo info = new ProcessStartInfo("D:\\闸机人证核查系统客户端使用说明 - V1.0.doc");info.Verb = "PrintTo";info.Arguments = pd.PrinterSettings.PrinterName;info.UseShellExecute = true;info.CreateNoWindow = false;info.WindowStyle = ProcessWindowStyle.Hidden;Process.Start(info);}}}
}

C# 打印文档(word文档)相关推荐

  1. wps文件一点打印就关闭打印机服务器,word打印闪退 word文档一打印就闪退

    是的打印机设置问题,可能是某个打印机服务没有打开当添加打印机或是使用打印机时,系统报错"打印后台程序服务没有运行",一般会发生在 Windows 2000.XP.2003 系统下, ...

  2. 如何在微信公众号推文加入Word文档、Excel表格,超实用30秒学会

    大家都知道,当我们订阅了公众号(关注公众号),公众号的运营者就能给我们推送最新发布的公众号文章,我们也能在订阅号栏收到最新的文章.有一些微信公众号推文中会有附件,如word文档.excel表格,比如政 ...

  3. 计算机作业word样文图片,word文档中插入图片学案

    word文档中插入图片学案 一.教学目的: 1.使学生学会在文章中如何插入图片,并调整图片周围文字的排版方式,去美化自己的文章. 2.培养学生的自主学习能力,合作学习能力,创新能力和动手操作能力.进一 ...

  4. java打印/导出自定义word文档

    因为采用的替换,所以word模板对应的空格必须与导出实体类的变量名/键名对应 如 @ResourcePoiUtils poiUtils;@AutowiredHttpServletResponse re ...

  5. 破解word只读文档 word文档保护后的破解办法

    1.首先用Word 2003打开已设置有密码的"保护文档"(原始DOC文件),此文档可由Word 2000/XP(2002)/2003创建:  2.在菜单中选择"文件→另 ...

  6. 想要打印大量Word文档在哪里打印比较便宜

    如果您想要打印大量的Word文档的话,您会选择在哪里打印呢?是选择在线下打印复印店,还是线上打印复印店呢?一般大批量打印文件,小编推荐大家可以到线上打印平台去打印.选择线上打印要比线下打印的价格低,而 ...

  7. C# 如何打印Word文档

    文档打印在工作中很常用,本篇文章将介绍一种在C#中通过第三方免费组件Free Spire.Doc for .NET来实现Word文档中打印的方法.这里提供了两种可供选择的打印方法,即静默打印和弹出对话 ...

  8. Word文档怎么横向排版?这三种方法简单高效专业

    我们在利用Word文档处理日常工作时,为了文档的美观,我们会经常对Word文档进行各种形式的排版,比如对Word文档进行横向排版.如果有小伙伴不知道该如何进行横向排版,那么今天小编将要向大家分享三个横 ...

  9. C#操作Word文档

    1.c#操作word 在指定书签插入文字或者图片 using Word = Microsoft.Office.Interop.Word; object Nothing = System.Reflect ...

  10. 安卓手机如何将Word文档转换成PDF

    相信大家大会去打印机去打印文件,我们大部分都是拿着Word文档去打印,但是Word文档 受软件版本的限制,它会出现格式错乱或者排版不正确. 但是PDF文档就不会受软件版本以及电脑字体的影响而发生排版. ...

最新文章

  1. JAVA一个项目的路径为_java 得到项目路径
  2. 【SpringCloud】Hystrix:熔断
  3. gdb C++程序coredump不显示行号问题
  4. Python-requests请求的超时时间
  5. DigSci科学数据挖掘大赛:如何在3天内拿下DigSci亚军
  6. CentOS7的/tmp目录自动清理规则(转)
  7. 0-1背包问题(物品不可分割)
  8. 【Breadth-first Search 】279. Perfect Squares
  9. Jquery Uploadify插件+Servlet解决FTP多文件上传
  10. 李明顺专栏周5月12日:给门户支招
  11. 【答题卡识别】基于matlab形态学答题卡识别【含Matlab源码 1135期】
  12. matlab7.0窗口教程,MATLAB7.0实用教程
  13. 【linux 学习】linux上安装Tim(linux mint)
  14. matlab 平滑曲线连接_用MATLAB做数据拟合究竟有多直观
  15. 再探幻读!什么是幻读?为什么会产生幻读,MySQL中是怎么解决幻读的?
  16. java field 字段类型_Java Field类
  17. 树莓派HDMI转VGA线有无源
  18. nosqlbooster 延长试用日期
  19. 工作能力强的人,都有哪些特点?
  20. 《Spring+Spring MVC+MyBatis从零开始学》傻瓜式学习笔记

热门文章

  1. CCS 使用报错合集 -mcu:cc26xx
  2. 创作者运营—创作者课程开发思路
  3. windows SVN server
  4. HihoCoder上网络流算法题目建模总结
  5. QIIME2得到PICRUSt2结果后如何分析
  6. 老股民箱底翻出发黄纸质股票 如今价值超过20万
  7. AttributeError: Can‘t get attribute ‘SPPF‘ on <module ‘models.common‘ from ‘/yolov5-5.0/models/commo
  8. Tasker实现的app界面实时翻译 - 界面翻译4.0
  9. 认知智能整体技术框架简介介绍
  10. python的numpy教程_ROS与Python入门教程-使用numpy