B2023-093 Convert PROMS Word sections to the DOCX format if needed before Editing or Printing

This commit is contained in:
2023-09-13 14:27:38 -04:00
parent a923a597f3
commit ef3efe2380
7 changed files with 306 additions and 0 deletions

View File

@@ -160,6 +160,78 @@ namespace VEPROMS.CSLA.Library
}
}
}
// B2023-093 This method is called before editing or printing a Word section and will convert it the Word .DOCX format if needed.
// Note that the core logic was taken from frmSectionProperties.cs and modified to convert both .RTF and .DOC files
// The conversion to DOCX is needs to be done only one time per Word section
public static void ConvertWordSectionToDOCX(ItemInfo itmInfo)
{
// check the Word file extension that is saved in the tblDocuments SQL database table
DocumentInfo docInfo = itmInfo.MyContent.MyEntry.MyDocument;
if (docInfo.FileExtension.ToUpper() == ".DOCX") return; // already a DOCX - no need to convert
// show user a status window of the Word section being converted to DOCX
// use the section number (DisplayNumber) unless the length is zero, then use the section title (DisplayText)
string statMsg = itmInfo.DisplayNumber;
if (statMsg.Length == 0)
statMsg = itmInfo.DisplayText;
FrmPopupStatusMessage pmsg = new FrmPopupStatusMessage("Converting This Section to Word DOCX", statMsg);
pmsg.Show();
// Get Document as file - it's placed in the user's temporary folder (like we do when we edit a Word section)
DSOFile myfile = new DSOFile(docInfo);
// Open MSWord App
LBWordLibrary.LBApplicationClass ap = new LBWordLibrary.LBApplicationClass();
LBWordLibrary.LBDocumentClass doc = ap.Documents.Open(myfile.FullName);
// Older versions of PROMS saved the Word section as either a RTF or the old-style Word DOC file
// In either case, we want to convert it to a Word DOCX file
// So now create a file name with the .DOCX extension
string orgFilename = myfile.FullName.ToUpper();
string filename = orgFilename.Replace(".RTF", ".DOCX").Replace(".DOC", ".DOCX"); // we want to convert either .RTF or .DOC Word sections
// This calls Word's convert function to convert the opened .DOC or .RTF to DOCX and save it to our new DOCX file name
doc.SaveAs(filename, LBWordLibrary.LBWdSaveFormat.wdFormatXMLDocument); // Convert to Word DOCX
doc.Close();
ap.Quit(); // close the Word app
// Now read in the new .DOCX file and save the contents to the SQL database
FileInfo fi = new FileInfo(filename);
FileStream fs = File.Open(filename, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);// B2016-053
long len = fs.Length;
byte[] ByteArray = new byte[len];
int nBytesRead = fs.Read(ByteArray, 0, (int)len);
fs.Close();
bool isLibraryDocument = (docInfo.LibTitle != null && docInfo.LibTitle != "");
Document myDoc = null;
if (isLibraryDocument)
myDoc = Document.Get(docInfo.DocID);
else
myDoc = Document.MakeDocument(null, ByteArray, docInfo.DocAscii, docInfo.Config, ".DOCX");
// update the document information in the database
SectionInfo msi = itmInfo as SectionInfo;
Section sec = msi.Get();
SectionConfig cfg = sec.SectionConfig;
if (!isLibraryDocument)
cfg.MySection.MyContent.MyEntry.MyDocument = myDoc; // resetting MyDocument will clear the library doc link - so don't do if a library document
else
cfg.MySection.MyContent.MyEntry.MyDocument.DocContent = ByteArray; // only update .DocContent for library documents
cfg.MySection.MyContent.MyEntry.MyDocument.FileExtension = ".DOCX"; // make sure the Word file extension is .DOCX
cfg.MySection.MyContent.MyEntry.MyDocument.DTS = fi.LastWriteTimeUtc;
cfg.MySection.MyContent.MyEntry.MyDocument.MarkDirty();
cfg.MySection.MyContent.MyEntry.Save();
_MyLog.InfoFormat("Converted Word Section to DOCX - Old ID {0} - New ID {1} - {2}", docInfo.DocID, myDoc.DocID, statMsg); // record in log file (aka error log) that conversion was done
// delete the temporary files
fi.Delete();// delete the temporary .DOCX file
FileInfo orgFile = new FileInfo(orgFilename);
orgFile.Delete();// delete the old temporary Word file (.DOC or .RTF)
pmsg.Close(); // close the statue message
}
/// <summary>
/// FixString processes the string returned and changes any symbols (0xF0??) to normal characters
/// </summary>