|
| 1 | +--- |
| 2 | +title: Convert Word to WordML file and vice versa in C# | Syncfusion |
| 3 | +description: Learn how to convert Word document to WordML file and vice versa using the .NET Word (DocIO) library without Microsoft Word or interop dependencies. |
| 4 | +platform: document-processing |
| 5 | +control: DocIO |
| 6 | +documentation: UG |
| 7 | +--- |
| 8 | + |
| 9 | +# WordML Conversions in Word Library |
| 10 | + |
| 11 | +The Essential<sup>®</sup> DocIO converts the Word document into WordML file and vice versa. |
| 12 | + |
| 13 | +## Convert Word to WordML |
| 14 | + |
| 15 | +The following code example shows how to convert the Word document into WordML file. |
| 16 | + |
| 17 | +N> Refer to the appropriate tabs in the code snippets section: ***C# [Cross-platform]*** for ASP.NET Core, Blazor, Xamarin, UWP, .NET MAUI, and WinUI; ***C# [Windows-specific]*** for WinForms and WPF; ***VB.NET [Windows-specific]*** for VB.NET applications. |
| 18 | + |
| 19 | +{% tabs %} |
| 20 | + |
| 21 | +{% highlight c# tabtitle="C# [Cross-platform]" playgroundButtonLink="https://raw.githubusercontent.com/SyncfusionExamples/DocIO-Examples/refs/heads/main/WordML-conversion/Convert-Word-to-WordML/.NET/Convert-Word-to-WordML/Program.cs" %} |
| 22 | +FileStream fileStreamPath = new FileStream("Template.docx", FileMode.Open, FileAccess.Read, FileShare.ReadWrite); |
| 23 | +//Loads an existing Word document into DocIO instance |
| 24 | +using (FileStream fileStreamPath = new FileStream(Path.GetFullPath(@"Data/Template.docx"), FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) |
| 25 | +{ |
| 26 | + using (WordDocument document = new WordDocument(fileStreamPath, FormatType.Docx)) |
| 27 | + { |
| 28 | + //Creates file stream |
| 29 | + using (FileStream outputFileStream = new FileStream(Path.GetFullPath(@"Output/Result.xml"), FileMode.Create, FileAccess.ReadWrite)) |
| 30 | + { |
| 31 | + //Save the loaded document in WordML format to the output stream |
| 32 | + document.Save(outputFileStream, FormatType.WordML); |
| 33 | + //Closes the Word document |
| 34 | + document.Close(); |
| 35 | + } |
| 36 | + } |
| 37 | +} |
| 38 | +{% endhighlight %} |
| 39 | + |
| 40 | +{% highlight c# tabtitle="C# [Windows-specific]" %} |
| 41 | +//Loads a template document |
| 42 | +WordDocument document = new WordDocument("Template.docx"); |
| 43 | +//Saves the document as WordML file |
| 44 | +document.Save("WordToWordML.xml", FormatType.WordML); |
| 45 | +//Closes the document |
| 46 | +document.Close(); |
| 47 | +{% endhighlight %} |
| 48 | + |
| 49 | +{% highlight vb.net tabtitle="VB.NET [Windows-specific]" %} |
| 50 | +'Loads a template document |
| 51 | +Dim document As New WordDocument("Template.docx") |
| 52 | +'Saves the document as WordML file |
| 53 | +document.Save("WordToWordML.xml", FormatType.WordML) |
| 54 | +'Closes the document |
| 55 | +document.Close() |
| 56 | +{% endhighlight %} |
| 57 | + |
| 58 | +{% endtabs %} |
| 59 | + |
| 60 | +You can download a complete working sample from [GitHub](https://github.com/SyncfusionExamples/DocIO-Examples/tree/main/WordML-conversion/Convert-Word-to-WordML/.NET). |
| 61 | + |
| 62 | +## Convert WordML to Word |
| 63 | + |
| 64 | +The following code example shows how to convert a WordML file into Word document. |
| 65 | + |
| 66 | +{% tabs %} |
| 67 | + |
| 68 | +{% highlight c# tabtitle="C# [Cross-platform]" playgroundButtonLink="https://raw.githubusercontent.com/SyncfusionExamples/DocIO-Examples/refs/heads/main/WordML-conversion/Convert-WordML-to-Word/.NET/Convert-WordML-to-Word/Program.cs" %} |
| 69 | +FileStream fileStreamPath = new FileStream("Template.txt", FileMode.Open, FileAccess.Read, FileShare.ReadWrite); |
| 70 | +//Loads an existing WordML file into DocIO instance |
| 71 | +using (FileStream fileStreamPath = new FileStream(Path.GetFullPath(@"Data/Template.xml"), FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) |
| 72 | +{ |
| 73 | + using (WordDocument document = new WordDocument(fileStreamPath, FormatType.WordML)) |
| 74 | + { |
| 75 | + //Creates file stream |
| 76 | + using (FileStream outputFileStream = new FileStream(Path.GetFullPath(@"Output/Result.docx"), FileMode.Create, FileAccess.ReadWrite)) |
| 77 | + { |
| 78 | + //Saves the Word document to file stream. |
| 79 | + document.Save(outputFileStream, FormatType.Docx); |
| 80 | + //Closes the Word document |
| 81 | + document.Close(); |
| 82 | + } |
| 83 | + } |
| 84 | +} |
| 85 | +{% endhighlight %} |
| 86 | + |
| 87 | +{% highlight c# tabtitle="C# [Windows-specific]" %} |
| 88 | +//Loads a WordML file |
| 89 | +WordDocument document = new WordDocument("Template.xml"); |
| 90 | +//Saves the document as Word document |
| 91 | +document.Save("WordMLToWord.docx", FormatType.Docx); |
| 92 | +//Closes the document |
| 93 | +document.Close(); |
| 94 | +{% endhighlight %} |
| 95 | + |
| 96 | +{% highlight vb.net tabtitle="VB.NET [Windows-specific]" %} |
| 97 | +'Loads a WordML file |
| 98 | +Dim document As New WordDocument("Template.xml") |
| 99 | +'Saves the document as Word document |
| 100 | +document.Save("WordMLToWord.docx", FormatType.Docx) |
| 101 | +'Closes the document |
| 102 | +document.Close() |
| 103 | +{% endhighlight %} |
| 104 | + |
| 105 | +{% endtabs %} |
| 106 | + |
| 107 | +You can download a complete working sample from [GitHub](https://github.com/SyncfusionExamples/DocIO-Examples/tree/main/WordML-conversion/Convert-WordML-to-Word/.NET). |
0 commit comments