369 lines
		
	
	
		
			13 KiB
		
	
	
	
		
			C#
		
	
	
	
	
	
			
		
		
	
	
			369 lines
		
	
	
		
			13 KiB
		
	
	
	
		
			C#
		
	
	
	
	
	
// ========================================================================
 | 
						|
// Copyright 2006 - Volian Enterprises, Inc. All rights reserved.          
 | 
						|
// Volian Enterprises - Proprietary Information - DO NOT COPY OR DISTRIBUTE
 | 
						|
// ------------------------------------------------------------------------
 | 
						|
// $Workfile: $     $Revision: $                                           
 | 
						|
// $Author: $   $Date: $                                                   
 | 
						|
//                                                                         
 | 
						|
// $History: $                                                             
 | 
						|
// ========================================================================
 | 
						|
using System;
 | 
						|
using System.Drawing;
 | 
						|
using System.Collections;
 | 
						|
using System.ComponentModel;
 | 
						|
using System.Windows.Forms;
 | 
						|
using System.Data;
 | 
						|
using System.Data.OleDb;
 | 
						|
using System.Collections.Specialized;
 | 
						|
using System.Collections.Generic;
 | 
						|
using System.Xml;
 | 
						|
using System.IO;
 | 
						|
using System.Text;
 | 
						|
using System.Text.RegularExpressions;
 | 
						|
using Volian.MSWord;
 | 
						|
using VEPROMS.CSLA.Library;
 | 
						|
using System.Threading;
 | 
						|
 | 
						|
namespace DataLoader
 | 
						|
{
 | 
						|
	public partial class Loader
 | 
						|
	{
 | 
						|
		private void SaveSectionDocument(string fname, string stpseq, string stype, ref int cid, string sectName, bool isLandscape)
 | 
						|
		{
 | 
						|
			int docid = SaveWordDoc(fname, stype, sectName, isLandscape);
 | 
						|
			switch (docid)
 | 
						|
			{
 | 
						|
				case 0:
 | 
						|
                    // could add the following back in - but many of these may be put out in the log, and
 | 
						|
                    // don't supply any pertinent info.
 | 
						|
                    //log.InfoFormat("Timing problem for save of word document, will retry. oldstepsequence = {0}", stpseq);
 | 
						|
					break;
 | 
						|
				case -1:
 | 
						|
					log.ErrorFormat("Could not complete save of word document, oldstepsequence = {0}", stpseq);
 | 
						|
					break;
 | 
						|
				default:
 | 
						|
					cid = docid;
 | 
						|
					break;
 | 
						|
			}
 | 
						|
		}
 | 
						|
 | 
						|
		private int SaveWordDoc(string fname, string title, string stype, bool isLandscape, ConfigInfo ci, string sectName, DateTime dtsutc)
 | 
						|
		{
 | 
						|
			int docid = 0;
 | 
						|
			if (System.IO.File.Exists(fname))
 | 
						|
			{
 | 
						|
				FileInfo myFile = new FileInfo(fname);
 | 
						|
				GC.Collect();
 | 
						|
				frmMain.AddInfo("Processing {0} {1}", fname, GC.GetTotalMemory(true));
 | 
						|
				//string tmpName = @"C:\Temp\DataLoader\" + myFile.Name.Replace(".", "_") + ".RTF";
 | 
						|
				string tmpName = Path.GetTempFileName();
 | 
						|
				string temppath = Path.GetTempFileName();
 | 
						|
				if (frmMain.MySettings.ExecutionMode == ExecutionMode.Production ||
 | 
						|
					frmMain.MySettings.ConvertTo == AccPageConversion.MSWord)
 | 
						|
				{
 | 
						|
					FileInfo tmpFile = new FileInfo(tmpName);
 | 
						|
					if (tmpFile.Exists)
 | 
						|
						tmpFile.Delete();
 | 
						|
					myFile.CopyTo(tmpName);
 | 
						|
					LoadAndSaveRichTextBox41(tmpName);
 | 
						|
					WordDocument wd = new WordDocument(tmpName, stype, temppath, isLandscape);
 | 
						|
					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.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
 | 
						|
				}
 | 
						|
			}
 | 
						|
			else
 | 
						|
				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
 | 
						|
			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 = Regex.Replace(rtf, @"\\ri[0-9]*(?=[ \\{])", "");
 | 
						|
			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 = 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);
 | 
						|
			if (rtf.StartsWith(@"{\rtf"))
 | 
						|
			{
 | 
						|
				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?");// 
 | 
						|
				rtf = rtf.Replace("`", @"\'B0");// Degree 
 | 
						|
				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, @"\\ri[0-9]*(?=[ \\{])", "");// Remove Right Indents
 | 
						|
				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);
 | 
						|
			}
 | 
						|
			else
 | 
						|
			{
 | 
						|
				MyRichTextBox41.Text = rtf;
 | 
						|
				rtf = MyRichTextBox41.Rtf;
 | 
						|
			}
 | 
						|
			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
 | 
						|
			{
 | 
						|
				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 static void CloseMSWord(WordDoc myWordDoc)
 | 
						|
		{
 | 
						|
			try
 | 
						|
			{
 | 
						|
				// RHM 20100628 - All of a sudden, this was null and would not work.
 | 
						|
				// I added the else clause so that the WinWord process would be terminated.
 | 
						|
				if (myWordDoc != null)
 | 
						|
				{
 | 
						|
					WordDoc.CloseApp();
 | 
						|
					Application.DoEvents();
 | 
						|
					while (WordDoc.WordProcesses.Length > 0)
 | 
						|
						Application.DoEvents();
 | 
						|
				}
 | 
						|
				else
 | 
						|
				{
 | 
						|
					WordDoc.KillWordApps();
 | 
						|
					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, bool isLandscape)
 | 
						|
		{
 | 
						|
			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, isLandscape, ci, sectName, myFile.LastWriteTimeUtc);
 | 
						|
		}
 | 
						|
		private int SaveTheDoc(string temppath, string title, ConfigInfo ci, string ascii, DateTime dtsutc)
 | 
						|
		{
 | 
						|
			try
 | 
						|
			{
 | 
						|
				FileStream fs = File.Open(temppath, FileMode.Open, FileAccess.Read, FileShare.None);
 | 
						|
				long len = fs.Length + 1;
 | 
						|
				byte[] ByteArray = new byte[len];
 | 
						|
				int nBytesRead = fs.Read(ByteArray, 0, (int)len);
 | 
						|
				fs.Close();
 | 
						|
                string t1 = (title == null || title == "") ? null : title;
 | 
						|
				using (Document doc = Document.MakeDocument(t1, ByteArray, null, ci == null ? null : ci.ToString(), dtsutc, "Migration", ".Doc"))
 | 
						|
				{
 | 
						|
					FileInfo tmpFile = new FileInfo(temppath);
 | 
						|
					string docfile = temppath.Substring(0, temppath.LastIndexOf(".")) + @".doc";
 | 
						|
					if (File.Exists(docfile)) File.Delete(docfile);
 | 
						|
					FileInfo doctmpFile = tmpFile.CopyTo(docfile);
 | 
						|
					doc.DocAscii = ascii;
 | 
						|
					doc.Save();
 | 
						|
					File.Delete(docfile);
 | 
						|
					return doc.DocID;
 | 
						|
				}
 | 
						|
			}
 | 
						|
			// for an io exception, keep trying
 | 
						|
			catch (IOException exio)
 | 
						|
			{
 | 
						|
				Console.WriteLine("IO: {0} - {1}", exio.GetType().Name, exio.Message);
 | 
						|
				frmMain.AddError(exio, "SaveTheDoc('{0}','{1}')", temppath, title);
 | 
						|
				Wait(2);
 | 
						|
				return 0;
 | 
						|
			}
 | 
						|
			catch (Exception ex)
 | 
						|
			{
 | 
						|
				log.Error("Save Word Doc");
 | 
						|
				log.ErrorFormat("{0}\r\n\r\n{1}", ex.Message, ex.InnerException);
 | 
						|
				frmMain.AddError(ex, "SaveTheDoc('{0}','{1}')", temppath, title);
 | 
						|
				return -1;
 | 
						|
			}
 | 
						|
 | 
						|
		}
 | 
						|
		private int SaveDoc(string temppath, string title, ConfigInfo ci, string ascii, DateTime dtsutc)
 | 
						|
		{
 | 
						|
			int done = 0;
 | 
						|
			int ntry = 0;
 | 
						|
			while (done == 0 && ntry < 4)
 | 
						|
			{
 | 
						|
				ntry++;
 | 
						|
				done = SaveTheDoc(temppath, title, ci, ascii, dtsutc);
 | 
						|
			}
 | 
						|
			return done;
 | 
						|
		}
 | 
						|
	}	 
 | 
						|
}
 |