类型不匹配将.doc / docx转换为PDF?

时间:2013-12-04 09:39:18

标签: c# asp.net pdf ms-word

在Web应用程序中,我必须实现一种将doc / docx转换为PDF的方法。这就是我的做法:

FileInfo FILE = new FileInfo(path + filename);

object missing = System.Reflection.Missing.Value;
object readOnly = false;
object objfilename = (object)FILE;
//Microsoft.Office.Interop.Word.Application word = new Microsoft.Office.Interop.Word.Application();

try
{
    Microsoft.Office.Interop.Word.Application appWord = new Microsoft.Office.Interop.Word.Application();
   // var wordDocument = appWord.Documents.Open(FILE);
    var wordDocument = appWord.Documents.Open(ref objfilename,
                                              ref missing, ref readOnly, ref missing, ref missing, ref missing,
                                              ref missing, ref missing, ref missing, ref missing, ref missing,
                                              ref missing, ref missing, ref missing, ref missing, ref missing);
    wordDocument.ExportAsFixedFormat(@"C:\Users\ABCD\Desktop\New folder\DocTo.pdf", WdExportFormat.wdExportFormatPDF);

但是代码在Documents.Open行上中断,它显示的错误是:

  

[System.Runtime.InteropServices.COMException] = {“类型不匹配。(HRESULT异常:0x80020005(DISP_E_TYPEMISMATCH))”}

2 个答案:

答案 0 :(得分:3)

您可以使用Microsoft.Office.Interop.Word.Application将word文件转换为odf。但您必须在您的计算机上安装Microsoft Word。这是我们将doc文件转换为pdf的方法。我不知道你是否可以将流转换为pdf。这将是更好的方式。或者另一种解决方案是,当用户从doc文件请求pdf时,您只需创建一个doc文件表格数据流,并在此处使用该创建的doc文件。

Microsoft.Office.Interop.Word.Application appWord = new Microsoft.Office.Interop.Word.Application();
        wordDocument = appWord.Documents.Open(@"D:\desktop\filename.docx");
        wordDocument.ExportAsFixedFormat(@"D:\desktop\DocTo.pdf", WdExportFormat.wdExportFormatPDF);

答案 1 :(得分:-1)

非常简单。

 Byte[] buffer =  mystream.ToArray();

        if (buffer != null)
        {
            Response.ContentType = "application/pdf"; 
            Response.AddHeader("content-length",buffer.Length.ToString()); 
            Response.BinaryWrite(buffer); 
        }

希望这会对你有所帮助。