From 3a27bf6eb65dd99200b2d74a10940483f086a587 Mon Sep 17 00:00:00 2001 From: Rich Date: Wed, 21 Apr 2010 20:52:24 +0000 Subject: [PATCH] Added code to handle conversion errors and code to create DocAscii field --- PROMS/DataLoader/Documents.cs | 290 ++++++++++++++++++++++++++-------- 1 file changed, 220 insertions(+), 70 deletions(-) diff --git a/PROMS/DataLoader/Documents.cs b/PROMS/DataLoader/Documents.cs index d435f380..738d2f0f 100644 --- a/PROMS/DataLoader/Documents.cs +++ b/PROMS/DataLoader/Documents.cs @@ -19,6 +19,7 @@ using System.Collections.Generic; using System.Xml; using System.IO; using System.Text; +using System.Text.RegularExpressions; using Volian.MSWord; using VEPROMS.CSLA.Library; @@ -26,9 +27,9 @@ namespace DataLoader { public partial class Loader { - private void SaveSectionDocument(string fname, string stpseq, string stype, ref int cid) + private void SaveSectionDocument(string fname, string stpseq, string stype, ref int cid, string sectName) { - int docid = SaveWordDoc(fname, stype); + int docid = SaveWordDoc(fname, stype, sectName); switch (docid) { case 0: @@ -44,80 +45,154 @@ namespace DataLoader break; } } - - private int SaveWordDoc(string fname, string title, string stype, ConfigInfo ci) + + private int SaveWordDoc(string fname, string title, string stype, ConfigInfo ci, string sectName) { int docid = 0; if (System.IO.File.Exists(fname)) { + FileInfo myFile = new FileInfo(fname); + //string tmpName = @"C:\Temp\DataLoader\" + myFile.Name.Replace(".", "_") + ".RTF"; + string tmpName = Path.GetTempFileName(); + string temppath = Path.GetTempFileName(); if (frmMain.cbSaveDocChecked) { - WordDoc d = new WordDoc(fname); - /* 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) + FileInfo tmpFile = new FileInfo(tmpName); + if (tmpFile.Exists) + tmpFile.Delete(); + myFile.CopyTo(tmpName); + bool keepTrying = true; + int attempt = 1; + while (keepTrying) { - // 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'); - d.SetLineSpacing(lpi); // if need landscape set too: , landscape); ; + WordDoc myWordDoc = null; + try + { + 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); // if need landscape set too: , landscape); ; + } + //string temppath = Path.GetTempFileName(); + /* 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); // if need landscape set too: , landscape); ; + } + float docLen = myWordDoc.Length; + 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); + 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"); + 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); + 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 + string rtf = LoadFileRaw(tmpName); + // Parse for margins because the RTB will remove them + Match match = Regex.Match(rtf, @"((\\marg[^\\]+)+)", RegexOptions.Singleline); + using (RichTextBox rtb = new RichTextBox()) + { + rtb.Rtf = rtf; // Allow the RichTextBox to parse the RTF + rtf = rtb.Rtf; // Get the RTF back from the RichTextBox + } + // Add the margins to the RTB output + rtf = rtf.Replace(@"\ansi", @"\ansi" + match.Value); + // Save the modified rtf to the temporary file + SaveFile(rtf, 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, ""); // Save the original file + keepTrying = false; + DeleteFile(temppath); + DeleteFile(tmpName); + frmMain.AddError("Failure: Original contents saved to database"); + frmMain.AddError("^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^"); + break; + } + } } - //string temppath = Path.GetTempFileName(); - /* 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'); - d.SetLineSpacing(lpi); // if need landscape set too: , landscape); ; - } - string temppath = Path.GetTempFileName(); - string s = d.Save(temppath); - float docLen = d.Length; - d.Close(); - WaitMS(wms); - if (ci == null) ci = new ConfigInfo(null); - ci.AddItem("Printing", "Length", string.Format("{0:0.0000}",docLen)); - docid = SaveDoc(temppath, title, ci); - File.Delete(temppath); } else { if (frmMain.cbSaveRTFChecked) - docid = SaveDoc(fname, title, ci); + docid = SaveDoc(fname, title, ci,""); // Need to get Ascii Text from RTF } } else @@ -125,11 +200,85 @@ namespace DataLoader frmMain.AddError("Missing rtf file: {0}", fname); return docid; } - private int SaveWordDoc(string temppath, string stype) + + private static void TryToShowMSWord(WordDoc myWordDoc) { - return SaveWordDoc(temppath, String.Empty, stype, null); + try + { + if (MessageBox.Show("Word is having problems processing this file.\r\n\r\nDo you want to see?", "MS Word Failure", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) + { + WordDoc.MyWordApp.Visible = true; + MessageBox.Show("Click on OK when you are done with MS Word", "Wait until done with MS Word", MessageBoxButtons.OK, MessageBoxIcon.Information); + } + } + catch (Exception ex) + { + Console.WriteLine("{0} - {1}", ex.GetType().Name, ex.Message); + } } - private int SaveTheDoc(string temppath, string title, ConfigInfo ci) + + private static void CloseMSWord(WordDoc myWordDoc) + { + try + { + if (myWordDoc != null) + { + WordDoc.CloseApp(); + Application.DoEvents(); + while (WordDoc.WordProcesses.Length > 0) + Application.DoEvents(); + } + } + catch (Exception ex) + { + Console.WriteLine("{0} - {1}", ex.GetType().Name, ex.Message); + } + } + private static void DeleteFile(string fName) + { + if (File.Exists(fName)) + File.Delete(fName); + } + private string LoadFileRaw(string fName) + { + try + { + FileInfo file = new FileInfo(fName); + byte[] myBytes = new byte[file.Length]; + using (FileStream fs = file.OpenRead()) + { + fs.Read(myBytes, 0, (int)file.Length); + fs.Close(); + } + StringBuilder sb = new StringBuilder(); + foreach (byte byt in myBytes) + { + int iByte = (int)byt; + sb.Append((char)byt); + } + return sb.ToString(); + } + catch (Exception ex) + { + Console.WriteLine("{0} - {1}", ex.GetType().Name, ex.Message); + Application.DoEvents(); + return LoadFileRaw(fName); + } + } + private void SaveFile(string str, string fName) + { + FileInfo file = new FileInfo(fName); + using (StreamWriter sw = file.CreateText()) + { + sw.Write(str); + sw.Close(); + } + } + private int SaveWordDoc(string temppath, string stype, string sectName) + { + return SaveWordDoc(temppath, String.Empty, stype, null, sectName); + } + private int SaveTheDoc(string temppath, string title, ConfigInfo ci, string ascii) { try { @@ -144,7 +293,8 @@ namespace DataLoader string docfile = temppath.Substring(0, temppath.LastIndexOf(".")) + @".doc"; if (File.Exists(docfile)) File.Delete(docfile); FileInfo doctmpFile = tmpFile.CopyTo(docfile); - doc.UpdateDocAscii(docfile); + doc.DocAscii = ascii; + //doc.UpdateDocAscii(docfile); doc.Save(); File.Delete(docfile); DocumentInfo di = DocumentInfo.Get(doc.DocID); @@ -167,14 +317,14 @@ namespace DataLoader } } - private int SaveDoc(string temppath, string title, ConfigInfo ci) + private int SaveDoc(string temppath, string title, ConfigInfo ci, string ascii) { int done = 0; int ntry = 0; while (done == 0 && ntry < 4) { ntry++; - done = SaveTheDoc(temppath, title, ci); + done = SaveTheDoc(temppath, title, ci, ascii); } return done; }