2011-03-17 69 views

回答

4

我能夠保存的FlowDocument內容.DOCX和使用的Microsoft.Office.Interop.Word

using moiw = Microsoft.Office.Interop.Word; 


public static void WordToPDF(string docFileName) 
    { 
     // Create a new Microsoft Word application object 
     moiw.Application word = new moiw.Application(); 

     // C# doesn't have optional arguments so we'll need a dummy value 
     object oMissing = Missing.Value; 

     // Get a Word file 
     FileInfo wordFile = new FileInfo(docFileName); 

     word.Visible = false; 
     word.ScreenUpdating = false; 

     // Cast as Object for word Open method 
     Object filename = (Object)wordFile.FullName; 

     // Use the dummy value as a placeholder for optional arguments 
     moiw.Document doc = word.Documents.Open(ref filename, ref oMissing, 
      ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, 
      ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, 
      ref oMissing, ref oMissing, ref oMissing, ref oMissing); 
     doc.Activate(); 

     object outputFileName = wordFile.FullName.Replace(".docx", ".pdf"); 
     object fileFormat = moiw.WdSaveFormat.wdFormatPDF; 

     // Save document into PDF Format 
     doc.SaveAs(ref outputFileName, 
      ref fileFormat, ref oMissing, ref oMissing, 
      ref oMissing, ref oMissing, ref oMissing, ref oMissing, 
      ref oMissing, ref oMissing, ref oMissing, ref oMissing, 
      ref oMissing, ref oMissing, ref oMissing, ref oMissing); 

     // Close the Word document, but leave the Word application open. 
     // doc has to be cast to type _Document so that it will find the 
     // correct Close method.     
     object saveChanges = moiw.WdSaveOptions.wdDoNotSaveChanges; 
     ((moiw._Document)doc).Close(ref saveChanges, ref oMissing, ref oMissing); 
     doc = null; 

     // word has to be cast to type _Application so that it will find 
     // the correct Quit method. 
     ((moiw._Application)word).Quit(ref oMissing, ref oMissing, ref oMissing); 
     word = null; 
    } 
+8

你是如何將FlowDocument內容轉換爲docx的? – SemMike 2012-05-07 05:06:02

0

我們已經爲這個優雅的解決方案稱爲NiPDF。這是一個純粹的.NET組件。

下面是一個轉換WPF視覺到PDF的例子:http://nixps.com/nipdf/example0001.html 同樣可以將的FlowDocument轉換爲XPS,並使用我們的NiPDF控制去PDF。

+0

將其轉換爲PDF解決我的問題,它很酷,但它是一個第三方控制 – xscape 2011-03-17 09:33:22