Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert .doc to .docx using C# [closed]

Tags:

c#

I convert PDF file to the word file using PDFFocus.net dll. But for my system I want .docx file. I tried different ways. There some libraries available. But those are not free. This is my pdf to doc convert code.

    Using System;
    Using System.Collections.Generic;
    Using System.Linq;
    Using System.Text;
    Using System.Threading.Tasks;
    Using iTextSharp.text;
    Using iTextSharp.text.pdf;

    namespace ConsoleApplication
    {
          class Program
          {
               static void main(String[] args)
               {
                    SautinSoft.PdfFocus f=new SautinSoft.PdfFocus();
                    f.OpenPdf(@"E:\input.pdf");

                         t.ToWord(@"E:\input.doc");
                }
          }
    }

This work successfully. Then I tried with below code to convert .doc to .docx. But it gives me error.

//Open a Document.
Document doc=new Document("input.doc");
//Save Document.
doc.save("output.docx");

Can anyone help me please.

like image 286
Lasa Avatar asked Feb 09 '23 09:02

Lasa


2 Answers

Yes like Erop said. You can use the Microsoft Word 14.0 Object Library. Then it's really easy to convert from doc to docx. E.g with a function like this:

    public void ConvertDocToDocx(string path)
    {
        Application word = new Application();

        if (path.ToLower().EndsWith(".doc"))
        {
            var sourceFile = new FileInfo(path);
            var document = word.Documents.Open(sourceFile.FullName);

            string newFileName = sourceFile.FullName.Replace(".doc", ".docx");
            document.SaveAs2(newFileName,WdSaveFormat.wdFormatXMLDocument, 
                             CompatibilityMode: WdCompatibilityMode.wdWord2010);

            word.ActiveDocument.Close();
            word.Quit();

            File.Delete(path);
        }
    }

Make sure to add CompatibilityMode: WdCompatibilityMode.wdWord2010 otherwise the file will stay in compatibility mode. And also make sure that Microsoft Office is installed on the machine where you want to run the application.

Another thing, I don't know PDFFocus.net but have you tried converting directly from pdf to docx. Like this:

     static void main(String[] args)
     {
           SautinSoft.PdfFocus f=new SautinSoft.PdfFocus();
           f.OpenPdf(@"E:\input.pdf");

                t.ToWord(@"E:\input.docx");
     }

I would assume that this is working, but it's only an assumption.

like image 123
Dave Avatar answered Feb 10 '23 23:02

Dave


Try to use Microsoft.Office.Interop.Word assembly.

An MSDN article can be found Here

Include references in your project, and enable their use in a code module via an example from the above link that shows

using System.Collections.Generic;
using Word = Microsoft.Office.Interop.Word;
like image 27
Ghost Avatar answered Feb 10 '23 23:02

Ghost