-
C# 打印PDF文件之使用不同打印机打印所有页面或部分页面
最近在逛国外各大编程社区论坛的时候,发现很多人都在问一个关于PDF文件打印的问题:打印时如何选择非默认打印机并设置打印页面的范围。而一般情况下,打印时选择的打印机是默认打印机,打印页面范围是所有页面,如果要选择其他打印机或打印部分页面,需要对代码进行一定的设置。通过在百度和各大网站上查找资料,终于在MSDN上找到了一个解决方案,我把它在这里分享出来,让其他有同样需求的朋友少走点冤枉路。
在该解决方案中我使用了一个免费的PDF控件,感兴趣的博友可以从Codeplex官网:https://freepdf.codeplex.com上下载。
下面是该解决方案的详细代码分步解析:
第一步:添加项目引用。
安装控件后,创建一个新的项目,找到控件的安装目录,在项目的“解决方案”窗口右击->添加引用,选择和项目.NET Framework版本对应的dll文件进行添加;
如下图:
第二步:使用命名空间。
在该方案中,我使用的命名空间如下:
1
2
3
|
using Spire.Pdf; using System.Windows.Forms; using System.Drawing.Printing; |
第三步:创建一个新的PDF文档,并加载待打印的PDF文件。
1
2
|
PdfDocument doc = new PdfDocument(); doc.LoadFromFile( "sample.pdf" ); |
如果需要使用默认打印机打印所有页面,请看第四步。如果需要使用其他打印机并设置打印页面范围,请看第五步。
第四步:使用默认打印机打印所有页面。
1
|
doc.PrintDocument.Print(); |
第五步:选择打印机和设置打印页面范围。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
PrintDialog dialogPrint = new PrintDialog(); dialogPrint.AllowPrintToFile = true ; dialogPrint.AllowSomePages = true ; dialogPrint.PrinterSettings.MinimumPage = 1; dialogPrint.PrinterSettings.MaximumPage = doc.Pages.Count; dialogPrint.PrinterSettings.FromPage = 1; dialogPrint.PrinterSettings.ToPage = doc.Pages.Count; if (dialogPrint.ShowDialog() == DialogResult.OK) { //设置打印的起始页码 doc.PrintFromPage = dialogPrint.PrinterSettings.FromPage; //设置打印的终止页码 doc.PrintToPage = dialogPrint.PrinterSettings.ToPage; //选择打印机 doc.PrinterName = dialogPrint.PrinterSettings.PrinterName; PrintDocument printDoc = doc.PrintDocument; dialogPrint.Document = printDoc; printDoc.Print(); } |
运行项目,输出的效果图如下(打印机和打印页面范围可以自己选择):
全部代码如下:
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
|
using Spire.Pdf; using System.Windows.Forms; using System.Drawing.Printing; namespace PrintPDF { class Program { static void Main( string [] args) { PdfDocument doc = new PdfDocument(); doc.LoadFromFile( "sample.pdf" ); //选择默认打印机打印所有页面 //doc.PrintDocument.Print(); //选择打印机并设置打印页面范围 PrintDialog dialogPrint = new PrintDialog(); dialogPrint.AllowPrintToFile = true ; dialogPrint.AllowSomePages = true ; dialogPrint.PrinterSettings.MinimumPage = 1; dialogPrint.PrinterSettings.MaximumPage = doc.Pages.Count; dialogPrint.PrinterSettings.FromPage = 1; dialogPrint.PrinterSettings.ToPage = doc.Pages.Count; if (dialogPrint.ShowDialog() == DialogResult.OK) { doc.PrintFromPage = dialogPrint.PrinterSettings.FromPage; doc.PrintToPage = dialogPrint.PrinterSettings.ToPage; doc.PrinterName = dialogPrint.PrinterSettings.PrinterName; PrintDocument printDoc = doc.PrintDocument; dialogPrint.Document = printDoc; printDoc.Print(); } } } } |
以上是我亲测以后觉得可用的方案,放在这里供大家参考,如果有什么不周到的地方,希望大家不吝赐教,谢谢!
原文链接:https://www.cnblogs.com/Yesi/p/5066835.html
栏目列表
最新更新
nodejs爬虫
Python正则表达式完全指南
爬取豆瓣Top250图书数据
shp 地图文件批量添加字段
爬虫小试牛刀(爬取学校通知公告)
【python基础】函数-初识函数
【python基础】函数-返回值
HTTP请求:requests模块基础使用必知必会
Python初学者友好丨详解参数传递类型
如何有效管理爬虫流量?
2个场景实例讲解GaussDB(DWS)基表统计信息估
常用的 SQL Server 关键字及其含义
动手分析SQL Server中的事务中使用的锁
openGauss内核分析:SQL by pass & 经典执行
一招教你如何高效批量导入与更新数据
天天写SQL,这些神奇的特性你知道吗?
openGauss内核分析:执行计划生成
[IM002]Navicat ODBC驱动器管理器 未发现数据
初入Sql Server 之 存储过程的简单使用
SQL Server -- 解决存储过程传入参数作为s
关于JS定时器的整理
JS中使用Promise.all控制所有的异步请求都完
js中字符串的方法
import-local执行流程与node模块路径解析流程
检测数据类型的四种方法
js中数组的方法,32种方法
前端操作方法
数据类型
window.localStorage.setItem 和 localStorage.setIte
如何完美解决前端数字计算精度丢失与数