Converting Word to ODT format
16 Dec 20226 minutes to read
The OpenDocument format (ODF) is an open file format for office documents originally developed for Open Office suite by Sun Microsystems. OpenDocument Text (ODT) is the file format for Word processing documents and currently for an OASIS and ISO standard.
The Essential DocIO supports converting the Word document into ODT file. The following code example shows how to convert the Word document into ODT file.
//Loads the existing Word document
WordDocument document = new WordDocument("Template.docx");
//Saves the document as ODT file
document.Save("WordToODT.odt", FormatType.Odt);
//Closes the document
document.Close();
'Loads the existing Word document
Dim document As New WordDocument("Template.docx")
'Saves the document as ODT file
document.Save("WordToODT.odt", FormatType.Odt)
'Closes the document
document.Close()
//"App" is the class of Portable project
Assembly assembly = typeof(App).GetTypeInfo().Assembly;
//Opens an existing document from file system through constructor of WordDocument class
using (WordDocument document = new WordDocument((assembly.GetManifestResourceStream("Sample.Assets.Template.docx")), FormatType.Docx))
{
MemoryStream stream = new MemoryStream();
await document.SaveAsync(stream, FormatType.Odt);
//Saves the stream as Word file in local machine
Save(stream, "WordToODT.odt");
//Closes the Word document
document.Close();
}
//Saves the Word document
async void Save(MemoryStream streams, string filename)
{
streams.Position = 0;
StorageFile stFile;
if (!(Windows.Foundation.Metadata.ApiInformation.IsTypePresent("Windows.Phone.UI.Input.HardwareButtons")))
{
FileSavePicker savePicker = new FileSavePicker();
savePicker.DefaultFileExtension = ".odt";
savePicker.SuggestedFileName = filename;
savePicker.FileTypeChoices.Add("Word Documents", new List<string>() { ".odt" });
stFile = await savePicker.PickSaveFileAsync();
}
else
{
StorageFolder local = Windows.Storage.ApplicationData.Current.LocalFolder;
stFile = await local.CreateFileAsync(filename, CreationCollisionOption.ReplaceExisting);
}
if (stFile != null)
{
using (IRandomAccessStream zipStream = await stFile.OpenAsync(FileAccessMode.ReadWrite))
{
//Write compressed data from memory to file
using (Stream outstream = zipStream.AsStreamForWrite())
{
byte[] buffer = streams.ToArray();
outstream.Write(buffer, 0, buffer.Length);
outstream.Flush();
}
}
}
//Launch the saved Word file
await Windows.System.Launcher.LaunchFileAsync(stFile);
}
FileStream fileStreamPath = new FileStream("Template.docx", FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
//Opens an existing document from file system through constructor of WordDocument class
using (WordDocument document = new WordDocument(fileStreamPath, FormatType.Docx))
{
MemoryStream stream = new MemoryStream();
document.Save(stream, FormatType.Odt);
//Closes the Word document
document.Close();
stream.Position = 0;
//Download Word document in the browser
return File(stream, "application/msword", "WordToODT.odt");
}
Assembly assembly = typeof(App).GetTypeInfo().Assembly;
//Opens an existing document from file system through constructor of WordDocument class
using (WordDocument document = new WordDocument((assembly.GetManifestResourceStream("Sample.Assets.Template.docx")), FormatType.Docx))
{
MemoryStream stream = new MemoryStream();
document.Save(stream, FormatType.Odt);
//Save the stream as a file in the device and invoke it for viewing
Xamarin.Forms.DependencyService.Get<ISave>().SaveAndView("WordToODT.odt", "application/msword", stream);
//Closes the Word document
document.Close();
}
You can download a complete working sample from GitHub.
Supported Document elements
Document Element | Attribute | Support Status | Notes |
---|---|---|---|
Bookmark |
Id |
Yes |
- |
Border |
Color |
Yes |
- |
Distance from text |
No |
- |
|
Line style |
Yes |
- |
|
Line width |
Yes |
- |
|
Document Properties |
|
No |
- |
Field |
|
Partial |
For some of the fields, the field results have been preserved as span text. |
Footnotes and Endnotes |
|
No |
- |
Form Field |
|
No |
- |
Header / Footer |
Different per section |
Yes |
- |
Hyperlink |
External URL |
Yes |
- |
Local |
Yes |
- |
|
Image |
Inline |
Yes |
- |
Scale |
Yes |
- |
|
List |
Custom bullets |
No |
- |
Multi-level |
Yes |
- |
|
Numbered |
Yes |
- |
|
Restart numbering |
No |
- |
|
Standard bullets |
Yes |
- |
|
Comment |
|
No |
|
Symbols |
|
Yes |
|
Paragraph |
Alignment |
Yes |
|
|
Borders |
Yes |
See Borders, for more details. |
|
Keep lines and paragraphs together |
Yes |
- |
|
Paragraph Indents |
Yes |
- |
|
Line spacing |
Yes |
- |
|
Page break before |
No |
- |
|
Shading |
No |
|
|
Spacing before and after |
Yes |
- |
Shading |
Background color |
Yes |
|
|
Foreground color |
No |
|
Styles |
Paragraph styles |
Yes |
- |
|
Character styles |
Yes |
- |
|
List styles |
Yes |
- |
Table |
Alignment |
Yes |
- |
|
Cell margins |
Yes |
- |
|
Column widths |
Yes |
- |
|
Indent from left |
No |
- |
|
Preferred width |
No |
- |
|
Spacing between cells |
Yes |
- |
|
Borders |
Yes |
See Borders, for more details. |
|
Shading |
No |
|
Nested Table |
|
Yes |
|
Table Cell |
Borders |
Yes |
See Borders, for more details. |
|
Cell margins |
Yes |
- |
|
Horizontal merge |
No |
- |
|
Shading |
No |
|
|
Vertical alignment |
Yes |
- |
|
Vertical merge |
No |
- |
Table Row |
Height |
Yes |
- |
|
Padding |
Yes |
- |
Text |
All caps |
Yes |
- |
|
Bold |
Yes |
- |
|
Character spacing |
Yes |
- |
|
Color |
Yes |
- |
|
Emboss |
Yes |
Rendered as bold. |
|
Engrave |
No |
|
|
Font |
Yes |
- |
|
Hidden |
No |
- |
|
Highlighting |
Yes |
- |
|
Italic |
Yes |
- |
|
Line breaks |
Yes |
- |
|
Outline |
Yes |
Rendered as bold. |
|
Page breaks |
Yes |
- |
|
Shading |
No |
|
|
Small caps |
Yes |
- |
|
Special symbols |
Yes |
- |
|
Strike out |
Yes |
- |
|
Subscript / Superscript |
Yes |
- |
|
Underline |
Yes |