Added File extension parameter to Make Document

Support for DocX and correction of Import Word Doc file
Change button from Print to Report
Support opening a Library Document that does not have any usages.
This commit is contained in:
Rich 2015-10-27 13:32:33 +00:00
parent d452b2f5b3
commit e22458bd1a
3 changed files with 243 additions and 241 deletions

View File

@ -272,20 +272,20 @@ namespace Volian.Controls.Library
private void InitializeWordDocument(LBDocumentClass doc) private void InitializeWordDocument(LBDocumentClass doc)
{ {
if (MyDocumentInfo.Config == null || MyDocumentInfo.Config == "") if (MyDocumentInfo.Config == null || MyDocumentInfo.Config == "" && MyDocumentInfo.DocumentEntryCount > 0)
{ {
DocStyle ds = MyDocumentInfo.DocumentEntries[0].MyContent.ContentItems[0].MyDocStyle; DocStyle ds = MyDocumentInfo.DocumentEntries[0].MyContent.ContentItems[0].MyDocStyle;
// this will cause an error and goto the Catch if the family or size is null, // this will cause an error and goto the Catch if the family or size is null,
// Westinghouse needs it to to this - at least for now // Westinghouse needs it to to this - at least for now
//if (ds.Font.Family != null) doc.Application.Selection.Font.Name = ds.Font.Family; //if (ds.Font.Family != null) doc.Application.Selection.Font.Name = ds.Font.Family;
//if (ds.Font.Size != null) doc.Application.Selection.Font.Size = (float)ds.Font.Size; //if (ds.Font.Size != null) doc.Application.Selection.Font.Size = (float)ds.Font.Size;
doc.Application.Selection.Font.Name = ds.Font.Family; doc.Application.Selection.Font.Name = ds.Font.Family;
doc.Application.Selection.Font.Size = (float)ds.Font.Size; doc.Application.Selection.Font.Size = (float)ds.Font.Size;
doc.Application.Selection.ParagraphFormat.SpaceBefore = 0; doc.Application.Selection.ParagraphFormat.SpaceBefore = 0;
doc.Application.Selection.ParagraphFormat.SpaceAfter = 0; doc.Application.Selection.ParagraphFormat.SpaceAfter = 0;
doc.Application.Selection.ParagraphFormat.LineSpacingRule = LBWdLineSpacing.wdLineSpaceExactly; doc.Application.Selection.ParagraphFormat.LineSpacingRule = LBWdLineSpacing.wdLineSpaceExactly;
doc.Application.Selection.ParagraphFormat.LineSpacing = 72 / 6; // for 6 LPI doc.Application.Selection.ParagraphFormat.LineSpacing = 72 / 6; // for 6 LPI
MSWordToPDF.AdjustMargins(ds, doc, false); MSWordToPDF.AdjustMargins(ds, doc, false);
} }
if (doc.ActiveWindow.ActivePane.View.Zoom.Percentage < 40) if (doc.ActiveWindow.ActivePane.View.Zoom.Percentage < 40)
doc.ActiveWindow.ActivePane.View.Zoom.Percentage = 100; doc.ActiveWindow.ActivePane.View.Zoom.Percentage = 100;

View File

@ -186,7 +186,7 @@ namespace Volian.Controls.Library
this.btnPrint.Name = "btnPrint"; this.btnPrint.Name = "btnPrint";
this.btnPrint.Size = new System.Drawing.Size(75, 30); this.btnPrint.Size = new System.Drawing.Size(75, 30);
this.btnPrint.TabIndex = 15; this.btnPrint.TabIndex = 15;
this.btnPrint.Text = "Print"; this.btnPrint.Text = "Report";
this.btnPrint.UseVisualStyleBackColor = true; this.btnPrint.UseVisualStyleBackColor = true;
this.btnPrint.Click += new System.EventHandler(this.btnPrint_Click); this.btnPrint.Click += new System.EventHandler(this.btnPrint_Click);
// //

View File

@ -16,50 +16,51 @@ namespace Volian.Controls.Library
public partial class DisplayLibDocs : UserControl public partial class DisplayLibDocs : UserControl
{ {
#region Properties #region Properties
private DocumentInfo _CurLibDoc; private DocumentInfo _CurLibDoc;
public DocumentInfo CurLibDoc public DocumentInfo CurLibDoc
{ {
get { return _CurLibDoc; } get { return _CurLibDoc; }
set { _CurLibDoc = value; } set { _CurLibDoc = value; }
} }
// selected index into listBoxLibDocs... // selected index into listBoxLibDocs...
private int _SelectedLibDoc = -1; private int _SelectedLibDoc = -1;
private DocumentInfoList _LibDocList; private DocumentInfoList _LibDocList;
public DocumentInfoList LibDocList public DocumentInfoList LibDocList
{ {
get { return _LibDocList; } get { return _LibDocList; }
set { _LibDocList = value; } set { _LibDocList = value; }
} }
private Color saveGroupPanelUsages; private Color saveGroupPanelUsages;
#endregion #endregion
#region Constructors #region Constructors
public DisplayLibDocs() public DisplayLibDocs()
{ {
InitializeComponent(); InitializeComponent();
btnOpenLibDoc.Enabled = false; btnOpenLibDoc.Enabled = false;
btnDelLibDoc.Enabled = false; btnDelLibDoc.Enabled = false;
btnPrint.Enabled = false; btnPrint.Enabled = listBoxLibDocs.Items.Count > 0;
saveGroupPanelUsages = groupPanelUsages.Style.BackColor; saveGroupPanelUsages = groupPanelUsages.Style.BackColor;
} }
DisplayTabControl _DisplayTabControl; DisplayTabControl _DisplayTabControl;
#endregion #endregion
#region LoadControlData #region LoadControlData
private bool _InitializingLibDocList; // used? see if selectionchanged event (still coding) private bool _InitializingLibDocList; // used? see if selectionchanged event (still coding)
public void LibDocListFillIn(DisplayTabControl tc) public void LibDocListFillIn(DisplayTabControl tc)
{ {
_InitializingLibDocList = true; _InitializingLibDocList = true;
listBoxLibDocs.Items.Clear(); listBoxLibDocs.Items.Clear();
tbComment.Clear(); tbComment.Clear();
tbName.Clear(); tbName.Clear();
groupPanelLibDocs.Style.BackColor = Color.Yellow; groupPanelLibDocs.Style.BackColor = Color.Yellow;
btnSave.Enabled = false; btnSave.Enabled = false;
btnCancel.Enabled = false; btnCancel.Enabled = false;
LibDocList = DocumentInfoList.GetLibraries(true); LibDocList = DocumentInfoList.GetLibraries(true);
for (int i = 0; i < LibDocList.Count; i++) for (int i = 0; i < LibDocList.Count; i++)
listBoxLibDocs.Items.Add(((LibDocList[i].DocumentEntries == null || LibDocList[i].DocumentEntries.Count == 0) ? "0 " : LibDocList[i].DocumentEntries.Count.ToString() + " ") + LibDocList[i].DocumentTitle); // + LibDocList[i].DocumentEntries.Count); listBoxLibDocs.Items.Add(((LibDocList[i].DocumentEntries == null || LibDocList[i].DocumentEntries.Count == 0) ? "0 " : LibDocList[i].DocumentEntries.Count.ToString() + " ") + LibDocList[i].DocumentTitle); // + LibDocList[i].DocumentEntries.Count);
_DisplayTabControl = tc; _DisplayTabControl = tc;
listBoxUsages.Items.Clear(); listBoxUsages.Items.Clear();
_InitializingLibDocList = false; _InitializingLibDocList = false;
btnPrint.Enabled = listBoxLibDocs.Items.Count > 0;
} }
public void SetSelectedLibDoc() public void SetSelectedLibDoc()
{ {
@ -122,225 +123,226 @@ namespace Volian.Controls.Library
btnOpenLibDoc.Enabled = false; btnOpenLibDoc.Enabled = false;
foreach (ItemInfo ii in dicnt.LibraryDocumentUsageList) foreach (ItemInfo ii in dicnt.LibraryDocumentUsageList)
{ {
if(ii.MyDocVersion!=null) if (ii.MyDocVersion != null)
listBoxUsages.Items.Add(ii.MyProcedure.ToString() + " - " + ii.MyContent.Number + " " + ii.MyContent.Text); listBoxUsages.Items.Add(ii.MyProcedure.ToString() + " - " + ii.MyContent.Number + " " + ii.MyContent.Text);
} }
btnPrint.Enabled = listBoxUsages.Items.Count > 0 ? true : false; btnPrint.Enabled = listBoxLibDocs.Items.Count > 0 ? true : false;
} }
private void btnDelLibDoc_Click(object sender, EventArgs e) private void btnDelLibDoc_Click(object sender, EventArgs e)
{ {
DocumentInfo di = LibDocList[_SelectedLibDoc]; DocumentInfo di = LibDocList[_SelectedLibDoc];
try try
{ {
Document.Delete(di.DocID); Document.Delete(di.DocID);
} }
catch (Exception ex) catch (Exception ex)
{ {
Console.WriteLine("error deleting doc = " + ex.Message); Console.WriteLine("error deleting doc = " + ex.Message);
} }
LibDocListFillIn(_DisplayTabControl); LibDocListFillIn(_DisplayTabControl);
} }
private void btnSave_Click(object sender, EventArgs e) private void btnSave_Click(object sender, EventArgs e)
{ {
bool chg = false; bool chg = false;
DocumentInfo di = LibDocList[_SelectedLibDoc]; DocumentInfo di = LibDocList[_SelectedLibDoc];
Document doc = Document.Get(di.DocID); Document doc = Document.Get(di.DocID);
if (tbName.Text != LibDocList[_SelectedLibDoc].LibTitle) if (tbName.Text != LibDocList[_SelectedLibDoc].LibTitle)
{ {
chg = true; chg = true;
doc.LibTitle = tbName.Text; doc.LibTitle = tbName.Text;
} }
DocumentConfig dc = new DocumentConfig(LibDocList[_SelectedLibDoc]); DocumentConfig dc = new DocumentConfig(LibDocList[_SelectedLibDoc]);
if (tbComment.Text != dc.LibDoc_Comment) if (tbComment.Text != dc.LibDoc_Comment)
{ {
chg = true; chg = true;
dc.LibDoc_Comment = tbComment.Text; dc.LibDoc_Comment = tbComment.Text;
doc.Config = dc.ToString(); doc.Config = dc.ToString();
} }
if (chg == true) if (chg == true)
{ {
doc.Save(); doc.Save();
int cursel = _SelectedLibDoc; int cursel = _SelectedLibDoc;
LibDocListFillIn(_DisplayTabControl); LibDocListFillIn(_DisplayTabControl);
listBoxLibDocs.SelectedIndex = cursel; listBoxLibDocs.SelectedIndex = cursel;
UpdateUsageList(); UpdateUsageList();
} }
} }
private void btnCancel_Click(object sender, EventArgs e) private void btnCancel_Click(object sender, EventArgs e)
{ {
DocumentInfo di = LibDocList[_SelectedLibDoc]; DocumentInfo di = LibDocList[_SelectedLibDoc];
Document doc = Document.Get(di.DocID); Document doc = Document.Get(di.DocID);
tbName.Text = doc.LibTitle; tbName.Text = doc.LibTitle;
DocumentConfig dc = new DocumentConfig(LibDocList[_SelectedLibDoc]); DocumentConfig dc = new DocumentConfig(LibDocList[_SelectedLibDoc]);
tbComment.Text = dc.LibDoc_Comment; tbComment.Text = dc.LibDoc_Comment;
btnSave.Enabled = false; btnSave.Enabled = false;
btnCancel.Enabled = false; btnCancel.Enabled = false;
} }
private void btnOpenLibDoc_Click(object sender, EventArgs e) private void btnOpenLibDoc_Click(object sender, EventArgs e)
{ {
DocumentInfo di = LibDocList[_SelectedLibDoc]; DocumentInfo di = LibDocList[_SelectedLibDoc];
_DisplayTabControl.OpenDSOTabPage(di); _DisplayTabControl.OpenDSOTabPage(di);
} }
private void tbName_TextChanged(object sender, EventArgs e) private void tbName_TextChanged(object sender, EventArgs e)
{ {
if (_InitializingLibDocList) return; if (_InitializingLibDocList) return;
btnSave.Enabled = true; btnSave.Enabled = true;
btnCancel.Enabled = true; btnCancel.Enabled = true;
} }
private void tbComment_TextChanged(object sender, EventArgs e) private void tbComment_TextChanged(object sender, EventArgs e)
{ {
if (_InitializingLibDocList) return; if (_InitializingLibDocList) return;
btnSave.Enabled = true; btnSave.Enabled = true;
btnCancel.Enabled = true; btnCancel.Enabled = true;
} }
private void btnImpLibDoc_Click(object sender, EventArgs e)
{
OpenFileDialog openFileDialog1 = new OpenFileDialog();
openFileDialog1.InitialDirectory = "c:\\"; private void btnImpLibDoc_Click(object sender, EventArgs e)
openFileDialog1.Filter = "Word files (*.doc;*.rtf)|*.doc;*.rtf|All files (*.*)|*.*"; {
openFileDialog1.FilterIndex = 0; OpenFileDialog openFileDialog1 = new OpenFileDialog();
openFileDialog1.RestoreDirectory = true;
if (openFileDialog1.ShowDialog() == DialogResult.OK) openFileDialog1.InitialDirectory = "c:\\";
{ openFileDialog1.Filter = "Word files (*.doc;*.rtf;*.docx)|*.doc;*.rtf;*.docx|All files (*.*)|*.*";
int done = 0; openFileDialog1.FilterIndex = 0;
int ntry = 0; openFileDialog1.RestoreDirectory = true;
while (done == 0 && ntry < 4)
{
ntry++;
done = SaveTheDoc(openFileDialog1.FileName, openFileDialog1.SafeFileName.Substring(0,openFileDialog1.SafeFileName.LastIndexOf(".")));
}
if (done > 0)
{
LibDocListFillIn(_DisplayTabControl);
// set selectedindex to the imported doc...
int indx = 0;
DocumentInfoList dil = DocumentInfoList.GetLibraries(true);
for(int i = 0; i<listBoxLibDocs.Items.Count; i++)
{
if (dil[i].DocID == done)
{
indx = i;
break;
}
}
listBoxLibDocs.SelectedIndex = indx;
}
}
}
private int SaveTheDoc(string temppath, string title)
{
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;
Document doc = Document.MakeDocument(t1, ByteArray, null, null); //, DateTime.Now, "Migration");
return doc.DocID;
}
// for an io exception, keep trying
catch (IOException)
{
Wait(2);
return 0;
}
catch (Exception ex)
{
MessageBox.Show(string.Format("Could not save the document, {0}, {1}", ex.Message, ex.InnerException));
return -1;
}
} if (openFileDialog1.ShowDialog() == DialogResult.OK)
private void Wait(int n) {
{ int done = 0;
DateTime dtw = DateTime.Now.AddSeconds(n); int ntry = 0;
while (DateTime.Now < dtw) while (done == 0 && ntry < 4)
{
Application.DoEvents();
}
}
private bool _ProcessingSelectedValueChanged = false;
private void listBoxUsages_SelectedValueChanged(object sender, EventArgs e)
{ {
if (!_InitializingLibDocList) ntry++;
{ done = SaveTheDoc(openFileDialog1.FileName, openFileDialog1.SafeFileName.Substring(0, openFileDialog1.SafeFileName.LastIndexOf(".")));
if (_ProcessingSelectedValueChanged) return;
_ProcessingSelectedValueChanged = true;
if (listBoxUsages.SelectedIndex == -1) return;
DocumentInfo di = LibDocList[listBoxLibDocs.SelectedIndex];
// see if the library document actually has data - data migration may have created a 'null' content within the
// record for missing library documents.
if (di.DocContent == null)
{
MessageBox.Show("No Content in this document");
return;
}
if (listBoxUsages.SelectedIndex < di.LibraryDocumentUsageList.Count && listBoxUsages.SelectedIndex >= 0)
{
ItemInfo ii = di.LibraryDocumentUsageList[listBoxUsages.SelectedIndex];
if (ii.MyDocVersion == null)
MessageBox.Show("This usage is not connected properly", "Disconnected Data", MessageBoxButtons.OK, MessageBoxIcon.Warning);
else
_DisplayTabControl.OpenItem(ii);
}
_ProcessingSelectedValueChanged = false;
}
} }
#endregion if (done > 0)
{
LibDocListFillIn(_DisplayTabControl);
// set selectedindex to the imported doc...
int indx = 0;
DocumentInfoList dil = DocumentInfoList.GetLibraries(true);
for (int i = 0; i < listBoxLibDocs.Items.Count; i++)
{
if (dil[i].DocID == done)
{
indx = i;
break;
}
}
listBoxLibDocs.SelectedIndex = indx;
}
}
}
private int SaveTheDoc(string temppath, string title)
{
try
{
FileInfo fi = new FileInfo(temppath);
FileStream fs = File.Open(temppath, FileMode.Open, FileAccess.Read, FileShare.None);
long len = fs.Length;
byte[] ByteArray = new byte[len];
int nBytesRead = fs.Read(ByteArray, 0, (int)len);
fs.Close();
string t1 = (title == null || title == "") ? null : title;
Document doc = Document.MakeDocument(t1, ByteArray, null, null, fi.Extension); //, DateTime.Now, "Migration");
return doc.DocID;
}
// for an io exception, keep trying
catch (IOException)
{
Wait(2);
return 0;
}
catch (Exception ex)
{
MessageBox.Show(string.Format("Could not save the document, {0}, {1}", ex.Message, ex.InnerException));
return -1;
}
}
private void Wait(int n)
{
DateTime dtw = DateTime.Now.AddSeconds(n);
while (DateTime.Now < dtw)
{
Application.DoEvents();
}
}
private bool _ProcessingSelectedValueChanged = false;
private void listBoxUsages_SelectedValueChanged(object sender, EventArgs e)
{
if (!_InitializingLibDocList)
{
if (_ProcessingSelectedValueChanged) return;
_ProcessingSelectedValueChanged = true;
if (listBoxUsages.SelectedIndex == -1) return;
DocumentInfo di = LibDocList[listBoxLibDocs.SelectedIndex];
// see if the library document actually has data - data migration may have created a 'null' content within the
// record for missing library documents.
if (di.DocContent == null)
{
MessageBox.Show("No Content in this document");
return;
}
if (listBoxUsages.SelectedIndex < di.LibraryDocumentUsageList.Count && listBoxUsages.SelectedIndex >= 0)
{
ItemInfo ii = di.LibraryDocumentUsageList[listBoxUsages.SelectedIndex];
if (ii.MyDocVersion == null)
MessageBox.Show("This usage is not connected properly", "Disconnected Data", MessageBoxButtons.OK, MessageBoxIcon.Warning);
else
_DisplayTabControl.OpenItem(ii);
}
_ProcessingSelectedValueChanged = false;
}
}
#endregion
private void btnPrint_Click(object sender, EventArgs e) private void btnPrint_Click(object sender, EventArgs e)
{ {
OnPrintRequest(new DisplayLibDocEventArgs("Library Document Usage",LibDocList)); OnPrintRequest(new DisplayLibDocEventArgs("Library Document Usage", LibDocList));
} }
} }
public class DisplayLibDocEventArgs public class DisplayLibDocEventArgs
{ {
private string _ReportTitle; private string _ReportTitle;
public string ReportTitle public string ReportTitle
{ {
get { return _ReportTitle; } get { return _ReportTitle; }
set { _ReportTitle = value; } set { _ReportTitle = value; }
} }
private DocumentInfoList _libDocList; private DocumentInfoList _libDocList;
public DocumentInfoList LibDocList public DocumentInfoList LibDocList
{ {
get { return _libDocList; } get { return _libDocList; }
set { _libDocList = value; } set { _libDocList = value; }
} }
//private string _SearchString = null; //private string _SearchString = null;
//public string SearchString //public string SearchString
//{ //{
// get { return _SearchString; } // get { return _SearchString; }
// set { _SearchString = value; } // set { _SearchString = value; }
//} //}
//private string _TypesSelected; //private string _TypesSelected;
//public string TypesSelected //public string TypesSelected
//{ //{
// get { return _TypesSelected; } // get { return _TypesSelected; }
// set { _TypesSelected = value; } // set { _TypesSelected = value; }
//} //}
//private ICollection<ItemInfo> _MyItemInfoList; //private ICollection<ItemInfo> _MyItemInfoList;
//public ICollection<ItemInfo> MyItemInfoList //public ICollection<ItemInfo> MyItemInfoList
//{ //{
// get { return _MyItemInfoList; } // get { return _MyItemInfoList; }
// set { _MyItemInfoList = value; } // set { _MyItemInfoList = value; }
//} //}
public DisplayLibDocEventArgs(string reportTitle, DocumentInfoList libDocList) public DisplayLibDocEventArgs(string reportTitle, DocumentInfoList libDocList)
{ {
_ReportTitle = reportTitle; _ReportTitle = reportTitle;
_libDocList = libDocList; _libDocList = libDocList;
} }
} }
public delegate void DisplayLibDocEvent(object sender, DisplayLibDocEventArgs args); public delegate void DisplayLibDocEvent(object sender, DisplayLibDocEventArgs args);
} }