Use threading to keep MSWord hangs from hanging the conversion process
Fix logic where RichTextBox 4.1 is used to convert RTF text to a word document
This commit is contained in:
parent
0e66393289
commit
32e9b47dae
@ -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>>\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>>\\par(?=[\r \\{])", @"\par");
|
||||
rtf = Regex.Replace(rtf, @" <<par>>\\f. \\", @"\par\");
|
||||
rtf = Regex.Replace(rtf, @" <<par>>", @"\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, @"(?<!\\[^ \\\r\n]*)\\sectd ", "");
|
||||
// rtf = Regex.Replace(rtf, @"(?<!\\[^ \\\r\n]*)\\sbkpage ", "");
|
||||
// rtf = Regex.Replace(rtf, @"\\sectd(?=[\\{])", "");
|
||||
// rtf = Regex.Replace(rtf, @"\\sbkpage(?=[\\{])", "");
|
||||
// rtf = Regex.Replace(rtf, @"\\sect(?=[ \\{])", @"\page");
|
||||
// MyRichTextBox41.Rtf = rtf; // Allow the RichTextBox to parse the RTF
|
||||
// rtf = MyRichTextBox41.Rtf; // Get the RTF back from the RichTextBox
|
||||
// // Add the margins to the RTB output
|
||||
// rtf = Regex.Replace(rtf, @"\\ansi(?=[ \\{])", @"\ansi" + match.Value);
|
||||
// // Save the modified rtf to the temporary file
|
||||
// SaveFile(rtf, tmpName);
|
||||
//}
|
||||
private void LoadAndSaveRichTextBox41(string tmpName)
|
||||
{
|
||||
string rtf = LoadFileRaw(tmpName);
|
||||
rtf = StripTokensFromFontTable(rtf);
|
||||
// Parse for margins because the RTB will remove them
|
||||
rtf = rtf.Replace("\x95", @"\bullet ");// Bullet
|
||||
rtf = rtf.Replace("\x93", @"\'93");// Left Double Quote
|
||||
rtf = rtf.Replace("\x94", @"\'94");// Right Double Quote
|
||||
rtf = rtf.Replace("\x96", @"\u8211?");//
|
||||
rtf = rtf.Replace("\x89", @"\u8240?");//
|
||||
rtf = rtf.Replace("\x92", @"\u8217?");//
|
||||
rtf = rtf.Replace("\x84", @"\u8222?");//
|
||||
rtf = rtf.Replace("\x85", @"\u8230?");//
|
||||
rtf = rtf.Replace("\x97", @"\u8212?");//
|
||||
rtf = rtf.Replace("\x9C", @"\u339?");//
|
||||
rtf = rtf.Replace("\x98", @"\u732?");//
|
||||
Match match = Regex.Match(rtf, @"((\\marg[^\\]+)+)", RegexOptions.Singleline);
|
||||
rtf = Regex.Replace(rtf, @"(?<!\\[^ \\\r\n]*)\\sectd ", "");
|
||||
rtf = Regex.Replace(rtf, @"(?<!\\[^ \\\r\n]*)\\sbkpage ", "");
|
||||
rtf = Regex.Replace(rtf, @"\\sectd(?=[\\{])", "");
|
||||
rtf = Regex.Replace(rtf, @"\\sbkpage(?=[\\{])", "");
|
||||
rtf = Regex.Replace(rtf, @"\\sect(?=[ \\{\r])", @"\page");
|
||||
MyRichTextBox41.Rtf = rtf; // Allow the RichTextBox to parse the RTF
|
||||
rtf = MyRichTextBox41.Rtf; // Get the RTF back from the RichTextBox
|
||||
// Add the margins to the RTB output
|
||||
rtf = Regex.Replace(rtf, @"\\ansi(?=[ \\{])", @"\ansi" + match.Value);
|
||||
// Save the modified rtf to the temporary file
|
||||
SaveFile(rtf, tmpName);
|
||||
}
|
||||
private string _Prefix;
|
||||
private string FixFontTableEntry(Match m)
|
||||
{
|
||||
string tableEntry = m.ToString();
|
||||
if (tableEntry.Contains(@"\page "))
|
||||
{
|
||||
tableEntry = tableEntry.Replace(@"\page ", "");
|
||||
_Prefix += "\x0C";
|
||||
}
|
||||
if (tableEntry.Contains(@"\column "))
|
||||
{
|
||||
tableEntry = tableEntry.Replace(@"\column ", "");
|
||||
_Prefix += "\x0E";
|
||||
}
|
||||
if (tableEntry.Contains(@"\tab "))
|
||||
{
|
||||
tableEntry = tableEntry.Replace(@"\tab ", "");
|
||||
_Prefix += "\x09";
|
||||
}
|
||||
return tableEntry;
|
||||
}
|
||||
private string StripTokensFromFontTable(string rtf)
|
||||
{
|
||||
_Prefix = "";
|
||||
string rtfout = Regex.Replace(rtf, @"{\\f[0-9]+[^}]*}", new MatchEvaluator(FixFontTableEntry), RegexOptions.Singleline);
|
||||
return rtfout;
|
||||
}
|
||||
private static void TryToShowMSWord(WordDoc myWordDoc)
|
||||
{
|
||||
try
|
||||
@ -293,6 +301,7 @@ namespace DataLoader
|
||||
ConfigInfo ci = new ConfigInfo(null);
|
||||
ci.AddItem("Edit", "Initialized", "true");
|
||||
FileInfo myFile = new FileInfo(temppath);
|
||||
ci.AddItem("History", "OriginalFileName", myFile.Name);
|
||||
return SaveWordDoc(temppath, String.Empty, stype, ci, sectName, myFile.LastWriteTimeUtc);
|
||||
}
|
||||
private int SaveTheDoc(string temppath, string title, ConfigInfo ci, string ascii, DateTime dtsutc)
|
||||
|
Loading…
x
Reference in New Issue
Block a user