调用虚拟打印机转PDF
把文件(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); }