Bloom.CollectionTab.LibraryModel.ExportDocFormat C# (CSharp) Method

ExportDocFormat() public method

All we do at this point is make a file with a ".doc" extension and open it.
The .doc extension allows the operating system to recognize which program should open the file, and the program (whether Microsoft Word or LibreOffice or OpenOffice) seems to handle HTML content just fine.
public ExportDocFormat ( string path ) : void
path string
return void
        public void ExportDocFormat(string path)
        {
            string sourcePath = _bookSelection.CurrentSelection.GetPathHtmlFile();
            if (RobustFile.Exists(path))
            {
                RobustFile.Delete(path);
            }
            // Linux (Trusty) LibreOffice requires slightly different metadata at the beginning
            // of the file in order to recognize it as HTML.  Otherwise it opens the file as raw
            // HTML (See https://silbloom.myjetbrains.com/youtrack/issue/BL-2276 if you don't
            // believe me.)  I don't know any perfect way to add this information to the file,
            // but a simple string replace should be safe.  This change works okay for both
            // Windows and Linux and for all three programs (Word, OpenOffice and Libre Office).
            string content = RobustFile.ReadAllText(sourcePath);
            string fixedContent = content.Replace("<meta charset=\"UTF-8\">", "<meta http-equiv=\"content-type\" content=\"text/html; charset=utf-8\">");
            RobustFile.WriteAllText(path, fixedContent);
        }

Usage Example

 private void exportToWordOrLibreOfficeToolStripMenuItem_Click(object sender, EventArgs e)
 {
     try
     {
         MessageBox.Show(LocalizationManager.GetString("CollectionTab.BookMenu.ExportDocMessage",
                                                       "Bloom will now open this HTML document in your word processing program (normally Word or LibreOffice). You will be able to work with the text and images of this book, but these programs normally don't too well with preserving the layout, so don't expect much."));
         var destPath = _bookSelection.CurrentSelection.GetPathHtmlFile().Replace(".htm", ".doc");
         _model.ExportDocFormat(destPath);
         PathUtilities.OpenFileInApplication(destPath);
         Analytics.Track("Exported To Doc format");
     }
     catch (IOException error)
     {
         Palaso.Reporting.ErrorReport.NotifyUserOfProblem(error.Message, "Could not export the book");
         Analytics.ReportException(error);
     }
     catch (Exception error)
     {
         Palaso.Reporting.ErrorReport.NotifyUserOfProblem(error, "Could not export the book");
         Analytics.ReportException(error);
     }
 }