利用虚拟打印机将Word、Execl文档转换成PDF,XPS文档出现的问题~解决问题了再加100分

mykelly6 2009-03-12 03:47:59

添加引用:COM,MS office11.0Object Library及Interop.Excel
设置默打印机:Microsoft XPS Document Writer或PDF Writer
源程序如下:
1.word ->pdf,xps
using oWord = Microsoft.Office.Interop.Word;
/// <summary>
/// 打印Word文档
/// </summary>
/// <param name="wordfile">完整本地路径,包括文件名</param>
public void Print(string wordfile)
{
oWord.ApplicationClass word = new oWord.ApplicationClass();
Type wordType = word.GetType();

//打开WORD文档
oWord.Documents docs = word.Documents;
Type docsType = docs.GetType();
object objDocName = wordfile;
oWord.Document doc = (oWord.Document)docsType.InvokeMember("Open", System.Reflection.BindingFlags.InvokeMethod, null, docs, new Object[] { objDocName, true, true });

//打印输出到指定文件
//可以使用 doc.PrintOut();方法,次方法调用中的参数设置较繁琐,建议使用 Type.InvokeMember 来调用时可以不用将PrintOut的参数设置全,只设置4个主要参数
Type docType = doc.GetType();
object printFileName = wordfile + ".xps";
docType.InvokeMember("PrintOut", System.Reflection.BindingFlags.InvokeMethod, null, doc, new object[] { false, false, oWord.WdPrintOutRange.wdPrintAllDocument, printFileName });

//退出WORD
wordType.InvokeMember("Quit", System.Reflection.BindingFlags.InvokeMethod, null, word, null);
}

1.execl->pdf,xps
/// <summary>
/// Execl直接打印
/// </summary>
/// <param name="execlfile">完整本地路径,包括文件名</param>
public void Print(string execlfile)
{
Excel.ApplicationClass eapp = new Excel.ApplicationClass();
Type eType = eapp.GetType();
Excel.Workbooks Ewb = eapp.Workbooks;
Type elType = Ewb.GetType();
object objelName = execlfile;
Excel.Workbook ebook = (Excel.Workbook)elType.InvokeMember("Open", System.Reflection.BindingFlags.InvokeMethod, null, Ewb, new Object[] { objelName, true, true });

object printFileName = execlfile + ".xps";

Object oMissing = System.Reflection.Missing.Value;
ebook.PrintOut(oMissing, oMissing, oMissing, oMissing, oMissing, true, oMissing, printFileName);

eType.InvokeMember("Quit", System.Reflection.BindingFlags.InvokeMethod, null, eapp, null);
}


设置了xps默认打印没问题,然后下了adobe acrobat 9就出现了“adobe PDF”的打印机,把它设成默认打印,然后把上面的程序里的“.xps”换成".pdf",excel打印出来了,但是pdf文件出错打不开,word打印的时候直接报错了。
哪位大哥大姐知道的来指点下。
...全文
4840 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
mykelly6 2009-03-16
  • 打赏
  • 举报
回复
站在墙头等答案~
mykelly6 2009-03-15
  • 打赏
  • 举报
回复
没有人知道吗?
mykelly6 2009-03-13
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 withcsharp 的回复:]
原来是用现成的啊

我倒是 写过虚拟打印机
那个 极其简单 把 DDK的genPrint驱动 改改 就可以


Word的你可以录制宏 看看
我做 Word的程序 都是 先在word里做一边 录制好宏
然后 有 代码了 ,想都不用想
[/Quote]
不是很明白阿,能不能像细说说~
mykelly6 2009-03-12
  • 打赏
  • 举报
回复
没用过水晶报表,去试下。
但是感觉跟水晶报表没什么关系阿。
withcsharp 2009-03-12
  • 打赏
  • 举报
回复
原来是用现成的啊

我倒是 写过虚拟打印机
那个 极其简单 把 DDK的genPrint驱动 改改 就可以


Word的你可以录制宏 看看
我做 Word的程序 都是 先在word里做一边 录制好宏
然后 有 代码了 ,想都不用想
Teng_s2000 2009-03-12
  • 打赏
  • 举报
回复
水晶报表或者Report Service都有这个功能,你创建个项目,一看就知道的
mykelly6 2009-03-12
  • 打赏
  • 举报
回复
没人知道吗?再up下
mykelly6 2009-03-12
  • 打赏
  • 举报
回复
1#的大哥能不能说的明确点,我是要把指定路径的excel,word文档通过这个程序直接生成xps或者pdf文档,现在xps是没有问题了
zzxap 2009-03-12
  • 打赏
  • 举报
回复
用水晶报表

110,568

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧