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

匿名 (未验证) 提交于 2019-12-03 09:05:37

问题:

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.

回答1:

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.



回答2:

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; 


易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!