C# 打印 word pdf PrintOut方法中OutputFileName参数问题

作者&投稿:赫话 (若有异议请与网页底部的电邮联系)
wordapplication控件怎么实现打印 就是printOut的参数应该怎么写~

打印到文件的一个例子,希望对你有帮助

string wordDoc= "c:est.doc";
string outFile= "c:est.doc.prn";
object wordFile = wordDoc;

object oMissing = Missing.Value;

//自定义object类型的布尔值
object oTrue = true;
object oFalse = false;
object oOutFile = outFile;


doc.PrintOut(
ref oTrue, // Background 此处为true,表示后台打印
ref oFalse, // Append true to append the document to the file specified by the OutputFileName argument; false to overwrite the contents of OutputFileName.
ref oMissing, // The page range. Can be any WdPrintOutRange value.
ref oOutFile, //If PrintToFile is true, this argument specifies the path and file name of the output file.
ref oMissing, //The starting page number when Range is set to wdPrintFromTo.
ref oMissing, //The ending page number when Range is set to wdPrintFromTo.
ref oMissing, //The item to be printed. Can be any WdPrintOutItem value.
ref oMissing, //The number of copies to be printed.
ref oMissing, //The page numbers and page ranges to be printed, separated by commas. For example, "2, 6-10" prints page 2 and pages 6 through 10.
ref oMissing, // The type of pages to be printed. Can be any WdPrintOutPages value.
ref oTrue, //PrintToFile true to send printer instructions to a file. Make sure to specify a file name with OutputFileName.
ref oMissing, //Collate When printing multiple copies of a document, true to print all pages of the document before printing the next copy.
ref oMissing, //ActivePrinterMacGX This argument is available only in Microsoft Office Macintosh Edition. For additional information about this argument, consult the language
//reference Help included with Microsoft Office Macintosh Edition.
ref oMissing, // ManualDuplexPrint 如果为 true,则在没有双面打印装置的打印机上打印双面文档。如果此参数为 true,则忽略 PrintBackground 和 PrintReverse 属性。
ref oMissing, // PrintZoomColumn 希望 Word 在一页上水平布置的页数。可以为 1、2、3 或 4。与 PrintZoomRow 参数一起使用时可在单张纸上打印多页
ref oMissing, // PrintZoomRow 希望 Word 在一页上垂直布置的页数。可以为 1、2 或 4。与 PrintZoomColumn 参数一起使用时可在单张纸上打印多页
ref oMissing, // PrintZoomPaperWidth 希望 Word 将打印页缩放到的宽度
ref oMissing // PrintZoomPaperHeight 希望 Word 将打印页缩放到的高度
);

如果没有什么特殊要求,直接无参
workbook.PrintOut(); //打印所有工作表
worksheet.PrintOut(); //打印工作表

一、
1)参数Append如果为 true,则会将文档追加到 OutputFileName 参数指定的文件;
2)参数Append如果为 false,则会改写 OutputFileName 的内容。
3)参数PrintToFile
如果为 true,则将打印机指令发送到文件。请确保使用 OutputFileName 指定一个文件名。

二、C#直接打印word文档
using usingMicrosoft.Office.Interop.Word;
(通过添加引用-com组件,找office的word组件
///<summary>
/// 打印word
/// </summary>
/// <paramname="filepath">word文件路径</param>
/// <paramname="printername">指定的打印机</param>
public void Printword(stringfilepath,string printername)
{
//filepath=@"d:\b.doc";
//printername = "Microsoft XPS Document Writer";
try
{
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 = filepath;
Help help = new Help();
help.LogMessage(filepath + "---------" + printername);
//指定执行的动作,是打印,即print,打开是 open
p.StartInfo.Verb = "print";
//获取当前默认打印机
string defaultPrinter = GetDefaultPrinter();
//将指定的打印机设为默认打印机
SetDefaultPrinter(printername);
//开始打印
p.Start();
//等待十秒
p.WaitForExit(10000);
//将默认打印机还原
SetDefaultPrinter(defaultPrinter);
}
catch(Exception ex)
{

help.LogMessage(filepath + "----" + printername + "-------"+ ex.Message);
}
}
[DllImport("Winspool.drv",CharSet = CharSet.Auto, SetLastError = true)]
private static extern boolSetDefaultPrinter(string printerName);
[DllImport("winspool.drv", CharSet = CharSet.Auto, SetLastError =true)]

private static extern boolGetDefaultPrinter(StringBuilder pszBuffer, ref int pcchBuffer);
/// <summary>
/// 获取默认的打印机
/// </summary>
/// <returns></returns>
static string GetDefaultPrinter()
{
const intERROR_FILE_NOT_FOUND = 2;
const int ERROR_INSUFFICIENT_BUFFER = 122;
int pcchBuffer = 0;
if (GetDefaultPrinter(null, ref pcchBuffer))
{
return null;
}
int lastWin32Error = Marshal.GetLastWin32Error();
if (lastWin32Error == ERROR_INSUFFICIENT_BUFFER)
{
StringBuilder pszBuffer = new StringBuilder(pcchBuffer);
if (GetDefaultPrinter(pszBuffer, ref pcchBuffer))
{
return pszBuffer.ToString();
}
lastWin32Error = Marshal.GetLastWin32Error();
}
if (lastWin32Error== ERROR_FILE_NOT_FOUND)
{
return null;
}
throw new Win32Exception(Marshal.GetLastWin32Error());

}
#region
///打印页面不会闪动
public void PrintMethodOther(stringfilepath,string printername)
{
try
{
object wordFile = filepath;
//@"d:\b.doc";
object oMissing =Missing.Value;
//自定义object类型的布尔值
object oTrue = true;
object oFalse = false;

object doNotSaveChanges = WdSaveOptions.wdDoNotSaveChanges;

//定义WORDApplication相关

Application appWord = new Application();

//WORD程序不可见
appWord.Visible = false;
//不弹出警告框
appWord.DisplayAlerts = WdAlertLevel.wdAlertsNone;

//先保存默认的打印机
string defaultPrinter = appWord.ActivePrinter;

//打开要打印的文件
//如果在其它函数调用中出错(doc为null),处理办法:建立临时文件夹,还要设置服务的权限属性
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 = printername;
//"Microsoft XPS Document Writer";

//打印
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;
}
catch(Exception ex)
{
help.LogMessage(filepath+"----"+printername+"-------"+ex.Message);
}
}

//// <summary>
///解决 word调用打印机报错问题,创建一个临时文件夹
/// </summary>
/// <returns></returns>
public bool CreateFolder()
{
boolifsuccesss = false;
try
{
string systempath =System.Environment.GetFolderPath(Environment.SpecialFolder.System);
string fullpath = string.Empty;
if (FindSystemWidth() == "32")
{
fullpath = systempath + "\\config\\systemprofile\\Desktop";
}
else
{
fullpath = systempath + "\\config\\systemprofile\\Desktop";
}
if (!Directory.Exists(fullpath))
{
Directory.CreateDirectory(fullpath);
}
ifsuccesss = true;
}
catch(Exception ex)
{
ifsuccesss = false;
}
returnifsuccesss;
}
/// <summary>
/// 获取系统的位数
/// </summary>
/// <returns></returns>
public string FindSystemWidth()
{
ConnectionOptions oConn = new ConnectionOptions();
System.Management.ManagementScope oMs = new System.Management.ManagementScope("\\\\localhost",oConn);
System.Management.ObjectQuery oQuery = newSystem.Management.ObjectQuery("select AddressWidth fromWin32_Processor");
ManagementObjectSearcher oSearcher = new ManagementObjectSearcher(oMs, oQuery);
ManagementObjectCollection oReturnCollection = oSearcher.Get();
stringaddressWidth = null;
foreach (ManagementObject oReturn in oReturnCollection)
{
addressWidth = oReturn["AddressWidth"].ToString();
}
return addressWidth;
}

遇到了同样的问题。
在内网尝试了各种方案都不可能。
在外网找到一个可用,但是似乎并不通用的解决方案。
在打印代码之前,写上设置活动打印机的代码
例如,ActivePrinter = "Microsoft Print to PDF"。
通过尝试,使用上述方式可以在指定保存路径和名称的情况下后台打印,并且可以打开pdf文档。
但是,如果使用下述活动打印机,ActivePrinter = "Adobe PDF",仍然会出现上述问题。

你解决了吗?


莎车县13739461646: C# 利用pdf虚拟打印机将word转换成pdf 博乐园
脂南枸橼: 前提条件是你电脑上有下载PDF虚拟打印机.在打印该文档的时候,点打印-选择打印机-选择PDF打印机-打印-保存于-名字更换-保存,就可以了.

莎车县13739461646: c# 如何调用打印机 打印PDF文件
脂南枸橼: 调用api弹出打印机属性对话框 Author:vitoriatang From:Internet .NET Framework封装了很多关于打印的对话框,比如说PrintDialog, PageSetupDialog. 但是有的时候我们还需要关心打印机属性对话框,那么就可以调用API来解决这个问题.有几...

莎车县13739461646: C# printDocument 直接打印Word文档
脂南枸橼: 用水晶报表,水晶报表能很方便打印为pdf,excel,word,powerpoint

莎车县13739461646: C#如何指定打印机,打印已有的PDF文件 -
脂南枸橼: 调用Spire.Pdf.dll,使用下面的代码可以指定打印机和要打印的文档 //创建PdfDocument对象 PdfDocument doc = new PdfDocument();//加载一个现有文档 doc.LoadFromFile(@"C:\Users\Administrator\Desktop\Test.pdf");//选择打印机 doc.PrintSettings.PrinterName = "HP LaserJet P1007";//选择打印页码范围 doc.PrintSettings.SelectPageRange(1, 10);//执行打印 doc.Print();

莎车县13739461646: C# 中 打印word文档问题
脂南枸橼: 可以自动提取word文档,但最好点刷新一下,好让内存重新获取打印内容;在打印选项里选择打印本页,不要全选

莎车县13739461646: 用C#语言编写一个打开PDF文档的程序 -
脂南枸橼: string path=@"d:\web\web.pdf"; System.Diagnostics.Process.Start(path);

莎车县13739461646: C#怎么调用openoffice将word转换成PDF - CSDN论坛 -
脂南枸橼: 1. 在 Linux 系统中,下载安装 WPS Office For Linux、LibreOffice、OpenOffice 等软件,打开 Word 文件,在另存为功能中,选择 PDF 格式文件,或者在打印功能中选择转换成 PDF 文件.2. 利用在线 Word to PDF 网站,在百度搜索“Word to PDF”,打开网站后上传 Word 文件,转换后下载 PDF 文件.

莎车县13739461646: c#如何利用pdf虚拟打印机实现word转pdf -
脂南枸橼: word有函数 ExportAsFixedFormat MSDN上说明 The ExportAsFixedFormat method is used to publish a workbook to either the PDF or XPS format.选一个word文件转为PDF public static string WordConvertTOPDF(string path, string saveFielPath) ...

莎车县13739461646: C#中打印内容的问题 -
脂南枸橼: 1、答:打开文本框 =》全选文字 =》右键复制 =》 打开word =》粘贴 =》 打印 =》OK!2、打开图像全屏=》点击键盘的一个名字叫做“Print Screen SysRq”键(在数字键左侧的一组键盘 home的左上方) =》开始 =》程序 =》 附件 =》画图 =》 粘贴 =》打印 祝你开心!(原创)给分吧 我想了好久 而且试过 可达到效果

莎车县13739461646: c# 自动选择打印机 打印Word文档 -
脂南枸橼: 你是指用c#编程?还是问的普通的办公问题?普通办公问题:麻烦你打开“苏州001.doc”,文件->打印->选择好相应城市的打印机->打印 编程问题:获取文件名中的城市名你会吧,然后根据城市名得到打印机名(这一步只能你手动设定,比如...

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 星空见康网