使用绝对文件路径来解决相对路径映射失败的问题。例如,使用以下代码来获取文件的绝对路径:
string filePath = HttpContext.Current.Server.MapPath("~/files/document.doc");
这里的“~/”指的是应用程序根目录。然后,将文件转换为PDF格式。
Word.Application word = new Word.Application();
Word.Document document = word.Documents.Open(filePath, ReadOnly: true);
string pdfPath = HttpContext.Current.Server.MapPath("~/files/document.pdf");
document.SaveAs2(pdfPath, Word.WdSaveFormat.wdFormatPDF);
document.Close();
word.Quit();
在这个示例中,“filePath”和“pdfPath”都使用了绝对路径,而不是相对路径。因此,可以避免相对路径映射失败的问题。