使用C#将.doc转换为.docx [英] Convert .doc to .docx using C#

查看:205
本文介绍了使用C#将.doc转换为.docx的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用PDFFocus.net dll将PDF文件转换为word文件.但是对于我的系统,我需要.docx文件.我尝试了不同的方法.有一些可用的库.但是那些不是免费的.这是我的pdf到doc转换代码.

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

这项工作成功.然后,我尝试使用下面的代码将.doc转换为.docx.但这给了我错误.

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

有人可以帮我吗?

推荐答案

是的,就像Erop所说的那样.您可以使用 Microsoft Word 14.0对象库.然后,从doc转换为docx真的很容易.例如,具有以下功能:

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

请确保添加 CompatibilityMode:WdCompatibilityMode.wdWord2010 ,否则文件将保持兼容模式.还要确保在要运行该应用程序的计算机上安装了Microsoft Office.

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.

另一件事,我不知道 PDFFocus.net ,但是您是否尝试过直接从pdf转换为docx.像这样:

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.

这篇关于使用C#将.doc转换为.docx的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆