РЕДАКТИРОВАТЬ: В свете этой статьи, предоставленной Джоном Сондерсом, Соображения относительно автоматизации Office на стороне сервера , приведенный ниже код не следует использовать для целей разработки на стороне сервера.
Вот код для преобразования Docx в PDF с использованием Interop.Надеюсь, вы сможете выяснить, как сделать другие документы, используя это в качестве отправной точки.
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();
}
}