65.9K
CodeProject 正在变化。 阅读更多。
Home

在 ASP.NET 服务器上将 Word 文档转换为 PDF

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.93/5 (19投票s)

2009 年 7 月 30 日

CPOL

3分钟阅读

viewsIcon

251408

downloadIcon

9431

使用 Word 2007 在服务器上将 Word 文档转换为 PDF 文件

引言

这个 PDFConverter 将 Microsoft Word 文档 (*.doc) 转换为 Web 服务器上的 PDF 文件。 有一个简单的 Web 表单,您可以在其中上传您的 Word 文档,它将被转换并作为 PDF 文件发送回。

必备组件

背景

有很多人尝试直接从他们的 ASP.NET 代码中使用 COM Interop 来进行 Word => PDF 转换。 正如 Microsoft 报告 的那样,不推荐在服务器上自动化 Word 或任何其他 Office 产品。 Office 产品针对使用桌面的客户端应用程序进行交互进行了优化。 这就是为什么如果您想在服务器上以任何方式使用 Office 产品,必须有用户登录。 但是,接下来是另一个问题:直接从 ASP.NET 使用 COM Interop 意味着调用由 ASP.NET 用户进行,该用户无权与 Word 交互。 即使在 DCOM 配置中更改此设置,此解决方案仍然存在许多与访问权限相关的问题。 这就是为什么我考虑了以下方法来做这件事

PDFConverter

解释

PDFConverter.exe 是一个可执行文件,其中包含一直在服务器上运行的 RemotableObjects。 要做到这一点,必须登录一个用户。 启动 PDFConverter.exe 时,将检查 Word 2007 是否可用。 我配置了 Word 对此检查可见,因此您可以看到 Microsoft Word 是否快速打开和关闭,一切正常。

然后是带有文件上传的普通网站。 将上传的文件存储在某个地方(不要忘记在此文件夹上为 ASP.NET 和 IIS 用户提供适当的权限,以便能够在其中保存上传的文件)。

保存文件后,您可以使用 Remoting 获取的 PDFConverter.RemoteConverter 实例调用 convert() 方法(参见代码)。 然后从 PDFConverter.exe 调用整个转换过程,该程序在具有与 Microsoft Word 交互的适当权限的“桌面”上运行。

转换完成后,您可以对 pdf 文件进行任何操作。 在本例中,它将作为文件下载流式传输回客户端。

Using the Code

Remoting

服务器端 PDFConverter.exe (app.config)

<configuration>
  <system.runtime.remoting>
    <application>
      <service>
        <wellknown objecturi="RemoteConverter" 
	type="PDFConverter.RemoteConverter, RemoteConverter" mode="Singleton">
      </wellknown>
      <channels>
        <channel port="8989" ref="http">
      </channel>
    </channels>
  </service>
</application>
</system.runtime.remoting></configuration>

服务器端 PDFConverter.exe (Form1.cs)

...
//initialize remoting
RemotingConfiguration.Configure("PDFConverter.exe.config",false);
RemotingConfiguration.RegisterWellKnownServiceType(new RemoteConverter().GetType(), 
	"RemoteConverter",WellKnownObjectMode.Singleton);
...

服务器端 ASP.NET

添加对 PDFConverter 的引用,然后使用此代码创建实例:Default.aspx.cs

...
converter=(PDFConverter.RemoteConverter)Activator.GetObject
	(typeof(PDFConverter.RemoteConverter),"https://:8989/RemoteConverter");
...

不要忘记在 Default.aspx.cs 中设置您的正确存储文件夹。

转换中

服务器端 PDFConverter.exe

我从 MSDN 文章中复制了使用 Word 2007 进行 PDF 转换的代码的重要部分。 参见这里:将 Word 2007 文档保存为 PDF 和 XPS 格式

服务器端 ASP.NET

...
//Save original file
FileUpload1.SaveAs(sourcefile);                    
                  
//call the converter method
converter.convert(sourcefile, outputfile);

//delete the original file
if (System.IO.File.Exists(sourcefile))
   System.IO.File.Delete(sourcefile);

//Send back a downloadable File
System.IO.FileInfo downloadFile = new System.IO.FileInfo(outputfile);
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.ContentType = "application/pdf";
HttpContext.Current.Response.AddHeader("Content-Disposition",
                                       string.Format("attachment; filename={0}",
                                       downloadFile.Name));
HttpContext.Current.Response.AddHeader("Content-Length", downloadFile.Length.ToString());
HttpContext.Current.Response.WriteFile(downloadFile.FullName);
HttpContext.Current.Response.End();
HttpContext.Current.Response.Close();
...

结论

如您所见,代码不多。 整个转换代码是从 MSDN 复制的,所以这更像是一种使用 Remoting 的想法。 我不是 Remoting 专家,所以可能有更容易/更好/更好的方法来做到这一点,所以如果您有任何想法,请告诉我。 无论如何,我认为这个解决方案比尝试直接从 ASP.NET 代码中使用 Word 好得多。

历史

  • 2009 年 7 月 30 日:初始版本
© . All rights reserved.