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

代码如下:
object oTrue = true;
object oFalse = false;

object oBackground=oMissing;//如果为 true,则可以让自定义项代码在 Microsoft Office Word 打印文档时继续工作。
object oAppend = oFalse;//如果为 true,则会将文档追加到 OutputFileName 参数指定的文件;如果为 false,则会改写 OutputFileName 的内容。
object oRange = WdPrintOutRange.wdPrintRangeOfPages;//页面范围。可以是任何 WdPrintOutRange 值。
object oOutputFileName = @"d:\test.PDF";//如果 PrintToFile 为 true,则此参数指定输出文件的路径和文件名。oMissing;//
object oFrom = oMissing;//当 Range 设置为 wdPrintFromTo 时的起始页码。
object oTo = oMissing;//当 Range 设置为 wdPrintFromTo 时的结束页码。
object oItem = oMissing;//要打印的项。可以是任何 WdPrintOutItem 值。
object oCopies = 1;//要打印的份数。
object oPages = "s2";//要打印的页码和页码范围,由逗号分隔。例如,“2, 6-10”意为打印第 2 页和第 6、7、8、9、10 页。
object oPrinttoFile = oTrue ;
doc.PrintOut(
ref oBackground , //Background 此处为true,表示后台打印
ref oAppend,
ref oRange,//oMissing, //Range 页面范围
ref oOutputFileName,// outputfilename
ref oFrom,//wdPrintFrom, //当 Range 设置为 wdPrintFromTo 时的起始页码
ref oTo,//wdPrintTo,//当 Range 设置为 wdPrintFromTo 时的结束页码
ref oItem,// WdPrintOutItem
ref oCopies, //要打印的份数
ref oPages,
ref oPageType,
ref oPrinttoFile,//oMissing, PrintToFile
ref oCollate, //Collete
ref oActivePrinterMacGX,
ref oManualDuplexPrint,
ref oPrintZoomColumn,
ref oPrintZoomRow,
ref oPrintZoomPaperWidth,
ref oPrintZoompaperHeight);

以上面的代码,如果OutputFileName 参数不指定文件名,打印过程中会弹出对话框,填写文件名称后打印成PDF文件没有问题,
我希望不需要填写文件名,自动将WORD打印保存为d:\test.PDF文件,但打印所生成的PDF文件无法打开,显示如下信息

请教各位高手该如何处理

一、
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;
}追问

doc.PrintOut()中,oOutputFileName为oMissing时,是可以打印的,但在打印过程中,会出现要求填写PDF文件名称的对话框,我想实现的效果是在打印过程中不需要填写该PDF文件名,直接打印成程序中设置为PDF文件。如果将oOutputFileName赋值,如d:\test.PDF,可以生成该test.PDF的文件,但该文件无法使用,打开时会出现上述问题附图中的提示。

温馨提示:答案为网友推荐,仅供参考
第1个回答  2022-02-18
遇到了同样的问题。
在内网尝试了各种方案都不可能。
在外网找到一个可用,但是似乎并不通用的解决方案。
在打印代码之前,写上设置活动打印机的代码
例如,ActivePrinter = "Microsoft Print to PDF"。
通过尝试,使用上述方式可以在指定保存路径和名称的情况下后台打印,并且可以打开pdf文档。
但是,如果使用下述活动打印机,ActivePrinter = "Adobe PDF",仍然会出现上述问题。
第2个回答  2016-06-15
你解决了吗?

相关了解……

你可能感兴趣的内容

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