博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C#中5步完成word文档打印的方法
阅读量:4302 次
发布时间:2019-05-27

本文共 1835 字,大约阅读时间需要 6 分钟。

在日常工作中,我们可能常常需要打印各种文件资料,比如word文档。对于编程员,应用程序中文档的打印是一项非常重要的功能,也一直是一个非常复杂的工作。特别是提到Web打印,这的确会很棘手。一般如果要想选择非默认打印机或者说想显示打印设置对话框时,我们也需要对代码进行一定的设置。

针对这样的问题,今天这篇文章我就来分享一下如何利用第三方组件 来实现Word文档打印。

 详细步骤

这是原来的word文档截图:

第一步:组件安装后,创建一个C#控制台项目,添加引用及命名空间如下:

1

2

3

using System;

using Spire.Doc;

using System.Windows.Forms;

第二步:实例化一个word文档对象,调用LoadFromFile方法加载待打印的word文档:

1

2

Document doc = new Document();

doc.LoadFromFile("sample.doc");

第三步:实例化一个PrintDialog的对象,设置相关属性。关联doc.PrintDialog属性和PrintDialog对象:

1

2

3

4

5

6

PrintDialog dialog = new PrintDialog();

dialog.AllowPrintToFile = true;

dialog.AllowCurrentPage = true;

dialog.AllowSomePages = true;

dialog.UseEXDialog = true;                    

doc.PrintDialog = dialog;

 第四步: 后台打印。使用默认打印机打印出所有页面。这段代码也可以用于网页后台打印:

1

2

PrintDocument printDoc = doc.PrintDocument;

printDoc.Print();

 第五步:  如要显示打印对话框,就调用ShowDialog方法,根据打印预览设置选项,打印word文档:

1

2

3

4

if (dialog.ShowDialog() == DialogResult.OK)

{              

printDoc.Print();

}

 这是打印文档过后XPS格式的屏幕截图:

全部代码:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

using System;

using Spire.Doc;

using System.Windows.Forms;

 

namespace Doc_Print

{

    public partial class Form1 : Form

    {

        public Form1()

        {

            InitializeComponent();

        }

 

        private void button1_Click(object sender, EventArgs e)

        {

            // 实例化一个word文档对象

            Document doc = new Document();

            // 加载文档

            doc.LoadFromFile(@"C:\Users\Administrator\Desktop\示例文档.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();

            }

 

        }

    }

}

有兴趣的朋友自己也可以试一下, 谢谢浏览!

转载地址:http://lwlws.baihongyu.com/

你可能感兴趣的文章
OpenGL ES 3.0(四)图元、VBO、VAO
查看>>
OpenGL ES 3.0(五)纹理
查看>>
OpenGL ES 3.0(八)实现带水印的相机预览功能
查看>>
OpenGL ES 3.0(九)实现美颜相机功能
查看>>
FFmpeg 的介绍与使用
查看>>
Android 虚拟机简单介绍——ART、Dalvik、启动流程分析
查看>>
原理性地理解 Java 泛型中的 extends、super 及 Kotlin 的协变、逆变
查看>>
FFmpeg 是如何实现多态的?
查看>>
FFmpeg 源码分析 - avcodec_send_packet 和 avcodec_receive_frame
查看>>
FFmpeg 新旧版本编码 API 的区别
查看>>
RecyclerView 源码深入解析——绘制流程、缓存机制、动画等
查看>>
Android 面试题整理总结(一)Java 基础
查看>>
Android 面试题整理总结(二)Java 集合
查看>>
学习笔记_vnpy实战培训day02
查看>>
学习笔记_vnpy实战培训day03
查看>>
VNPY- VnTrader基本使用
查看>>
VNPY - CTA策略模块策略开发
查看>>
VNPY - 事件引擎
查看>>
MongoDB基本语法和操作入门
查看>>
学习笔记_vnpy实战培训day04_作业
查看>>