public RemainingRiskReport(int projectID, string sortingKey, object newDocumentLocation, byte[] remainingRiskHeaderPageTemplateLocation, byte[] remainingRiskPageTemplateLocation, byte[] indexPageTemplateLocation, byte[] frontPageTemplateLocation)
        {
            // This event will be raised on the worker thread when the worker starts.
            DoWorkEventArgs eventArgs = new GenerateRemainingRiskReportEvent(projectID, sortingKey, newDocumentLocation, remainingRiskHeaderPageTemplateLocation, remainingRiskPageTemplateLocation, indexPageTemplateLocation, frontPageTemplateLocation);

            this.runBackgroundWorker(eventArgs);
            InitializeComponent();
        }
        protected override void generateReport(object sender, DoWorkEventArgs args)
        {
            GenerateRemainingRiskReportEvent e = args as GenerateRemainingRiskReportEvent;

            if (e == null)
            {
                throw new Exception("No generate risk report event args parsed");
            }

            Document wordDocument = null;

            try
            {
                //Create a word interface so we can talk easylie to word.
                WordInterface wordInterface = new WordInterface();

                // Open a new document.
                FileStream newDocument = File.Create((string)e.newDocumentLocation + ".docx");
                newDocument.Close();
                newDocument = null;

                wordDocument = wordInterface.app.Documents.Open(e.newDocumentLocation + ".docx");

                DataRow  projectInfoRow = this.tbl_Risk_AnalysisTableAdapter.GetData().FindByProjectID(e.projectID);
                DataView riskDataRows   = new DataView(this.get_RemainingRisks_In_ProjectTableAdapter.GetData(e.projectID));

                riskDataRows.Sort = ARA_Globals.RiskSortingOptions[e.sortingKey];

                //Set generating process.

                backgroundWorker1.ReportProgress(5, (object)"Generating front page.");

                generateFrontPage(e.frontPageTemplateLocation, wordInterface, wordDocument, projectInfoRow);

                backgroundWorker1.ReportProgress(10, (object)"Generating page index.");

                //generateIndexPage(e.indexPageTemplateLocation, wordInterface, wordDocument,riskDataRows);

                backgroundWorker1.ReportProgress(15, (object)"Generating risk pages.");

                generateRemainingRiskPages(e.remainingRiskHeaderPageTemplateLocation, e.remainingRiskPageTemplateLocation, wordInterface, wordDocument, riskDataRows, projectInfoRow);

                backgroundWorker1.ReportProgress(68, (object)"Adding watermarks(if needed).");

                setWaterMark(wordInterface, wordDocument, projectInfoRow["StateName"].ToString(), projectInfoRow["DraftVersion"].ToString());

                backgroundWorker1.ReportProgress(70, (object)"Saving the document.");

                wordInterface.saveDocument(wordDocument, (string)e.newDocumentLocation);

                backgroundWorker1.ReportProgress(90, (object)"Converting document to pdf.");

                wordInterface.saveDocumentAsPdf(wordDocument, (string)e.newDocumentLocation);

                backgroundWorker1.ReportProgress(100, (object)"Done generating.");

                //Do we need to cancel the backgroundworker?
                if (backgroundWorker1.CancellationPending)
                {
                    args.Cancel = true;
                }

                //Clean the word interface.
                wordInterface.Quit();
                wordInterface = null;
            }
            catch (Exception ex)
            {
                System.Windows.Forms.MessageBox.Show(ARA_Constants.messageBoxSomethingWrongWhileGenerating + ex.Message, ARA_Constants.messageBoxSomethingWrongWhileGeneratingHeader, MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            finally
            {
                GC.Collect();
                GC.WaitForPendingFinalizers();
                GC.Collect();
                GC.WaitForPendingFinalizers();
            }

            // Close and release the Document object.
            if (wordDocument != null)
            {
                wordDocument.Close();
                wordDocument = null;
            }
        }