前提条件:安装 Acrobat Distillr,可以通过安装 Acrobat Professional 版本实现。
1.在Winform下实现转换
很简单,需要引用COM组件 PDFMake


public static string ConvertPdf( string p_sFileSrc, string sTgtFile)
{
object missing = System.Type.Missing;
try
{
PDFMAKERAPILib.PDFMakerApp app = new PDFMAKERAPILib.PDFMakerApp();
if (File.Exists(sSrcFile))
{
// 获取PDF生成路径
int bSuccess = app.CreatePDF(sSrcFile, sTgtFile, PDFMAKERAPILib.PDFMakerSettings.kConvertAllPages, false , true , true , missing);
return Enum.GetName( typeof (PDFMAKERAPILib.PDFMakerRetVals), bSuccess);
}
else
{
return " 源文件不存在! " ;
}
}
catch (Exception er)
{
return " 文件转换失败! " ;
}
}
}
2.在WebForm下转换
比上面复杂点,因为上面的代码 在IIS时,就是转换不成功,花了点时间,发现原因是两个的用户不同,一个是管理员,一个是Asp.net 帐户或者是network Service 看IIS是5.0 还是6.0。想通过更改设置权限来解决,改了不少,效果是从一个错误,变成了另一个错误。
从网上搜索到下面这个办法,也是比较简单的。
前提条件:
(1)安装虚拟打印机: 进入WINDOWS的控制面板,进入打印机,点击"添加打印机"图标,在安装对话框上"按一步",出现选择打印机时,在制造商一栏中选择"Generic",在打印机一栏中,选择"MS Publisher Color Printer",然后一路按下一步,知道安装结束。 以下代码并没有提供当前的打印机,而是使用系统默认的,即设置的"MS Publisher Color Printer"。
(2)必须添加引用Acrobat Distiller与WORD,可以从COM组件中添加。


private void WordConvert()
{
oWord.ApplicationClass word = new Microsoft.Office.Interop.Word.ApplicationClass();
Type wordType = word.GetType();
oWord.Documents docs = word.Documents;
Type docsType = docs.GetType();
object objDocName = @" c:\tmp\test.doc " ;
oWord.Document doc = (oWord.Document)docsType.InvokeMember( " Open " , System.Reflection.BindingFlags.InvokeMethod, null , docs, new Object[] { objDocName, true , true });
// 打印输出到指定文件
Type docType = doc.GetType();
object printFileName = @" c:\tmp\test.ps " ;
docType.InvokeMember( " PrintOut " , System.Reflection.BindingFlags.InvokeMethod, null , doc, new object [] { false , false , oWord.WdPrintOutRange.wdPrintAllDocument, printFileName });
wordType.InvokeMember( " Quit " , System.Reflection.BindingFlags.InvokeMethod, null , word, null );
string o1 = " c:\\tmp\\test.ps " ;
string o2 = " c:\\tmp\\test.pdf " ;
string o3 = "" ;
// 引用将PS转换成PDF的对象
try
{
ACRODISTXLib.PdfDistillerClass pdf = new ACRODISTXLib.PdfDistillerClass();
pdf.FileToPDF(o1, o2, o3);
}
catch { }
// 为防止本方法调用多次时发生错误,必须停止acrodist.exe进程
foreach (Process proc in System.Diagnostics.Process.GetProcesses())
{
int begpos;
int endpos;
string sProcName = proc.ToString();
begpos = sProcName.IndexOf( " ( " ) + 1 ;
endpos = sProcName.IndexOf( " ) " );
sProcName = sProcName.Substring(begpos, endpos - begpos);
if (sProcName.ToLower().CompareTo( " acrodist " ) == 0 )
{
try
{
proc.Kill();
}
catch { }
break ;
}
}
}
更多文章、技术交流、商务合作、联系博主
微信扫码或搜索:z360901061

微信扫一扫加我为好友
QQ号联系: 360901061
您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描下面二维码支持博主2元、5元、10元、20元等您想捐的金额吧,狠狠点击下面给点支持吧,站长非常感激您!手机微信长按不能支付解决办法:请将微信支付二维码保存到相册,切换到微信,然后点击微信右上角扫一扫功能,选择支付二维码完成支付。
【本文对您有帮助就好】元
