我正在将 doc 文件转换为 pdf 文件,但使用我当前的代码,即使在转换后文件似乎也是打开的。我在输出文件夹中看到 pdf 文件,但如果我尝试重新上传,它会告诉我它已在另一个程序中打开(我在任何地方都看不到)。
代码部分错误:
if (getExt == ".doc")
{
Microsoft.Office.Interop.Word.Application appWord = new Microsoft.Office.Interop.Word.Application();
wordDocument = appWord.Documents.Open(DocumentUNCPath.Text);
wordDocument.ExportAsFixedFormat(@"c:\temp\DocTo.pdf", WdExportFormat.wdExportFormatPDF);
}
完整的方法:
private void btnSubmitStep2_Click(object sender, EventArgs e)
{
string getExt = Path.GetExtension(DocumentUNCPath.Text);
if (getExt == ".doc")
{
Microsoft.Office.Interop.Word.Application appWord = new Microsoft.Office.Interop.Word.Application();
wordDocument = appWord.Documents.Open(DocumentUNCPath.Text);
wordDocument.ExportAsFixedFormat(@"c:\temp\DocTo.pdf", WdExportFormat.wdExportFormatPDF);
}
// Frame up the record of submission
using (var dc = new DocMgmtDataContext())
{
DocumentLibrary.Document doc = new DocumentLibrary.Document()
{
LibraryID = (AssignmentListStep2.SelectedItem as Library).ID,
OwnedByUserID = (StudentListStep2.SelectedItem as User).ID,
UploadedByUserID = (StudentListStep2.SelectedItem as User).ID,
UploadDT = DateTime.UtcNow,
ID = Guid.NewGuid()
};
dc.Documents.InsertOnSubmit(doc);
dc.SubmitChanges();
// Copy file into managed storage
doc.StoragePath = FILESTORELOCATION + doc.ID + ".pdf";
File.Copy(DocumentUNCPath.Text, doc.StoragePath);
doc.Pages = CompatiblePdfReader.VerifyAndFixPdfDocument(doc.StoragePath);
dc.SubmitChanges();
}
// Refresh the list of student submissions
UpdateStudentSubmissionGrid();
}