日期:2014-05-18  浏览次数:21153 次

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

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);

        }




------解决方案--------------------
帮你顶一下 别忘了给点分 谢谢!
------解决方案--------------------
你这种貌似就把后缀输出成了pdf
http://www.hbjjrb.com/Jishu/ASPNET/200704/5929.html
你看一下这里面有把word转为pdf的方法主要用到PDF Distiller看看是否满足你的需求

------解决方案--------------------
ding...............
------解决方案--------------------
换别的虚拟打印机,PDF Creator或者官方的虚拟打印机
------解决方案--------------------
换个.pdf文件试
http://topic.csdn.net/u/20100415/17/687b3da9-436a-4560-97fa-72e53bbcf6e0.html