-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))"}

Deduplicator
  • 44,692
  • 7
  • 66
  • 118
ABCD
  • 249
  • 1
  • 4
  • 17
  • @MitchWheat: Can you please share some code on how should i use iText for converting memory stream(of a word doc/docx) into a pdf. It will be very helpful. – ABCD Dec 04 '13 at 10:43
  • Also i have an existing document and i do not think iText will helphttp://stackoverflow.com/questions/1537063/itextsharp-convert-word-doc-docx-to-pdf – ABCD Dec 04 '13 at 10:49
  • possible duplicate of [convert doc to pdf in c#](http://stackoverflow.com/questions/4908819/convert-doc-to-pdf-in-c-sharp) – Werner Henze Dec 04 '13 at 10:57

2 Answers2

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);
Robin Joseph
  • 1,210
  • 1
  • 11
  • 12
  • :On 2nd line i get this error: [System.Runtime.InteropServices.COMException] = {"Type mismatch. (Exception from HRESULT: 0x80020005 (DISP_E_TYPEMISMATCH))"} – ABCD Dec 04 '13 at 19:06
  • First of all add COM reference named Microsoft Word Objects LIbrary. Then Add Microsoft.Office.Interop.Word; namespace. Then just add the type 'Document' before wordDocument. Like Microsoft.Office.Interop.Word.Application appWord = new Microsoft.Office.Interop.Word.Application(); Document wordDocument = appWord.Documents.Open(@"D:\desktop\filename.docx"); wordDocument.ExportAsFixedFormat(@"D:\desktop\DocTo.pdf", WdExportFormat.wdExportFormatPDF); – Robin Joseph Dec 05 '13 at 10:13
-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.

Syeda
  • 1,215
  • 11
  • 23
  • I downvoted because this wouldn't work at all. Changing the content-type header doesn't convert from the doc or docx format to the PDF format. – user247702 Dec 04 '13 at 10:35
  • this is not about to convert doc or docx to PDF. I told here that, how to open memory stream data in web browser in PDF format. As he has said that he has saved his doc in memory stream. – Syeda Dec 05 '13 at 05:01
  • 1
    Quoting the original question: *"I googled but could not get a resource which will help me convert a doc or docx (saved as memory stream) into a PDF."* Your answer does not answer that question at all. – user247702 Dec 05 '13 at 08:41