把文件(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); }
目前采取朋友的建议,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); }
解决方案 »
- webbrowser控件载入问题
- c# 通过webbrowser控件绑定httpwatch(一个BHO),运行出现错误,如何解决?
- 怎么得到数组中不为空的项的个数(要求不用循环)
- winform关于datagridview显示问题请教
- 如何根据一个字符串判断另一字符串的一部分是否相等
- 新手请教一个很简单的问题,请大家帮忙,我没有分了,555555555555
- 请问:我怎么从excel指定的区域中select某一列?
- 急,web服务。尽量详细点,我是初学者兼菜鸟。
- DataSet与DataGrid的选择???
- 各位大虾,有一个将字符串变为byte类型的问题,请帮帮我,多谢
- C# 多线程 winform 弹出连个窗口,而且会卡住 咋办?
- VS2010的项目如何在VS2005中打开
http://www.hbjjrb.com/Jishu/ASPNET/200704/5929.html
你看一下这里面有把word转为pdf的方法主要用到PDF Distiller看看是否满足你的需求
http://topic.csdn.net/u/20100415/17/687b3da9-436a-4560-97fa-72e53bbcf6e0.html
中的Bullzip.PDFPrinterSettings是什么意思啊??
我通过楼主的方法,执行到
System.Type oType = System.Type.GetTypeFromProgID(progid);
这句时,oType等于null
我该怎么解决啊??Bullzip.PDFPrinterSettings是虚拟打印机的标识符id吗?
如果是,我该怎么找?
{
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);
帮楼主回答一下:
因为这种方法是通过虚拟打印机来制作PDF文件的,所以你需要安装一个虚拟打印机。随便给出个下载地址:http://zjwt.9xz.net/soft/201009/BullzipPDFPrinter_7_1_0_1212.rar