diff --git a/PROMS/DataLoader/Documents.cs b/PROMS/DataLoader/Documents.cs index 092ce8aa..43ea2297 100644 --- a/PROMS/DataLoader/Documents.cs +++ b/PROMS/DataLoader/Documents.cs @@ -22,6 +22,7 @@ using System.Text; using System.Text.RegularExpressions; using Volian.MSWord; using VEPROMS.CSLA.Library; +using System.Threading; namespace DataLoader { @@ -57,127 +58,41 @@ namespace DataLoader //string tmpName = @"C:\Temp\DataLoader\" + myFile.Name.Replace(".", "_") + ".RTF"; string tmpName = Path.GetTempFileName(); string temppath = Path.GetTempFileName(); - if (frmMain.MySettings.ExecutionMode == ExecutionMode.Production || + if (frmMain.MySettings.ExecutionMode == ExecutionMode.Production || frmMain.MySettings.ConvertTo == AccPageConversion.MSWord) { FileInfo tmpFile = new FileInfo(tmpName); if (tmpFile.Exists) tmpFile.Delete(); myFile.CopyTo(tmpName); - bool keepTrying = true; - int attempt = 1; - while (keepTrying) + LoadAndSaveRichTextBox41(tmpName); + WordDocument wd = new WordDocument(tmpName, stype, temppath); + Thread myThread = new Thread(wd.GetAsciiFromWord); + myThread.Start(); + myThread.Join(30000); // Wait 30 seconds to time-out + if (myThread.ThreadState == System.Threading.ThreadState.Running) { - WordDoc myWordDoc = null; - try - { - LoadAndSaveRichTextBox(tmpName); - myWordDoc = new WordDoc(tmpName); - /* 16-bit's type[1] used the following codes to represent the respective lpi setting - * - * char far typestr[] = "*pP46f7L"; - * - * char * far printtypes[] = { - * "Compressed, 8 lines per inch", - * "Elite, 6 lines per inch", - * "Pica, 6 lines per inch", - * "Default font, 4 Lines Per Inch", - * "Default font, 6 Lines Per Inch", - * "Compressed 6 LPI", - * "Default font, 7 Lines Per Inch", - * "Special Landscape, Elite, 6 lines per inch" - */ - - if (stype != null) - { - // stype[1] == 'p' or 'P' or '6' 'f' or 'L' get spc = 6 - int lpi = 6; - if (stype[1] == '*') lpi = 8; - else if (stype[1] == '4') lpi = 4; - else if (stype[1] == '7') lpi = 7; - // if need landscape set too: bool landscape = (stype[1] == 'L'); - myWordDoc.SetLineSpacing(lpi); - } - else - { - myWordDoc.SetLineSpacing(6); - } - float docLen = 1.0f; - try - { - docLen = myWordDoc.Length; - } - catch (Exception ex1) - { - frmMain.AddError("Length Exception {0}\r\n\r\n{1}", ex1.Message, ex1.StackTrace); - } - //if (ci == null) ci = new ConfigInfo(null); - //ci.AddItem("Printing", "Length", string.Format("{0:0.0000}", docLen)); - string ascii = myWordDoc.MyWordDoc.Ascii; - string s = myWordDoc.Save(temppath); - myWordDoc.Close(); - WaitMS(wms); - docid = SaveDoc(temppath, title, ci, ascii, dtsutc); - DeleteFile(temppath); - DeleteFile(tmpName); - keepTrying = false; - if (attempt == 2) - { - frmMain.AddError("Success: MSWord was able to process file"); - frmMain.AddError("^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^"); - } - } - catch (Exception ex) - { - //frmMain.AddError(ex, "SaveWordDoc"); - frmMain.AddError("Convert Exception {0}\r\n\r\n{1}", ex.Message, ex.StackTrace); - if (attempt == 1) - frmMain.AddError("vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv"); - else - frmMain.AddError("============================================================================"); - frmMain.AddError("Attempt {0} - File Could not be opened in Word", attempt); - frmMain.AddError("{0}", fname); - frmMain.AddError("Section: '{0}'", title == string.Empty ? sectName : title); - log.Error("SaveWordDoc"); - log.ErrorFormat("oldstepsequence = {0}", fname); - log.ErrorFormat("{0}\r\n\r\n{1}", ex.Message, ex.InnerException); - log.ErrorFormat(ex.StackTrace); - MSWordToPDF.CloseApp(); - switch (attempt) - { - case 1: // Covert using RTB - CloseMSWord(myWordDoc); - frmMain.AddError("Attempting to fix the file and try again."); - // Get the rtf from the file - LoadAndSaveRichTextBox(tmpName); - attempt = 2; - break; - default: - frmMain.AddError("MS Word cannot process the file"); - if (myWordDoc != null) - { - TryToShowMSWord(myWordDoc); - CloseMSWord(myWordDoc); - } - docid = SaveDoc(fname, title, ci, "",dtsutc); // Save the original file - keepTrying = false; - DeleteFile(temppath); - DeleteFile(tmpName); - frmMain.AddError("Failure: Original contents saved to database"); - frmMain.AddError("^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^"); - break; - } - } + WordDoc.KillWordApps(); + Thread.Sleep(1000);// Give the process a second to shutdown + frmMain.AddError("Could not open file '{0}'", fname); + docid = SaveDoc(fname, title, ci, "", dtsutc); // Save the original file + } + else + { + if (ci == null) ci = new ConfigInfo(null); + ci.AddItem("Printing", "Length", string.Format("{0:0.0000}", wd.DocLen)); + docid = SaveDoc(temppath, title, ci, wd.ASCII, dtsutc); + DeleteFile(temppath); + DeleteFile(tmpName); } } else { if (frmMain.MySettings.ConvertTo == AccPageConversion.RichTextFormat) - docid = SaveDoc(fname, title, ci,"",dtsutc); // Need to get Ascii Text from RTF + docid = SaveDoc(fname, title, ci, "", dtsutc); // Need to get Ascii Text from RTF } } else - //log.ErrorFormat("Missing rtf file: {0}", fname); frmMain.AddError("Missing rtf file: {0}", fname); return docid; } @@ -195,17 +110,110 @@ namespace DataLoader { string rtf = LoadFileRaw(tmpName); // Parse for margins because the RTB will remove them + rtf = rtf.Replace("\x95", @"\bullet "); Match match = Regex.Match(rtf, @"((\\marg[^\\]+)+)", RegexOptions.Singleline); - rtf = rtf.Replace("\\sectd", ""); - rtf = rtf.Replace("\\sbkpage", ""); - rtf = rtf.Replace("\\sect", "\\page"); + //rtf = rtf.Replace("\\sectd", ""); + //rtf = rtf.Replace("\\sbkpage", ""); + //rtf = rtf.Replace("\\sect", "\\page"); + rtf = Regex.Replace(rtf, @"\\sectd(?=[ \\{])", ""); + rtf = Regex.Replace(rtf, @"\\sbkpage(?=[ \\{])", ""); + rtf = Regex.Replace(rtf, @"\\sect(?=[ \\{])", @"\page"); + rtf = Regex.Replace(rtf, @"\\par(?=[ \\{])", @" <>\par"); MyRichTextBox.Rtf = rtf; // Allow the RichTextBox to parse the RTF rtf = MyRichTextBox.Rtf; // Get the RTF back from the RichTextBox // Add the margins to the RTB output - rtf = rtf.Replace(@"\ansi", @"\ansi" + match.Value); + rtf = Regex.Replace(rtf, @"( |\r\n)<>\\par(?=[\r \\{])", @"\par"); + rtf = Regex.Replace(rtf, @" <>\\f. \\", @"\par\"); + rtf = Regex.Replace(rtf, @" <>", @"\par"); + rtf = Regex.Replace(rtf, @"\\ansi(?=[ \\{])", @"\ansi" + match.Value); + //rtf = rtf.Replace(@"\ansi\", @"\ansi\" + match.Value); // Save the modified rtf to the temporary file SaveFile(rtf, tmpName); } + private RichTextBox41 _MyRichTextBox41 = null; + public RichTextBox41 MyRichTextBox41 + { + get + { + if (_MyRichTextBox41 == null) + _MyRichTextBox41 = new RichTextBox41(); + return _MyRichTextBox41; + } + } + //private void LoadAndSaveRichTextBox41(string tmpName) + //{ + // string rtf = LoadFileRaw(tmpName); + // // Parse for margins because the RTB will remove them + // rtf = rtf.Replace("\x95", @"\bullet "); + // Match match = Regex.Match(rtf, @"((\\marg[^\\]+)+)", RegexOptions.Singleline); + // rtf = Regex.Replace(rtf, @"(?