转:http://www.cnblogs.com/DotNet1010/archive/2007/08/29/873826.html
项目中用到word转pdf 的功能 ,刚开始的要求是做一个应用程序来转 主要代码如下:
项目中用到word转pdf 的功能 ,刚开始的要求是做一个应用程序来转 主要代码如下:
using PDF = PDFMAKERAPILib;
string wordPath = string.Empty;
string pdfPath = string.Empty;
PDF.PDFMakerApp app = new PDFMAKERAPILib.PDFMakerApp();
int iReslut= app.CreatePDF(wordPath, pdfPath, PDF.PDFMakerSettings.kConvertAllPages, true, false, true, System.Type.Missing);
if (iReslut == 0)
{
this.lblInfo.Text = "转换成功!";
}
else
{
//转换失败!
this.lblInfo.Text = Enum.GetName(typeof(PDF.PDFMakerRetVals), iReslut);
}
string pdfPath = string.Empty;
PDF.PDFMakerApp app = new PDFMAKERAPILib.PDFMakerApp();
int iReslut= app.CreatePDF(wordPath, pdfPath, PDF.PDFMakerSettings.kConvertAllPages, true, false, true, System.Type.Missing);
if (iReslut == 0)
{
this.lblInfo.Text = "转换成功!";
}
else
{
//转换失败!
this.lblInfo.Text = Enum.GetName(typeof(PDF.PDFMakerRetVals), iReslut);
}
后来要求改变 必须用ASP.NET 来调用 心想,代码复制到Web窗体里面不就行了吗?
在用WebDev.WebServer.exe 时候 OK,没问题,当用IIS时,就是转换不成功,花了点时间,发现原因是两个的用户不同,一个是管理员,一个是Asp.net 帐户或者是network Service 看IIS是5.0 还是6.0。想通过更改设置权限来解决,改了不少,效果是从一个错误,变成了另一个错误!
后来从网上查资料,受了点启发:(我做COM测试的时候喜欢用VB.NET 代码简练。)
Imports Word = Microsoft.Office.Interop.Word
Imports PDF = ACRODISTXLib
Imports PDF = ACRODISTXLib
Dim Range As New Object()
Range = Word.WdPrintOutRange.wdPrintAllDocument
Dim Item As New Object()
Item = Word.WdPrintOutItem.wdPrintDocumentContent
Dim PageType As New Object()
PageType = Word.WdPrintOutPages.wdPrintAllPages
Dim ManualDuplexPrint As New Object()
ManualDuplexPrint = False
Dim OutPutFileName As String = "C:\Topdf\123456.ps"
Dim wordApp As New Word.Application()
wordApp.Documents.Open("C:\Topdf\123456.doc", False, False, False, "", "", False, "", "", Word.WdOpenFormat.wdOpenFormatAuto, , , , , , "")
wordApp.Documents.Save()
wordApp.ActivePrinter = "Adobe PDF"
wordApp.PrintOut(False, , Range, OutPutFileName, , , Item, 1, "", PageType, False, True, "", , False, 0, 0, 0, 0)
wordApp.Quit()
Dim pdftest = New PDF.PdfDistiller()
pdftest.bShowWindow = 0
pdftest.FileToPDF(OutPutFileName, "C:\Topdf\123456.pdf", "")
pdftest = Nothing
''连续调用会出错,可以先杀掉进程
Range = Word.WdPrintOutRange.wdPrintAllDocument
Dim Item As New Object()
Item = Word.WdPrintOutItem.wdPrintDocumentContent
Dim PageType As New Object()
PageType = Word.WdPrintOutPages.wdPrintAllPages
Dim ManualDuplexPrint As New Object()
ManualDuplexPrint = False
Dim OutPutFileName As String = "C:\Topdf\123456.ps"
Dim wordApp As New Word.Application()
wordApp.Documents.Open("C:\Topdf\123456.doc", False, False, False, "", "", False, "", "", Word.WdOpenFormat.wdOpenFormatAuto, , , , , , "")
wordApp.Documents.Save()
wordApp.ActivePrinter = "Adobe PDF"
wordApp.PrintOut(False, , Range, OutPutFileName, , , Item, 1, "", PageType, False, True, "", , False, 0, 0, 0, 0)
wordApp.Quit()
Dim pdftest = New PDF.PdfDistiller()
pdftest.bShowWindow = 0
pdftest.FileToPDF(OutPutFileName, "C:\Topdf\123456.pdf", "")
pdftest = Nothing
''连续调用会出错,可以先杀掉进程
思路是先用Word 将doc 转换为ps,然后用pdfDistiller 将ps转换为pdf,经过测试,可以在IIS下成功执行。
在此将代码写出来,希望碰到此类问题的,能够少走些弯路!
当然,最好是不调COM,方法是找到了,就是要花钱,先这样用着吧!