Read and Save MSWord documents with RichTextBox before they are opened in word to reduce manual fixes to accessory pages

Added logic to replace sbkpage, sect, and sectd RTF tokens.
Use one RichTextBox to process text since RichTextBoxes are not released if the RTF value is set
This commit is contained in:
Rich 2012-07-25 21:39:01 +00:00
parent 808a5db5a9
commit bf9985110a

View File

@ -71,6 +71,7 @@ namespace DataLoader
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
*
@ -148,18 +149,7 @@ namespace DataLoader
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);
LoadAndSaveRichTextBox(tmpName);
attempt = 2;
break;
default:
@ -191,7 +181,31 @@ namespace DataLoader
frmMain.AddError("Missing rtf file: {0}", fname);
return docid;
}
private RichTextBox _MyRichTextBox = null;
public RichTextBox MyRichTextBox
{
get
{
if (_MyRichTextBox == null)
_MyRichTextBox = new RichTextBox();
return _MyRichTextBox;
}
}
private void LoadAndSaveRichTextBox(string tmpName)
{
string rtf = LoadFileRaw(tmpName);
// Parse for margins because the RTB will remove them
Match match = Regex.Match(rtf, @"((\\marg[^\\]+)+)", RegexOptions.Singleline);
rtf = rtf.Replace("\\sectd", "");
rtf = rtf.Replace("\\sbkpage", "");
rtf = rtf.Replace("\\sect", "\\page");
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);
// Save the modified rtf to the temporary file
SaveFile(rtf, tmpName);
}
private static void TryToShowMSWord(WordDoc myWordDoc)
{
try