Exemplo n.º 1
0
        public Threading.Task PrintAsync(ViewClaim claim, string selectedPrinter)
        {
            if (claim == null)
            {
                throw new ArgumentNullException("claim");
            }
            Threading.Task tsk = Threading.Task.Run(async() =>
            {
                _jobId++;
                lock (_locker)     //Write to thread shared variable;
                {
                    _backgroundJobs.Enqueue(_jobId);
                }
                Documents docs = null;
                _Document doc  = null;
                try
                {
                    docs  = _wordApplication.Documents;
                    doc   = docs.Add(_wordTemplatePath);
                    var d = FieldMapping(claim);
                    FillFields(doc.Fields, d);

                    //doc.Activate();
                    //_wordApplication.Dialogs[WdWordDialog.wdDialogFilePrint].Show();
                    //if (result == -1)
                    //{
                    //    doc.PrintOut();
                    //}

                    //Don't call microsoft word PrintOut method directly here because PrintOut method
                    //Runs async and will return immediately, therefor letting you to close the word befor
                    //The print job finished which will lead to uncompleted printing job.
                    //So, the solution is to wrap the Word 'PrintOut' method in an asyn method and then
                    //wait on it so we will not proceed until my async method return which will return only
                    //after the Word PrintOut method runs to complete.
                    await PrintOut(doc, selectedPrinter);
                }
                finally
                {
                    doc.Close(false);
                    lock (_locker)     //Write to thread shared variable;
                    {
                        _backgroundJobs.Dequeue();
                    }
                }
            }
                                                    );
            return(tsk);
        }
Exemplo n.º 2
0
        private Dictionary <string, string> FieldMapping(ViewClaim claim)
        {
            Dictionary <string, string> lst = new Dictionary <string, string>();

            lst.Add("ClaimSequence", claim.SequenceText);
            lst.Add("CustomerName", claim.CustomerName);
            lst.Add("ClaimLetter", claim.ClaimLetter);
            lst.Add("GrandTotal", claim.GrandTotal.ToString("#,0", CultureInfo.InvariantCulture));
            //This is a flag to experience the ArgumentOutOfRangeException just once.
            bool argumentOutOfRangeReached = false;

            for (int i = 0; i < 10; i++)
            {
                string row = null;
                switch (i)
                {
                case 0:
                    row = "a";
                    break;

                case 1:
                    row = "b";
                    break;

                case 2:
                    row = "c";
                    break;

                case 3:
                    row = "d";
                    break;

                case 4:
                    row = "e";
                    break;

                case 5:
                    row = "f";
                    break;

                case 6:
                    row = "g";
                    break;

                case 7:
                    row = "h";
                    break;

                case 8:
                    row = "i";
                    break;

                case 9:
                    row = "j";
                    break;
                }
                try
                {
                    //User "#,0" format for ToString method so that you will get
                    //number formated with thousands separator and if the number is zero
                    //zero will be returned no empty string.
                    if (!argumentOutOfRangeReached)
                    {
                        var detailRow = claim.Details[i];
                        lst.Add((row + "1"), detailRow.PropertyType);
                        lst.Add((row + "2"), detailRow.TypeNo);
                        lst.Add((row + "3"), detailRow.PropertyLocation);
                        lst.Add((row + "4"), detailRow.Rent.ToString("#,0", CultureInfo.InvariantCulture));
                        lst.Add((row + "5"), detailRow.Maintenance.ToString("#,0", CultureInfo.InvariantCulture));
                        lst.Add((row + "6"), detailRow.Deposit.ToString("#,0", CultureInfo.InvariantCulture));
                        lst.Add((row + "7"), detailRow.Others.ToString("#,0", CultureInfo.InvariantCulture));
                        lst.Add((row + "8"), detailRow.Total.ToString("#,0", CultureInfo.InvariantCulture));
                        lst.Add((row + "9"), detailRow.Paid.ToString("#,0", CultureInfo.InvariantCulture));
                        lst.Add((row + "10"), detailRow.Balance.ToString("#,0", CultureInfo.InvariantCulture));
                        lst.Add((row + "11"), detailRow.OutstandingRentBalance.ToString("#,0", CultureInfo.InvariantCulture));
                        lst.Add((row + "12"), detailRow.OutstandingMaintBalance.ToString("#,0", CultureInfo.InvariantCulture));
                        lst.Add((row + "13"), detailRow.NetBalance.ToString("#,0", CultureInfo.InvariantCulture));
                    }
                    else
                    {
                        lst.Add((row + "1"), "");
                        lst.Add((row + "2"), "");
                        lst.Add((row + "3"), "");
                        lst.Add((row + "4"), "");
                        lst.Add((row + "5"), "");
                        lst.Add((row + "6"), "");
                        lst.Add((row + "7"), "");
                        lst.Add((row + "8"), "");
                        lst.Add((row + "9"), "");
                        lst.Add((row + "10"), "");
                        lst.Add((row + "11"), "");
                        lst.Add((row + "12"), "");
                        lst.Add((row + "13"), "");
                    }
                }
                catch (ArgumentOutOfRangeException)
                {
                    lst.Add((row + "1"), "");
                    lst.Add((row + "2"), "");
                    lst.Add((row + "3"), "");
                    lst.Add((row + "4"), "");
                    lst.Add((row + "5"), "");
                    lst.Add((row + "6"), "");
                    lst.Add((row + "7"), "");
                    lst.Add((row + "8"), "");
                    lst.Add((row + "9"), "");
                    lst.Add((row + "10"), "");
                    lst.Add((row + "11"), "");
                    lst.Add((row + "12"), "");
                    lst.Add((row + "13"), "");
                    argumentOutOfRangeReached = true;
                }
            }
            return(lst);
        }