C#如何指定打印机扫描pdf,打印已有的PDF文件

PrintPdfReport
说明:&&C#打印pdf文件,windows平台下使用C#代码打印PDF文件的逻辑(C# printing PDF file logic)
文件列表:
PrintPdfReport.exe
近期下载者:
相关文件:C#获取指定PDF文件页数的方法
转载 &更新时间:日 15:02:53 & 作者:work24
这篇文章主要介绍了C#获取指定PDF文件页数的方法,涉及C#操作pdf文件的技巧,非常具有实用价值,需要的朋友可以参考下
本文实例讲述了C#获取指定PDF文件页数的方法。分享给大家供大家参考。具体如下:
using System.IO;
using System.Text.RegularE
using System.Windows.F
namespace RobvanderWoude
class PDFPageCount
static int Main( string[] args )
#region Get help
if ( args.Length == 0 )
ShowHelp( );
foreach ( string arg in args )
if ( arg == "/?" || arg == "-?" || arg.ToLower( ) == "--help" )
ShowHelp( );
#endregion
int errors = 0;
foreach ( string arg in args )
Regex regexp = new Regex( @"^(.*)\\([^\\]+\.pdf)$", RegexOptions.IgnoreCase );
if ( regexp.IsMatch( arg ) )
// Match means the filespec has a valid format (i.e. *.pdf)
string[] matches = regexp.Split( arg );
string folder = matches[1];
string filespec = matches[2];
if ( Directory.Exists( folder ) )
// Folder exists, check for matching files
string[] fileList = Directory.GetFiles( folder, filespec );
if ( fileList.Length == 0 )
// No matching files in this folder
ShowError( "ERROR: No files matching \"{0}\" were found in \"{1}\"", filespec, folder );
errors += 1;
// Iterate through list of matching files
foreach ( string file in fileList )
int pagecount = PageCount( file );
if ( pagecount == -1 )
// Just increase the error count, the PageCount( )
// procedure already wrote an error message to screen
errors += 1;
// No pages means there is a problem with the file
if ( pagecount == 0 )
Console.ForegroundColor = ConsoleColor.R
errors += 1;
// Display the formated result on screen
Console.WriteLine( "{0,4} {1,-10} {2}", pagecount.ToString( ), ( pagecount == 1 ? "page" : "pages" ), file );
if ( pagecount == 0 )
Console.ForegroundColor = ConsoleColor.G
// Folder doesn't exist
ShowError( "ERROR: Folder \"{0}\" not found", folder );
errors += 1;
// No match for the regular expression means the filespec was invalid
ShowError( "ERROR: Invalid filespec \"{0}\", please specify PDF files only", arg );
errors += 1;
catch ( Exception e )
// All other errors: display an error message and then continue
ShowError( "ERROR: {0}", e.Message );
errors += 1;
if ( errors != 0 )
ShowError( "
{0} finished with {1} error{2}", GetExeName( ), errors.ToString( ), ( errors == 1 ? "" : "s" ) );
static string GetExeName( )
string exe = Application.ExecutablePath.ToString( );
Regex regexp = new Regex( @"\\([^\\]+)$" );
return regexp.Split( exe )[1];
static int PageCount( string filename )
Regex regexp = new Regex( @"\.pdf$", RegexOptions.IgnoreCase );
if ( regexp.IsMatch( filename ) )
FileStream fs = new FileStream( filename, FileMode.Open, FileAccess.Read );
StreamReader sr = new StreamReader( fs );
string pdfText = sr.ReadToEnd( );
regexp = new Regex( @"/Type\s*/Page[^s]" );
MatchCollection matches = regexp.Matches( pdfText );
return matches.C
catch ( Exception e )
ShowError( "ERROR: {0} ({1})", e.Message, filename );
return -1;
ShowError( "ERROR: {0} is not a PDF file", filename );
return -1;
static void ShowError( string message, string param1, string param2 = "", string param3 = "" )
Console.Error.WriteLine( );
Console.ForegroundColor = ConsoleColor.R
Console.Error.WriteLine( message, param1, param2, param3 );
Console.ForegroundColor = ConsoleColor.G
Console.Error.WriteLine( );
#region Display help text
static void ShowHelp( )
Console.Error.WriteLine( );
Console.Error.WriteLine( "{0}, Version 1.02", GetExeName( ) );
Console.Error.WriteLine( "Return the page count for the specified PDF file(s)" );
Console.Error.WriteLine( );
Console.Error.WriteLine( "Usage: {0} filespec [ filespec [ filespec [ ... ] ] ]", GetExeName( ).ToUpper( ) );
Console.Error.WriteLine( );
Console.Error.WriteLine( "Where: \"filespec\"
is a file specification for the PDF file(s) to" );
Console.Error.WriteLine( "
be listed (wildcards * and ? are allowed)" );
Console.Error.WriteLine( );
Console.Error.WriteLine( "Note:
The program's return code equals the number of errors encountered." );
Console.Error.WriteLine( );
Console.Error.WriteLine( "Written by Rob van der Woude" );
#endregion
希望本文所述对大家的C#程序设计有所帮助。
您可能感兴趣的文章:
大家感兴趣的内容
12345678910
最近更新的内容
常用在线小工具C#调用PDF的打印功能_百度知道
C#调用PDF的打印功能
我要在网页中显示PDF文件,然后打印这个文件,因为文件打印后我要做下一个操作,所以不能用adobe自身的打印功能,需要将打印按钮隐藏(这个怎么实现?),然后在用外部的Button调用打印功能,希望各位大虾指点谢谢啦
我有更好的答案
利用Adobe PDF Reader 控件,在Winform中实现打印PDF文档。 实现方法如下:(1)前提条件必须事先在计算机安装Adobe Reader软件。Adobe Reader是免费软件,可以从Adobe官网下载安装(2)在Visual Studio中新建一个“Windows 窗体应用程序”项目(3)在工具箱上点鼠标右键--&选择项 --&COM组件--&Adobe PDF Reader(4)在窗体Form1上布置一个Adobe PDF Reader控件和两个Button控件(5)Form1窗体代码Form1.csusing&Susing&System.Windows.Fusing&System.IO;namespace&WindowsFormsApplication1{&&&&public&partial&class&Form1&:&Form&&&&{&&&&&&&&public&Form1()&&&&&&&&{&&&&&&&&&&&&InitializeComponent();&&&&&&&&&&&&&&&&&&&&&&&&button1.Text&=&&打开PDF文档...&;&&&&&&&&&&&&//&没有载入PDF文档时,禁用打印功能&&&&&&&&&&&&button2.Text&=&&打印...&;&&&&&&&&&&&&button2.Enabled&=&&&&&&&&&}&&&&&&&&private&void&button1_Click(object&sender,&EventArgs&e)&&&&&&&&{&&&&&&&&&&&&OpenFileDialog&openDlg&=&new&OpenFileDialog();&&&&&&&&&&&&openDlg.Filter&=&&PDF文档|*.pdf&;&&&&&&&&&&&&if&(openDlg.ShowDialog()&==&DialogResult.OK)&&&&&&&&&&&&{&&&&&&&&&&&&&&&&axAcroPDF1.LoadFile(openDlg.FileName);&&&&&&&&&&&&&&&&//&有载入PDF文档时,允许打印功能&&&&&&&&&&&&&&&&button2.Enabled&=&&&&&&&&&&&&&}&&&&&&&&}&&&&&&&&private&void&button2_Click(object&sender,&EventArgs&e)&&&&&&&&{&&&&&&&&&&&&&&&&//&显示打印PDF文档对话框&&&&&&&&&&&&axAcroPDF1.printWithDialog();&&&&&&&&}&&&&}}(6)运行效果
采纳率:87%
来自团队:
在网页中么?使用javascript的打印不可以么~&div align='center'&&input type='button' value='打印' onClick='window.print();'&&/div& 也可以控制,打印某一部分
本回答被提问者采纳
为什么用adobe的自身打印功能就不能做下一个操作了呢,行的。
为您推荐:
其他类似问题
您可能关注的内容
pdf的相关知识
换一换
回答问题,赢新手礼包
个人、企业类
违法有害信息,请在下方选择后提交
色情、暴力
我们会通过消息、邮箱等方式尽快将举报结果通知您。打印,是做开发的人的经久不变的话题。
今天,用实例代码,说明.NET是如何打印WORD、EXCEL等OFFICE文件,以及PDF文件的。
采用指定的打印机打印OFFICE文件
此方法又分为 “显示相应的程序窗口” 和 “不显示相应的程序窗口”两种方式。
(1) 显示WORD、EXCEL等程序窗口
采用操作系统自身的自动识别模式的打印,此方法实际适用于N多种文件,并不限于WORD,EXCEL,PDF之类的,但是这种方法,有一个缺陷,就是:对于某些类型的文档,如WORD,EXCEL,PDF等,打印时,会有相应的程序窗口一闪而过。
实现代码如下:
System.Diagnostics.Process p = new System.Diagnostics.Process();
//不现实调用程序窗口,但是对于某些应用无效
p.StartInfo.CreateNoWindow = true;
p.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
//采用操作系统自动识别的模式
p.StartInfo.UseShellExecute = true;
//要打印的文件路径,可以是WORD,EXCEL,PDF,TXT等等
p.StartInfo.FileName = @"d:\a.doc";
//指定执行的动作,是打印,即print,打开是 open
p.StartInfo.Verb = "print";
p.Start();
此种方法,代码简单,性能好,可靠稳定。此种方式,如果要指定打印机,则只能利用设置默认打印机的方式来实现。
C#设置系统默认打印机的实现方法,见 “” 一文
添加了指定打印机功能的代码如下:
System.Diagnostics.Process p = new System.Diagnostics.Process();
//不现实调用程序窗口,但是对于某些应用无效
p.StartInfo.CreateNoWindow = true;
p.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
//采用操作系统自动识别的模式
p.StartInfo.UseShellExecute = true;
//要打印的文件路径
p.StartInfo.FileName = @"d:\a.doc";
//指定执行的动作,是打印,即print,打开是 open
p.StartInfo.Verb = "print";
//获取当前默认打印机
//string defaultPrinter = GetDefaultPrinter();
//将指定的打印机设为默认打印机
SetDefaultPrinter("指定的打印机");
//开始打印
p.Start();
//等待十秒
p.WaitForExit(10000);
//将默认打印机还原
SetDefaultPrinter(defaultPrinter);
(2) 不显示WORD、EXCEL等程序窗口
此种方式,使用.NET调用COM的方式来实现,利用COM对象本身的特性来设置可见性和打印机
使用此方法前,需要先添加Office的COM引用,这里略过。
//要打印的文件路径
object wordFile = @"d:\a.doc";
object oMissing = Missing.Value;
//自定义object类型的布尔值
object oTrue = true;
object oFalse = false;
object doNotSaveChanges = Word.WdSaveOptions.wdDoNotSaveChanges;
//定义WORD Application相关
Word.Application appWord = new Word.Application();
//WORD程序不可见
appWord.Visible = false;
//不弹出警告框
appWord.DisplayAlerts = Word.WdAlertLevel.wdAlertsNone;
//先保存默认的打印机
string defaultPrinter = appWord.ActivePrinter;
//打开要打印的文件
Word.Document doc = appWord.Documents.Open(
ref wordFile,
ref oMissing,
ref oTrue,
ref oFalse,
ref oMissing,
ref oMissing,
ref oMissing,
ref oMissing,
ref oMissing,
ref oMissing,
ref oMissing,
ref oMissing,
ref oMissing,
ref oMissing,
ref oMissing,
ref oMissing);
//设置指定的打印机
appWord.ActivePrinter = "指定打印机的名字";
doc.PrintOut(
ref oTrue, //此处为true,表示后台打印
ref oFalse,
ref oMissing,
ref oMissing,
ref oMissing,
ref oMissing,
ref oMissing,
ref oMissing,
ref oMissing,
ref oMissing,
ref oMissing,
ref oMissing,
ref oMissing,
ref oMissing,
ref oMissing,
ref oMissing,
ref oMissing,
ref oMissing
//打印完关闭WORD文件
doc.Close(ref doNotSaveChanges, ref oMissing, ref oMissing);
//还原原来的默认打印机
appWord.ActivePrinter = defaultPrinter;
//退出WORD程序
appWord.Quit(ref oMissing, ref oMissing, ref oMissing);
doc = null;
appWord = null;
此方法,于COM交互,性能有些损失,要注意COM对象的释放,以及异常控制,此处忽略。
阅读(...) 评论()打印 word pdf
PrintOut方法 - Snowfun - 博客园
随笔 - 612
文章 - 468
//要打印的文件路径&&&&&&&&&&&&object&wordFile&=&lstDoc.SelectedValue.ToString();//@"c:\test.doc";&&&&&&&&&&&&object&oMissing&=&Missing.V&&&&&&&&&&&&//自定义object类型的布尔值&&&&&&&&&&&&object&oTrue&=&true;&&&&&&&&&&&&object&oFalse&=&false;&&&&&&&&&&&&object&Copies&=&nudCopies.V&//打印份数&&&&&&&&&&&&object&wdPrintFrom&=&txtPage1.T//打印的起始页码&&&&&&&&&&&&object&wdPrintTo&=&txtPage2.T//打印的结束页码&&&&&&&&&&&&object&doNotSaveChanges&=&Microsoft.Office.Interop.Word.WdSaveOptions.wdDoNotSaveC&&&&&&&&&&&&//定义WORD&Application相关&&&&&&&&&&&&Microsoft.Office.Interop.Word.Application&appWord&=&new&Microsoft.Office.Interop.Word.Application();&&&&&&&&&&&&//WORD程序不可见&&&&&&&&&&&&appWord.Visible&=&false;&&&&&&&&&&&&//不弹出警告框&&&&&&&&&&&&appWord.DisplayAlerts&=&Microsoft.Office.Interop.Word.WdAlertLevel.wdAlertsN&&&&&&&&&&&&//先保存默认的打印机&&&&&&&&&&&&string&defaultPrinter&=&appWord.ActiveP&&&&&&&&&&&&//打开要打印的文件&&&&&&&&&&&&Microsoft.Office.Interop.Word.Document&doc&=&appWord.Documents.Open(&&&&&&&&&&&&&&&&ref&wordFile,&&&&&&&&&&&&&&&&ref&oMissing,&&&&&&&&&&&&&&&&ref&oTrue,&&&&&&&&&&&&&&&&ref&oFalse,&&&&&&&&&&&&&&&&ref&oMissing,&ref&oMissing,ref&oMissing,&ref&oMissing,&ref&oMissing,&ref&oMissing,&ref&oMissing,&ref&oMissing,&&&&&&&&&&&&&&&&ref&oMissing,ref&oMissing,ref&oMissing,&ref&oMissing);&&&&&&&&&&&&//设置指定的打印机&&&&&&&&&&&&appWord.ActivePrinter&=&cboInstalledPrinters.SelectedT&//"\\\\10.10.96.236\\HP&Deskjet&6500&Series";&&&&&&&&&&&&//打印&&&&&&&&&&&&doc.PrintOut(&&&&&&&&&&&&&&&&ref&oTrue,&//Background&此处为true,表示后台打印&&&&&&&&&&&&&&&&ref&oFalse,&&&&&&&&&&&&&&&&ref&oMissing,&//Range&页面范围&&&&&&&&&&&&&&&&ref&oMissing,&&&&&&&&&&&&&&&&ref&wdPrintFrom,&//当&Range&设置为&wdPrintFromTo&时的起始页码&&&&&&&&&&&&&&&&ref&wdPrintTo,//当&Range&设置为&wdPrintFromTo&时的结束页码&&&&&&&&&&&&&&&&ref&oMissing,&&&&&&&&&&&&&&&&ref&Copies,&&//要打印的份数&&&&&&&&&&&&&&&&ref&oMissing,ref&oMissing,&ref&oMissing&,&ref&oMissing,&ref&oMissing,ref&oMissing,&ref&oMissing,ref&oMissing,&ref&oMissing,&ref&oMissing);&&&&&&&&&&&&lblMessage.Text&=&"正在打印,请稍候!";&&&&&&&&&&&&//打印完关闭WORD文件&&&&&&&&&&&&doc.Close(ref&doNotSaveChanges,&ref&oMissing,&ref&oMissing);&&&&&&&&&&&&//还原原来的默认打印机&&&&&&&&&&&&appWord.ActivePrinter&=&defaultP&&&&&&&&&&&&//退出WORD程序&&&&&&&&&&&&appWord.Quit(ref&oMissing,&ref&oMissing,&ref&oMissing);&&&&&&&&&&&&doc&=&null;&&&&&&&&&&&&appWord&=&null;&&&&&&&&&&&&lblMessage.Text&=&"打印成功!";
Background
如果为 true,则可以让自定义项代码在 Microsoft Office Word 打印文档时继续工作。
如果为 true,则会将文档追加到 OutputFileName 参数指定的文件;如果为 false,则会改写 OutputFileName 的内容。
页面范围。可以是任何
OutputFileName
如果 PrintToFile 为 true,则此参数指定输出文件的路径和文件名。
当 Range 设置为
时的起始页码。
当 Range 设置为 wdPrintFromTo 时的结束页码。
要打印的项。可以是任何
要打印的份数。
要打印的页码和页码范围,由逗号分隔。例如,&#-10”意为打印第 2 页和第 6、7、8、9、10 页。
要打印的页面的类型。可以是任何
PrintToFile
如果为 true,则将打印机指令发送到文件。请确保使用 OutputFileName 指定一个文件名。
在打印多份文档时,如果为 true,则先打印该文档的所有页,然后再打印下一份。
ActivePrinterMacGX
此参数仅在 Microsoft Office Macintosh Edition 中可用。有关此参数的其他信息,请查询 Microsoft Office Macintosh Edition 附带的语言参考帮助。
ManualDuplexPrint
如果为 true,则在没有双面打印装置的打印机上打印双面文档。如果此参数为 true,则忽略
属性。使用
属性在手动进行双面打印时控制输出。您可能无法使用此参数,具体取决于您选择或安装的语言支持(例如,美国英语)。
PrintZoomColumn
希望 Word 在一页上水平布置的页数。可以为 1、2、3 或 4。与 PrintZoomRow 参数一起使用时可在单张纸上打印多页。
PrintZoomRow
希望 Word 在一页上垂直布置的页数。可以为 1、2 或 4。与 PrintZoomColumn 参数一起使用时可在单张纸上打印多页。
PrintZoomPaperWidth
希望 Word 将打印页缩放到的宽度(以缇表示,20 缇 = 1 磅,72 磅 = 1 英寸)。
PrintZoomPaperHeight
希望 Word 将打印页缩放到的高度(以缇表示,20 缇 = 1 磅,72 磅 = 1 英寸)。
阅读(...) 评论()}

我要回帖

更多关于 添加pdf打印机 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信