Type mismatch convert a .doc/docx into a PDF?

-1

In a web application i have to implement a method to convert a doc/docx to a PDF. This is how i am doing it:

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);

But the code breaks on Documents.Open line and the error that it displays is:

[System.Runtime.InteropServices.COMException] = {"Type mismatch. (Exception from HRESULT: 0x80020005 (DISP_E_TYPEMISMATCH))"}

c#
asp.net
pdf
ms-word
asked on Stack Overflow Dec 4, 2013 by ABCD • edited Mar 3, 2015 by Deduplicator

2 Answers

3

You can use Microsoft.Office.Interop.Word.Application to convert word files into odf. But you must have Microsoft Word installed on you machine. This is how we can convert a doc file to pdf. I don't know whether you can convert stream into pdf. This will be the much better way. Or another solution is, When user requests pdf from doc file, you just create a doc file form DB stream and use that created doc file here.

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);
answered on Stack Overflow Dec 4, 2013 by Robin Joseph
-1

Its quite simple.

 Byte[] buffer =  mystream.ToArray();

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

Hope this will help you.

answered on Stack Overflow Dec 4, 2013 by Syeda • edited Dec 4, 2013 by Syeda

User contributions licensed under CC BY-SA 3.0