可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
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;