private void generateFrontPage(byte[] templateLocation, WordInterface wordInterface, Document wordDocument, DataRow projectInfoRow) { lock (this.threadLock) { try { //Open the template String tempTemplateFile = Path.GetTempFileName(); File.WriteAllBytes(tempTemplateFile, templateLocation); Document frontPageTemplate = wordInterface.app.Documents.Open(tempTemplateFile); //Copy document to main document. wordInterface.copyDocumentToOtherDocument(frontPageTemplate, wordDocument, true); wordDocument.Activate(); //Set template data. wordInterface.searchAndReplace(wordDocument, "<CustomerName>", projectInfoRow["Customer"].ToString()); wordInterface.searchAndReplace(wordDocument, "<MachineNumber>", projectInfoRow["MachineNumber"].ToString()); wordInterface.searchAndReplace(wordDocument, "<OrderNumber>", projectInfoRow["OrderNumber"].ToString()); wordInterface.searchAndReplace(wordDocument, "<MachineType>", projectInfoRow["MachineType"].ToString()); wordInterface.searchAndReplace(wordDocument, "<CurrentDate>", ARA_Globals.ARa_Date); //Remove template from memory. if (frontPageTemplate != null) { ((_Document)frontPageTemplate).Close(ref paramFalse, ref missing, ref missing); File.Delete(tempTemplateFile); frontPageTemplate = null; } } catch (Exception ex) { throw; } finally { GC.Collect(); GC.WaitForPendingFinalizers(); GC.Collect(); GC.WaitForPendingFinalizers(); } } }
private void generateRemainingRiskPages(byte[] headerTemplateLocation, byte[] templateLocation, WordInterface wordInterface, Document wordDocument, DataView riskDataRows, DataRow projectInfoRow) { lock (this.threadLock) { int currentRisk = 0; try { //Open template documents. String tempHeaderTemplateFile = Path.GetTempFileName(); String tempTemplateFile = Path.GetTempFileName(); File.WriteAllBytes(tempHeaderTemplateFile, headerTemplateLocation); File.WriteAllBytes(tempTemplateFile, templateLocation); Document remainingRiskHeaderTemplate = wordInterface.app.Documents.Open(tempHeaderTemplateFile); Document remainingRiskTemplate = wordInterface.app.Documents.Open(tempTemplateFile); foreach (DataRowView riskDataRow in riskDataRows) { //Set generating process backgroundWorker1.ReportProgress( 33 + (Int32)((float)currentRisk / (float)riskDataRows.Count * 33), (object)string.Format("Generating page {0} of {1}.", currentRisk + 1, riskDataRows.Count) ); currentRisk++; //Get the risk data id and set some variables. int riskDataID = (Int32)riskDataRow["RiskDataID"]; Color[] riskEstimationColors = { ARA_Colors.ARA_Green, ARA_Colors.ARA_Orange, ARA_Colors.ARA_Red }; //Get some more info about the risk. DataRow riskData = this.tbl_Risk_DataTableAdapter.GetData().FindByRiskDataID(riskDataID); DataView riskEstimationBeforeView = new DataView(this.get_RiskEstimation_In_RiskData_BeforeTableAdapter.GetData(riskDataID)); DataView riskEstimationAfterView = new DataView(this.get_RiskEstimation_In_RiskData_AfterTableAdapter.GetData(riskDataID)); DataView appliedRiskReductionMeasures = new DataView(this.get_RiskReduction_In_RiskDataTableAdapter.GetData(riskDataID)); DataView minimalAdditionMeasures = new DataView(this.get_MinimalAddition_In_RiskDataTableAdapter.GetData(riskDataID)); //Set some value's before copying document. remainingRiskTemplate.Activate(); foreach (DataRow exposedPersonRow in this.get_ExposedPersons_In_RiskDataTableAdapter.GetData(riskDataID).Rows) { ContentControls personCheckbox = remainingRiskTemplate.SelectContentControlsByTitle(exposedPersonRow["PersonDescription"].ToString()); if (personCheckbox.Count > 0) { personCheckbox[(object)1].Checked = exposedPersonRow["InProject"].ToString() == "1"; } } //Copy template. if (currentRisk % 2 == 1) { wordInterface.copyDocumentToOtherDocument(remainingRiskHeaderTemplate, wordDocument, false); wordInterface.copyDocumentToOtherDocument(remainingRiskTemplate, wordDocument, false); } else { wordInterface.copyDocumentToOtherDocument(remainingRiskTemplate, wordDocument, true); } wordDocument.Activate(); //Sets the risk hazard specifications. generateHazardIndentification(wordInterface, wordDocument, (Int32)riskData["DangerID"], (Int32)riskData["DangerSourceID"]); //Search and replace headerinfo. wordInterface.searchAndReplace(wordDocument, "<CustomerName>", projectInfoRow["Customer"].ToString()); wordInterface.searchAndReplace(wordDocument, "<MachineInfo>", string.Format("{0}/{1}", projectInfoRow["MachineNumber"].ToString(), projectInfoRow["OrderNumber"].ToString())); wordInterface.searchAndReplace(wordDocument, "<RiskID>", riskDataRow["RiskID"].ToString()); wordInterface.searchAndReplace(wordDocument, "<BriefActionDescription>", riskDataRow["HazardSituation"].ToString(), ARA_Colors.ARA_Red); //Search and replace riskdata. wordInterface.searchAndReplace(wordDocument, "<RiskGroup>", string.Format("{0} - {1}", riskDataRow["GroupName"].ToString(), riskDataRow["TypeName"].ToString())); wordInterface.searchAndReplace(wordDocument, "<ActionEvent>", riskData["HazardEvent"].ToString()); wordInterface.searchAndReplace(wordDocument, "<RiskReductionInfo>", riskData["RiskReductionInfo"].ToString()); wordInterface.searchAndReplace(wordDocument, "<MinimalAdditionInfo>", riskData["MinimalAdditionInfo"].ToString()); //Set the riskestimation fields. ARA_EditRiskRiskEstimation temp = new ARA_EditRiskRiskEstimation(); temp.setControlData(riskEstimationBeforeView); //Set risk class fields. riskEstimationBeforeView.RowFilter = "InProject = '1'"; if (riskEstimationBeforeView.Count != 4) { throw new Exception("Cant generate report, because risk " + riskDataID.ToString() + " isn't correctly filled in."); } wordInterface.searchAndReplace(wordDocument, "<RiskClassValueB>", temp.calculateRiskEstimationClass().ToString()); wordInterface.searchAndReplace(wordDocument, "<RiskClassDescriptionB>", ARA_Globals.RiskClassDescription[temp.calculateRiskEstimationClass()], riskEstimationColors[temp.calculateRiskEstimationClass()]); temp.setControlData(riskEstimationAfterView); riskEstimationAfterView.RowFilter = "InProject = '1'"; if (riskEstimationAfterView.Count != 4) { throw new Exception("Cant generate report, because risk " + riskDataID.ToString() + " isn't correctly filled in."); } wordInterface.searchAndReplace(wordDocument, "<RiskClassValueA>", temp.calculateRiskEstimationClass().ToString()); wordInterface.searchAndReplace(wordDocument, "<RiskClassDescriptionA>", ARA_Globals.RiskClassDescription[temp.calculateRiskEstimationClass()], riskEstimationColors[temp.calculateRiskEstimationClass()]); //Find minimal addition options table and fill it. Microsoft.Office.Interop.Word.Table minimalAdditionMeasuresTable = wordInterface.findTableWithTitle(wordDocument, "MinimalAdditionMeasures"); minimalAdditionMeasures.RowFilter = "InProject = '1'"; wordInterface.fillTableWithRiskReducingMeasures(wordDocument, minimalAdditionMeasuresTable, minimalAdditionMeasures, "MeasureSubGroup", "InProject"); minimalAdditionMeasuresTable.Title = ""; //Clear some memory. temp.Dispose(); temp = null; //Do we need to cancel execution? if (backgroundWorker1.CancellationPending) { break; } } //Remove template from memory. if (remainingRiskTemplate != null) { ((_Document)remainingRiskTemplate).Close(ref paramFalse, ref missing, ref missing); File.Delete(tempTemplateFile); remainingRiskTemplate = null; } //Remove headerTemplate from memory. if (remainingRiskHeaderTemplate != null) { ((_Document)remainingRiskHeaderTemplate).Close(ref paramFalse, ref missing, ref missing); File.Delete(tempHeaderTemplateFile); remainingRiskHeaderTemplate = null; } } catch (Exception ex) { } } }
private void generateRiskPages(byte[] templateLocation, WordInterface wordInterface, Document wordDocument, DataView riskDataRows, DataRow projectInfoRow) { lock (this.threadLock) { int currentRisk = 0; try { //Open template document. String tempTemplateFile = Path.GetTempFileName(); File.WriteAllBytes(tempTemplateFile, templateLocation); Document riskTemplate = wordInterface.app.Documents.Open(tempTemplateFile); foreach (DataRowView riskDataRow in riskDataRows) { //Set generating process backgroundWorker1.ReportProgress( 33 + (Int32)((float)currentRisk / (float)riskDataRows.Count * 33), (object)string.Format("Generating page {0} of {1}.", currentRisk + 1, riskDataRows.Count) ); currentRisk++; //Get the risk data id and set some variables. int riskDataID = riskDataRow["ProjectRiskDataID"] != DBNull.Value ? (Int32)riskDataRow["ProjectRiskDataID"] : (Int32)riskDataRow["DefaultRiskDataID"]; Color[] riskEstimationColors = { ARA_Colors.ARA_Green, ARA_Colors.ARA_Orange, ARA_Colors.ARA_Red }; //Get some more info about the risk. DataRow riskData = this.tbl_Risk_DataTableAdapter.GetData().FindByRiskDataID(riskDataID); DataRow authorRow = this.tbl_UserTableAdapter.GetData().FindByUserID((Int32)projectInfoRow["UserID"]); DataView riskEstimationBeforeView = new DataView(this.get_RiskEstimation_In_RiskData_BeforeTableAdapter.GetData(riskDataID)); DataView riskEstimationAfterView = new DataView(this.get_RiskEstimation_In_RiskData_AfterTableAdapter.GetData(riskDataID)); DataView appliedRiskReductionMeasures = new DataView(this.get_RiskReduction_In_RiskDataTableAdapter.GetData(riskDataID)); DataView minimalAdditionMeasures = new DataView(this.get_MinimalAddition_In_RiskDataTableAdapter.GetData(riskDataID)); //Add fileID row to the riskDataRow procedure. //Get the blob data and convert it to byte[]. //And call the wordInterface function to add image watermark to a specific page. //Disable default watermark generation. //if(riskDataRow["fileID"] != DBNull.Value) //{ // DataRow blobDataView = this.tbl_BLOB_StorageTableAdapter.GetData().FindByFileID((Int32)riskDataRow["fileID"]); //} //Set some value's before copying document. riskTemplate.Activate(); foreach (DataRow exposedPersonRow in this.get_ExposedPersons_In_RiskDataTableAdapter.GetData(riskDataID).Rows) { ContentControls personCheckbox = riskTemplate.SelectContentControlsByTitle(exposedPersonRow["PersonDescription"].ToString()); if (personCheckbox.Count > 0) { personCheckbox[(object)1].Checked = exposedPersonRow["InProject"].ToString() == "1"; } } //Copy template. wordInterface.copyDocumentToOtherDocument(riskTemplate, wordDocument, true); wordDocument.Activate(); //Sets the risk hazard specifications. generateHazardIndentification(wordInterface, wordDocument, (Int32)riskData["DangerID"], (Int32)riskData["DangerSourceID"]); //Search and replace headerinfo. wordInterface.searchAndReplace(wordDocument, "<CustomerName>", projectInfoRow["Customer"].ToString()); wordInterface.searchAndReplace(wordDocument, "<MachineInfo>", string.Format("{0}/{1}", projectInfoRow["MachineNumber"].ToString(), projectInfoRow["OrderNumber"].ToString())); wordInterface.searchAndReplace(wordDocument, "<RiskID>", riskDataRow["RiskID"].ToString()); wordInterface.searchAndReplace(wordDocument, "<BriefActionDescription>", riskDataRow["HazardSituation"].ToString(), ARA_Colors.ARA_Red); //Search and replace riskdata. wordInterface.searchAndReplace(wordDocument, "<RiskGroup>", string.Format("{0} - {1}", riskDataRow["GroupName"].ToString(), riskDataRow["TypeName"].ToString())); wordInterface.searchAndReplace(wordDocument, "<ActionEvent>", riskData["HazardEvent"].ToString()); wordInterface.searchAndReplace(wordDocument, "<RiskReductionInfo>", riskData["RiskReductionInfo"].ToString()); wordInterface.searchAndReplace(wordDocument, "<MinimalAdditionInfo>", riskData["MinimalAdditionInfo"].ToString()); //Set the riskestimation fields. ARA_EditRiskRiskEstimation temp = new ARA_EditRiskRiskEstimation(); temp.setControlData(riskEstimationBeforeView); riskEstimationBeforeView.RowFilter = "InProject = '1'"; if (riskEstimationBeforeView.Count != 4) { throw new Exception("Cant generate report, because risk " + riskDataRow["RiskID"].ToString() + " isn't correctly filled in."); } wordInterface.searchAndReplace(wordDocument, "<SEDescriptionB>", riskEstimationBeforeView[0]["ItemDescription"].ToString()); wordInterface.searchAndReplace(wordDocument, "<FRDescriptionB>", riskEstimationBeforeView[1]["ItemDescription"].ToString()); wordInterface.searchAndReplace(wordDocument, "<PRDescriptionB>", riskEstimationBeforeView[2]["ItemDescription"].ToString()); wordInterface.searchAndReplace(wordDocument, "<AVDescriptionB>", riskEstimationBeforeView[3]["ItemDescription"].ToString()); wordInterface.searchAndReplace(wordDocument, "<SEWeightB>", riskEstimationBeforeView[0]["ItemWeight"].ToString()); wordInterface.searchAndReplace(wordDocument, "<FRWeightB>", riskEstimationBeforeView[1]["ItemWeight"].ToString()); wordInterface.searchAndReplace(wordDocument, "<PRWeightB>", riskEstimationBeforeView[2]["ItemWeight"].ToString()); wordInterface.searchAndReplace(wordDocument, "<AVWeightB>", riskEstimationBeforeView[3]["ItemWeight"].ToString()); wordInterface.searchAndReplace(wordDocument, "<RiskClassValueB>", temp.calculateRiskEstimationClass().ToString()); wordInterface.searchAndReplace(wordDocument, "<RiskClassDescriptionB>", ARA_Globals.RiskClassDescription[temp.calculateRiskEstimationClass()], riskEstimationColors[temp.calculateRiskEstimationClass()]); temp.setControlData(riskEstimationAfterView); riskEstimationAfterView.RowFilter = "InProject = '1'"; if (riskEstimationAfterView.Count != 4) { throw new Exception("Cant generate report, because risk " + riskDataRow["RiskID"].ToString() + " isn't correctly filled in."); } wordInterface.searchAndReplace(wordDocument, "<SEDescriptionA>", riskEstimationAfterView[0]["ItemDescription"].ToString()); wordInterface.searchAndReplace(wordDocument, "<FRDescriptionA>", riskEstimationAfterView[1]["ItemDescription"].ToString()); wordInterface.searchAndReplace(wordDocument, "<PRDescriptionA>", riskEstimationAfterView[2]["ItemDescription"].ToString()); wordInterface.searchAndReplace(wordDocument, "<AVDescriptionA>", riskEstimationAfterView[3]["ItemDescription"].ToString()); wordInterface.searchAndReplace(wordDocument, "<SEWeightA>", riskEstimationAfterView[0]["ItemWeight"].ToString()); wordInterface.searchAndReplace(wordDocument, "<FRWeightA>", riskEstimationAfterView[1]["ItemWeight"].ToString()); wordInterface.searchAndReplace(wordDocument, "<PRWeightA>", riskEstimationAfterView[2]["ItemWeight"].ToString()); wordInterface.searchAndReplace(wordDocument, "<AVWeightA>", riskEstimationAfterView[3]["ItemWeight"].ToString()); wordInterface.searchAndReplace(wordDocument, "<RiskClassValueA>", temp.calculateRiskEstimationClass().ToString()); wordInterface.searchAndReplace(wordDocument, "<RiskClassDescriptionA>", ARA_Globals.RiskClassDescription[temp.calculateRiskEstimationClass()], riskEstimationColors[temp.calculateRiskEstimationClass()]); //Find table and do stuff with it. Microsoft.Office.Interop.Word.Table appliedRiskReductionMeasuresTable = wordInterface.findTableWithTitle(wordDocument, "AppliedRiskReductionMeasures"); appliedRiskReductionMeasures.RowFilter = "InProject = '1'"; wordInterface.fillTableWithRiskReducingMeasures(wordDocument, appliedRiskReductionMeasuresTable, appliedRiskReductionMeasures, "MeasureSubGroup", "InProject"); appliedRiskReductionMeasuresTable.Title = ""; Microsoft.Office.Interop.Word.Table minimalAdditionMeasuresTable = wordInterface.findTableWithTitle(wordDocument, "MinimalAdditionMeasures"); minimalAdditionMeasures.RowFilter = "InProject = '1'"; wordInterface.fillTableWithRiskReducingMeasures(wordDocument, minimalAdditionMeasuresTable, minimalAdditionMeasures, "MeasureSubGroup", "InProject"); minimalAdditionMeasuresTable.Title = ""; //Set author en reviewer names. wordInterface.searchAndReplace(wordDocument, "<AuthorName>", authorRow["UserName"].ToString(), new Color(), true); //Did someone review this risk? if (riskDataRow["ReviewedByUser"] != DBNull.Value) { DataRow reviewerRow = this.tbl_UserTableAdapter.GetData().FindByUserID((Int32)riskDataRow["ReviewedByUser"]); wordInterface.searchAndReplace(wordDocument, "<ReviewerName>", reviewerRow["UserName"].ToString(), new Color(), true); } else { wordInterface.searchAndReplace(wordDocument, "<ReviewerName>", "NOT REVIEWED", new Color(), true); } //Clear some memory. temp.Dispose(); temp = null; //Do we need to cancel execution? if (backgroundWorker1.CancellationPending) { break; } } //Remove template from memory. if (riskTemplate != null) { ((_Document)riskTemplate).Close(ref paramFalse, ref missing, ref missing); File.Delete(tempTemplateFile); riskTemplate = null; } } catch (Exception ex) { System.Windows.Forms.MessageBox.Show(ARA_Constants.messageBoxSomethingWrongWhileGenerating + ex.Message, ARA_Constants.messageBoxSomethingWrongWhileGeneratingHeader, MessageBoxButtons.OK, MessageBoxIcon.Warning); } } }