调用虚拟打印机转PDF

liuyun1987 2010-08-23 07:27:31
把文件(office文件、图片等文件)转换为pdf文件,各位有啥好的建议?
目前采取朋友的建议,C#调用虚拟打印机(项目中用BullZip Printer)来把文件转换为pdf文件,word转换pdf后,打开pdf文件报错:
Adobe Reader无法打开"XX.pdf",因为不支持此文件类型或者文件已损坏。
以下是源码,请教高人指点:

public void PrintWordToPDF(string fileName)
{
string progid = "Bullzip.PDFPrinterSettings";

System.Type oType = System.Type.GetTypeFromProgID(progid);

object obj_printer_settings = System.Activator.CreateInstance(oType);

//找出虚拟打印机
string PrinterName = (string)oType.InvokeMember("GetPrinterName", System.Reflection.BindingFlags.InvokeMethod, null, obj_printer_settings, null);

Microsoft.Office.Interop.Word.ApplicationClass word = new Microsoft.Office.Interop.Word.ApplicationClass();

Type wordType = word.GetType();

//打开WORD文档
Microsoft.Office.Interop.Word.Documents docs = word.Documents;

Type docsType = docs.GetType();

object objDocName = fileSavePath+"\\"+fileName;

Microsoft.Office.Interop.Word.Document doc = (Microsoft.Office.Interop.Word.Document)docsType.InvokeMember("Open", System.Reflection.BindingFlags.InvokeMethod, null, docs, new Object[] { objDocName, true, true });

doc.Application.ActivePrinter = PrinterName;

Type docType = doc.GetType();

object printFileName = fileSavePath+"\\"+GetFileName(fileName)+ ".pdf";

//打印输出
docType.InvokeMember("PrintOut", System.Reflection.BindingFlags.InvokeMethod, null, doc, new object[] { false, false, Microsoft.Office.Interop.Word.WdPrintOutRange.wdPrintAllDocument, printFileName });

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

}

...全文
2010 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
ahumrli 2012-07-25
  • 打赏
  • 举报
回复
有没有用VC实现的代码啊?
game9701 2012-01-11
  • 打赏
  • 举报
回复
郁闷的,不要钱就好了
googlie 2011-02-15
  • 打赏
  • 举报
回复
感觉不错。
diaopan1985 2010-10-17
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 panhaiyanphy 的回复:]
不解,迷惑中:...等待楼主解决!!..string progid = "Bullzip.PDFPrinterSettings";
中的Bullzip.PDFPrinterSettings是什么意思啊??
我通过楼主的方法,执行到
System.Type oType = System.Type.GetTypeFromProgID(progid);
这句时,oType等于null
我该怎么……
[/Quote]

帮楼主回答一下:
因为这种方法是通过虚拟打印机来制作PDF文件的,所以你需要安装一个虚拟打印机。随便给出个下载地址:http://zjwt.9xz.net/soft/201009/BullzipPDFPrinter_7_1_0_1212.rar
rain_day 2010-10-05
  • 打赏
  • 举报
回复
public class WordCommonConsts
{
public static object docUnknowOption = System.Reflection.Missing.Value;

public static object docTrue = true;

public static object docFalse = false;

}

...

wdoc对象是office com对象我用的Microsft Word 11.0.ObjectLibrary,是office2003的
又引用了com组件的Bullzip,可以直接new,不用取type再生成对象

Bullzip.PDFPrinterSettingsClass pdfpset = new Bullzip.PDFPrinterSettingsClass();

string PrinterName = pdfpset.GetPrinterName();
wdoc.WordDocObj.Application.ActivePrinter = PrinterName;

object copies = "1";
//object pages = "";
object range = Word.WdPrintOutRange.wdPrintAllDocument;
object items = Word.WdPrintOutItem.wdPrintDocumentContent;
object pageType = Word.WdPrintOutPages.wdPrintAllPages;

wdoc.PrintOut(ref WordCommonConsts.docFalse, ref WordCommonConsts.docFalse,ref range,ref WordCommonConsts.docUnknowOption,
ref WordCommonConsts.docUnknowOption, ref WordCommonConsts.docUnknowOption, ref items, ref copies,ref WordCommonConsts.docUnknowOption,
ref pageType,ref WordCommonConsts.docUnknowOption,ref WordCommonConsts.docUnknowOption, ref WordCommonConsts.docUnknowOption,
ref WordCommonConsts.docUnknowOption,ref WordCommonConsts.docUnknowOption,ref WordCommonConsts.docUnknowOption,
ref WordCommonConsts.docUnknowOption, ref WordCommonConsts.docUnknowOption);
rain_day 2010-10-05
  • 打赏
  • 举报
回复
你没装Bullzip的虚拟打印机吧
panhaiyanPHY 2010-09-26
  • 打赏
  • 举报
回复
不解,迷惑中:...等待楼主解决!!..string progid = "Bullzip.PDFPrinterSettings";
中的Bullzip.PDFPrinterSettings是什么意思啊??
我通过楼主的方法,执行到
System.Type oType = System.Type.GetTypeFromProgID(progid);
这句时,oType等于null
我该怎么解决啊??

Bullzip.PDFPrinterSettings是虚拟打印机的标识符id吗?
如果是,我该怎么找?
liuyun1987 2010-08-25
  • 打赏
  • 举报
回复
已通过调用Bullzip printer实现word excel txt转换为pdf,还差image ppt等转换为pdf,到时候完成了拿出来同大家共享
wjq 2010-08-23
  • 打赏
  • 举报
回复
换别的虚拟打印机,PDF Creator或者官方的虚拟打印机
poloyzhang 2010-08-23
  • 打赏
  • 举报
回复
ding...............
HolyPlace 2010-08-23
  • 打赏
  • 举报
回复
你这种貌似就把后缀输出成了pdf
http://www.hbjjrb.com/Jishu/ASPNET/200704/5929.html
你看一下这里面有把word转为pdf的方法主要用到PDF Distiller看看是否满足你的需求
justiceyves 2010-08-23
  • 打赏
  • 举报
回复
帮你顶一下 别忘了给点分 谢谢!

110,533

社区成员

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

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

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