我已经看过几个关于这个的帖子,但在我的搜索中没有得到非常直接的答案。我有一个网络应用程序,需要在doc,docx,xls,xlsx文件,并将它们转换为PDF。现在我们有一个进程,它使用Microsoft.Office.Interop.Word库打开文档,将其打印成PS文件,然后GPL GhostScript将PS文件转换成PDF。
这个过程适用于OKish,但总体上有几个步骤,这最初是在几年前开发的,当时更难找到一个PDF打印驱动程序并将其连接起来。本着更新的精神,我正在寻找一种可能的更好的方法来处理这个问题。主要原因是,在我们的应用程序中,我们使用web服务调用来执行转换过程的提升操作,对于较新的windows服务器,尤其是用于开发的Windows7,即使使用模拟打开文件的能力也会导致Interop库出现一些问题。
我相信所有这一切都是可以解决的,但我想知道是否有更新更好的方法来解决这个问题。我研究过PDF995,但没有找到一种很好的编程方法,可以直接将文件打印到PDF。他们提供的代码是用C++编写的,我不知道如何用C#模仿这些调用。
发布于 2011-10-06 01:24:19
如果您正在寻找一个“免费”的解决方案,我认为您可能有唯一可行的选择,但就像John所说的,服务器端互操作通常不是一个好主意。我们已经成功地使用了.NET Aspose组件。这是一个纯托管的解决方案,不需要互操作或办公。
发布于 2011-10-06 01:23:53
编辑:根据 的John Saunders提供的这篇文章,下面的代码不应该用于服务器端开发目的。
下面是使用Interop将Docx转换为PDF的代码。希望您能以此为起点,找出如何处理其他文档。
private void DocxToPdf(String sourcePath, String destPath) {
//Change the path of the .docx file and filename to your file name.
object paramSourceDocPath = sourcePath;
object paramMissing = Type.Missing;
var wordApplication = new Microsoft.Office.Interop.Word.Application();
Document wordDocument = null;
//Change the path of the .pdf file and filename to your file name.
string paramExportFilePath = destPath;
WdExportFormat paramExportFormat = WdExportFormat.wdExportFormatPDF;
bool paramOpenAfterExport = false;
WdExportOptimizeFor paramExportOptimizeFor =
WdExportOptimizeFor.wdExportOptimizeForPrint;
WdExportRange paramExportRange = WdExportRange.wdExportAllDocument;
int paramStartPage = 0;
int paramEndPage = 0;
WdExportItem paramExportItem = WdExportItem.wdExportDocumentContent;
bool paramIncludeDocProps = true;
bool paramKeepIRM = true;
WdExportCreateBookmarks paramCreateBookmarks =
WdExportCreateBookmarks.wdExportCreateWordBookmarks;
bool paramDocStructureTags = true;
bool paramBitmapMissingFonts = true;
bool paramUseISO19005_1 = false;
try {
// Open the source document.
wordDocument = wordApplication.Documents.Open(
ref paramSourceDocPath, ref paramMissing, ref paramMissing,
ref paramMissing, ref paramMissing, ref paramMissing,
ref paramMissing, ref paramMissing, ref paramMissing,
ref paramMissing, ref paramMissing, ref paramMissing,
ref paramMissing, ref paramMissing, ref paramMissing,
ref paramMissing);
// Export it in the specified format.
if (wordDocument != null)
wordDocument.ExportAsFixedFormat(paramExportFilePath,
paramExportFormat, paramOpenAfterExport,
paramExportOptimizeFor, paramExportRange, paramStartPage,
paramEndPage, paramExportItem, paramIncludeDocProps,
paramKeepIRM, paramCreateBookmarks, paramDocStructureTags,
paramBitmapMissingFonts, paramUseISO19005_1,
ref paramMissing);
}
catch (Exception ex) {
// Respond to the error
System.Windows.Forms.MessageBox.Show(ex.Message);
}
finally {
// Close and release the Document object.
if (wordDocument != null) {
wordDocument.Close(ref paramMissing, ref paramMissing,
ref paramMissing);
wordDocument = null;
}
// Quit Word and release the ApplicationClass object.
if (wordApplication != null) {
wordApplication.Quit(ref paramMissing, ref paramMissing,
ref paramMissing);
wordApplication = null;
}
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
GC.WaitForPendingFinalizers();
}
}发布于 2015-11-18 22:54:19
Syncfusion Essential PDF可用于将办公文档转换为PDF。该库可从Windows Forms、WPF、MVC Webforms、ASP.NET ASP.NET应用程序中使用
如果您符合条件,可以通过community license程序免费获得整个控件套件(商业应用程序也可以)。社区许可证是没有限制或水印的完整产品。
注意:我在Syncfusion工作。
https://stackoverflow.com/questions/7665062
复制相似问题