Beispiel #1
0
        private async Task <List <CockleFilePdf> > executePdfConversionForProof(
            IProgress <string> progress, CancellationToken cancellationToken, List <CockleFile> latestFiles)
        {
            bool exceptionThrownInAwait = false; // tracks excptn in await

            List <CockleFile> filesSelectedForConversion;
            bool convertAll = false;

            // get files from grid if null
            if (null == latestFiles)
            {
                filesSelectedForConversion = SelectedFiles;
            }
            else
            {
                convertAll = true; filesSelectedForConversion = latestFiles;
            }

            if (filesSelectedForConversion.Count < 1)
            {
                throw new Exception();
            }

            // begin AWAIT
            var filesToReturnFromTask = await Task.Run(() =>
            {
                //instantiate Word Application & Document
                Word.Application app = new Word.Application();
                app.Visible          = false; //true; // changed 9-7-17
                Word.Document doc    = null;

                //get original printer & prepare to create PDF
                var current_printer        = app.ActivePrinter;
                var konica_554_prn_printer = "554 Print to File";
                app.ActivePrinter          = konica_554_prn_printer;

                // collection of special class to track files
                var filesPrintedToPrnSuccessfully = new List <FilesPrintedToPrnSuccessfully>();
                // the Word file saved to scratch
                try
                {
                    // counter to track files printed
                    int i = 0;
                    // Q. copy all files to scratch?  A. cannot, because need tagline to read server drive



                    // loop through files
                    foreach (CockleFile fileSelected in filesSelectedForConversion)
                    {
                        // cancel if requested
                        try { cancellationToken.ThrowIfCancellationRequested(); }
                        catch (OperationCanceledException) { exceptionThrownInAwait = true; throw new OperationCanceledException(); }

                        // Open docx files in Word, clean up, and convert
                        try
                        {
                            doc = app.Documents.Open(FileName: fileSelected.LocalFullFilename, ReadOnly: true);
                        }
                        catch
                        {
                            System.Diagnostics.Debug.WriteLine($"{fileSelected.FullName} failed to open");
                        }

                        // cancel if requested
                        try { cancellationToken.ThrowIfCancellationRequested(); }
                        catch (OperationCanceledException) { exceptionThrownInAwait = true; throw new OperationCanceledException(); }

                        // print to pdf, reporting progress
                        var newPrnConvertedFromWord = string.Empty;
                        newPrnConvertedFromWord     = MicrosoftWordStaticClass.PrintToFileForProof(app, doc.FullName);

                        // halt process here: wait for COM background status to end
                        while (app.BackgroundPrintingStatus > 0 && app.BackgroundSavingStatus > 0)
                        {
                            try { cancellationToken.ThrowIfCancellationRequested(); }
                            catch (OperationCanceledException) { exceptionThrownInAwait = true; throw new OperationCanceledException(); }
                        }

                        // add to files_printed list
                        filesPrintedToPrnSuccessfully.Add(
                            new FilesPrintedToPrnSuccessfully
                        {
                            CockleFile  = fileSelected,
                            PrnFilename = newPrnConvertedFromWord,
                            Filetype    = fileSelected.FileType
                        });

                        // make sure file exists before closing
                        while (!System.IO.File.Exists(newPrnConvertedFromWord))
                        {
                            System.Diagnostics.Debug.WriteLine($"Waiting to print to pdf: {newPrnConvertedFromWord}");
                            // cancel if requested
                            try { cancellationToken.ThrowIfCancellationRequested(); }
                            catch (OperationCanceledException) { exceptionThrownInAwait = true; throw new OperationCanceledException(); }
                        }
                        // close document & delete temp file
                        doc.Close(SaveChanges: Word.WdSaveOptions.wdDoNotSaveChanges);
                        // increment counter
                        i++;
                    }// end for loop to convert each files
                }
                //catch (OperationCanceledException ex) { }
                catch { }
                finally
                {
                    app.ActivePrinter = current_printer;
                    app?.Quit();
                }
                if (exceptionThrownInAwait)
                {
                    // close app
                    app.ActivePrinter = current_printer;
                    app?.Quit();
                    // try to clean folder
                    filesPrintedToPrnSuccessfully.ForEach(f =>
                    {
                        if (System.IO.File.Exists(f.PrnFilename))
                        {
                            System.IO.File.Delete(f.PrnFilename);
                        }
                    });
                    return(null);
                }

                // block until all files exist
                while (filesPrintedToPrnSuccessfully?.Count != filesSelectedForConversion.Count)
                {
                    ;
                }

                // NOW, CHANGE NAMES OF FILES TO PS, AND USE GHOSTSCRIPT TO CONVERT TO PDF

                var filesPrintedSuccessfully = new List <FilePrintedSuccessfully>();

                foreach (var f in filesPrintedToPrnSuccessfully)
                {
                    var psFileName  = f.PrnFilename.Replace(".prn", ".ps");
                    var pdfFileName = f.PrnFilename.Replace(".prn", ".pdf");

                    System.IO.File.Move(f.PrnFilename, psFileName);
                    // block until successful
                    while (!System.IO.File.Exists(psFileName)) /*waiting for IO*/ } {
                    using (var processor = new Ghostscript.NET.Processor.GhostscriptProcessor())
                    {
                        List <string> switches = new List <string>();
                        switches.Add("-empty");
                        switches.Add("-dQUIET");
                        switches.Add("-dSAFER");
                        switches.Add("-dBATCH");
                        switches.Add("-dNOPAUSE");
                        switches.Add("-dNOPROMPT");
                        switches.Add("-sDEVICE=pdfwrite");
                        switches.Add("-o" + pdfFileName);
                        switches.Add("-q");
                        switches.Add("-f");
                        switches.Add(psFileName);

                        processor.StartProcessing(switches.ToArray(), null);
                    }

                    // make sure successful
                    while (!System.IO.File.Exists(pdfFileName)) /*waiting for IO*/ } {
                    filesPrintedSuccessfully.Add(
                        new FilePrintedSuccessfully
                    {
                        CockleFile    = f.CockleFile,
                        TempWordFile  = string.Empty,
                        PdfFilename   = pdfFileName,
                        Filetype      = SourceFileTypeEnum.Unrecognized, // may have to adjust type here
                        LengthOfCover = null
                    });

                    System.IO.File.Delete(psFileName);
            }

                                                       #region POINT OF NO RETURN IN CONVERSION
                                                       // convert files to CockleFilePdf
                                                       var cockleFilePdfsPrintedSuccessfully = new List <CockleFilePdf>();
                                                       foreach (var _f in filesPrintedSuccessfully)
                {
                    if (_f.LengthOfCover == null && _f.CockleFile == null)
                    {
                        cockleFilePdfsPrintedSuccessfully.Add(new CockleFilePdf(_f.PdfFilename, _f.Filetype));
                    }
                    else
                    {
                        cockleFilePdfsPrintedSuccessfully.Add(
                            new CockleFilePdf(_f.CockleFile, _f.PdfFilename, _f.LengthOfCover));
                    }
                }

                                                       // test whether all converted files have same ticket
                                                       bool allConvertedFilesSameTicket = cockleFilePdfsPrintedSuccessfully
                                                                                          .TrueForAll(f => f.TicketNumber ==
                                                                                                      cockleFilePdfsPrintedSuccessfully.First().TicketNumber);

                                                                                                                                                             // move files to unique folder
                                                       if ((allConvertedFilesSameTicket || IsSingleTicket) && cockleFilePdfsPrintedSuccessfully.Count() > 0) // PROBLEM HERE !!!
                {
                    var firstFile    = cockleFilePdfsPrintedSuccessfully.First();
                    string timeStamp = string.Format("({0} {1}, {2}, {3})", DateTime.Now.ToString("MMM") /*Oct*/,
                                                     DateTime.Now.ToString("dd") /*09*/, DateTime.Now.ToString("yyy") /*2015*/,
                                                     DateTime.Now.ToString("T").ToLower() /*10:58:44 AM*/ /*, len_text*/)
                                       .Replace(':', ' ');
                    var folderName      = $"{firstFile.TicketNumber} {firstFile.Attorney} {timeStamp}";
                    var scratchLocation = @"c:\scratch";

                    var newFolder = System.IO.Directory.CreateDirectory(System.IO.Path.Combine(scratchLocation, folderName));

                    foreach (var f in cockleFilePdfsPrintedSuccessfully)
                    {
                        try
                        {
                            var new_filename = System.IO.Path.Combine(newFolder.FullName, f.Filename);
                            System.IO.File.Move(f.FullName, new_filename);

                            // associate new location with CockleFilePdf
                            f.FullName = new_filename;
                        }
                        catch (Exception ex)
                        {
                            System.Diagnostics.Debug.WriteLine(ex);
                        }
                    }
                }

                                                       // set destination property
                                                       DestinationFolderConvertedFiles = System.IO.Path.GetDirectoryName(cockleFilePdfsPrintedSuccessfully.First().FullName);

                                                       // set ranks of pdfs before returning
                                                       setCockleFilePdfRanks(cockleFilePdfsPrintedSuccessfully);


                                                       if (convertAll)
                {
                    // 1. create imposed pdf files
                    Models.Imposition.ImposeFullConvertedTicket createImposedPdfiTextSharp = null;
                    try
                    {
                        var len_of_cover = cockleFilePdfsPrintedSuccessfully
                                           .Where(f => f.FileType == SourceFileTypeEnum.Cover).First().CoverLength ?? -1;

                        // prepare files for imposition
                        //var cockleFilePdfsPrintedSuccessfullyToCombine = cockleFilePdfsPrintedSuccessfully.ToList();
                        //cockleFilePdfsPrintedSuccessfullyToCombine.Remove(mergedCockleFile);

                        createImposedPdfiTextSharp =
                            new Models.Imposition.ImposeFullConvertedTicket(
                                DestinationFolderConvertedFiles,
                                cockleFilePdfsPrintedSuccessfully.ToList(),
                                len_of_cover,
                                TypeOfBindEnum.ProgramDecidesByPageCount);

                        // add imposed files to list of cocklefilepdf files
                        if (createImposedPdfiTextSharp.ImposedFilesCreated.All(f => System.IO.File.Exists(f.FullName)))
                        {
                            createImposedPdfiTextSharp.ImposedFilesCreated.ForEach(f =>
                            {
                                cockleFilePdfsPrintedSuccessfully.Add(
                                    new CockleFilePdf(
                                        f.FullName,
                                        filesSelectedForConversion.First().Attorney,
                                        filesSelectedForConversion.First().TicketNumber,
                                        SourceFileTypeEnum.Imposed_Cover_and_Brief,
                                        "pdf",
                                        null));
                            });
                        }
                    }
                    catch (Exception ex)
                    {
                        if (null != createImposedPdfiTextSharp.ImposedFilesCreated &&
                            createImposedPdfiTextSharp.ImposedFilesCreated.Count > 0)
                        {
                            createImposedPdfiTextSharp.ImposedFilesCreated.ForEach(x => System.IO.File.Delete(x.FullName));

                            foreach (var imposed_file in createImposedPdfiTextSharp.ImposedFilesCreated)
                            {
                                if (cockleFilePdfsPrintedSuccessfully.Contains(imposed_file))
                                {
                                    cockleFilePdfsPrintedSuccessfully.Remove(imposed_file);
                                }
                            }
                        }
                        System.Diagnostics.Debug.WriteLine(ex.Message);
                    }

                    // 2. combine files into single pdf
                    Models.MergePdf.CreateMergedPDFAcrobat createMergedPdfAcrobat = null;
                    CockleFilePdf mergedCockleFile = null;
                    try
                    {
                        createMergedPdfAcrobat = new Models.MergePdf.CreateMergedPDFAcrobat(
                            cockleFilePdfsPrintedSuccessfully, Models.MergePdf.TypeOfCombinedPdf.All, centerPdf: true);

                        // add combined file to list of cocklefilepdf files
                        if (System.IO.File.Exists(createMergedPdfAcrobat.CombinedPdfFilename))
                        {
                            mergedCockleFile =
                                new CockleFilePdf(
                                    createMergedPdfAcrobat.CombinedPdfFilename,
                                    filesSelectedForConversion.First().Attorney,
                                    filesSelectedForConversion.First().TicketNumber,
                                    SourceFileTypeEnum.Combined_Pdf,
                                    "pdf",
                                    null);
                            cockleFilePdfsPrintedSuccessfully.Add(mergedCockleFile);
                        }
                    }
                    catch (Exception ex)
                    {
                        if (null != createMergedPdfAcrobat.CombinedPdfFilename &&
                            System.IO.File.Exists(createMergedPdfAcrobat.CombinedPdfFilename))
                        {
                            System.IO.File.Delete(createMergedPdfAcrobat.CombinedPdfFilename);
                        }
                        if (null != mergedCockleFile &&
                            cockleFilePdfsPrintedSuccessfully.Contains(mergedCockleFile))
                        {
                            cockleFilePdfsPrintedSuccessfully.Remove(mergedCockleFile);
                        }
                        System.Diagnostics.Debug.WriteLine(ex.Message);
                    }
                }
                                                       #endregion


                                                       return(cockleFilePdfsPrintedSuccessfully);
});

            if (exceptionThrownInAwait)
            {
                throw new OperationCanceledException();
            }
            return(filesToReturnFromTask);
        }
Beispiel #2
0
        /// <summary>
        /// This is the core method of the program.
        /// With a single ticket number, it collects Word files from
        /// the Current directory (H:\ locally mapped or CLBDC02\Current\ on server)
        /// and converts the files to PDFs in the user's scratch folder.
        ///
        /// In the process, it also combines the ticket in several ways:
        /// first, in total, but also as distinct PDF/A brief and appendix.
        ///
        /// It also imposes the files for printing.
        /// </summary>
        /// <param name="progress"></param>
        /// <param name="cancellationToken"></param>
        /// <param name="latestFiles"></param>
        /// <returns></returns>
        private async Task <List <CockleFilePdf> > executePdfConversion(
            IProgress <string> progress,
            CancellationToken cancellationToken,
            List <CockleFile> latestFiles)
        {
            bool exceptionThrownInAwait = false; // tracks excptn in await

            List <CockleFile> filesSelectedForConversion;
            bool convertAll = false;

            // get files from grid if null
            if (null == latestFiles)
            {
                filesSelectedForConversion = SelectedFiles;
            }
            else
            {
                convertAll = true; filesSelectedForConversion = latestFiles;
            }

            if (filesSelectedForConversion.Count < 1)
            {
                throw new Exception();
            }

            // Convert files to Pdf: Open Word and Print to Pdf
            var filesToReturnFromTask = await Task.Run(() =>
            {
                OpenMSWordAndCreatePdfs openMSWordAndCreatePdfs         = null;
                List <FilePrintedSuccessfully> filesPrintedSuccessfully = null;
                try
                {
                    openMSWordAndCreatePdfs = new OpenMSWordAndCreatePdfs(
                        filesSelectedForConversion,
                        UpdateLabel,
                        progress,
                        cancellationToken,
                        exceptionThrownInAwait);
                    cancellationToken        = openMSWordAndCreatePdfs.CancellationToken;
                    exceptionThrownInAwait   = openMSWordAndCreatePdfs.ExceptionThrownInAwait;
                    filesPrintedSuccessfully = openMSWordAndCreatePdfs.FilesPrintedSuccessfully;
                }
                catch (Exception ex)
                {
                    if (null != openMSWordAndCreatePdfs)
                    {
                        cancellationToken        = openMSWordAndCreatePdfs.CancellationToken;
                        exceptionThrownInAwait   = openMSWordAndCreatePdfs.ExceptionThrownInAwait;
                        filesPrintedSuccessfully = openMSWordAndCreatePdfs.FilesPrintedSuccessfully;
                    }
                    System.Diagnostics.Debug.WriteLine(ex);
                    return(null); // should this be here?
                }

                #region POINT OF NO RETURN IN CONVERSION
                // save each new pdf as CockleFilePdf object
                var cockleFilePdfsPrintedSuccessfully = new List <CockleFilePdf>();
                foreach (var _f in filesPrintedSuccessfully)
                {
                    if (_f.LengthOfCover == null && _f.CockleFile == null)
                    {
                        cockleFilePdfsPrintedSuccessfully.Add(new CockleFilePdf(_f.PdfFilename, _f.Filetype));
                    }
                    else
                    {
                        cockleFilePdfsPrintedSuccessfully.Add(
                            new CockleFilePdf(_f.CockleFile, _f.PdfFilename, _f.LengthOfCover));
                    }
                }

                // test whether all converted files have same ticket
                bool allConvertedFilesSameTicket = cockleFilePdfsPrintedSuccessfully
                                                   .TrueForAll(f => f.TicketNumber ==
                                                               cockleFilePdfsPrintedSuccessfully.First().TicketNumber);

                // move files to unique folder
                if ((allConvertedFilesSameTicket || IsSingleTicket) && cockleFilePdfsPrintedSuccessfully.Count() > 0) // PROBLEM HERE !!!
                {
                    DestinationFolderConvertedFiles = movePdfFilesToNewScratchFolder(cockleFilePdfsPrintedSuccessfully);
                }

                // set ranks of pdfs before returning
                setCockleFilePdfRanks(cockleFilePdfsPrintedSuccessfully);

                if (convertAll)
                {
                    // 1. create imposed pdf files
                    Models.Imposition.ImposeFullConvertedTicket createImposedPdfiTextSharp = null;
                    int?len_of_cover = null;
                    try
                    {
                        len_of_cover = cockleFilePdfsPrintedSuccessfully
                                       .Find(x => x.FileType == SourceFileTypeEnum.Cover)
                                       ?.CoverLength ?? null;

                        createImposedPdfiTextSharp =
                            new Models.Imposition.ImposeFullConvertedTicket(
                                DestinationFolderConvertedFiles,
                                cockleFilePdfsPrintedSuccessfully.ToList(),
                                len_of_cover != null ? (int)len_of_cover : -1,
                                TypeOfBindEnum.ProgramDecidesByPageCount);

                        // add imposed files to list of cocklefilepdf files
                        if (createImposedPdfiTextSharp.ImposedFilesCreated.All(x => System.IO.File.Exists(x.FullName)))
                        {
                            createImposedPdfiTextSharp.ImposedFilesCreated.ForEach(f =>
                            {
                                cockleFilePdfsPrintedSuccessfully.Add(
                                    new CockleFilePdf(
                                        f.FullName,
                                        filesSelectedForConversion.First().Attorney,
                                        filesSelectedForConversion.First().TicketNumber,
                                        SourceFileTypeEnum.Imposed_Cover_and_Brief,
                                        "pdf",
                                        null));
                            });
                        }
                    }
                    catch (Exception ex)
                    {
                        if (null != createImposedPdfiTextSharp &&
                            null != createImposedPdfiTextSharp.ImposedFilesCreated &&
                            createImposedPdfiTextSharp.ImposedFilesCreated.Count > 0)
                        {
                            createImposedPdfiTextSharp.ImposedFilesCreated.ForEach(x => System.IO.File.Delete(x.FullName));

                            foreach (var imposed_file in createImposedPdfiTextSharp.ImposedFilesCreated)
                            {
                                if (cockleFilePdfsPrintedSuccessfully.Contains(imposed_file))
                                {
                                    cockleFilePdfsPrintedSuccessfully.Remove(imposed_file);
                                }
                            }
                        }
                        System.Diagnostics.Debug.WriteLine(ex.Message);
                    }

                    // 2. combine files into single pdf & create combined files of brief & appendix
                    Models.MergePdf.CreateMergedPDFAcrobat createMergedPdfAcrobat = null;
                    CockleFilePdf mergedCockleFile = null;
                    try
                    {
                        createMergedPdfAcrobat = new Models.MergePdf.CreateMergedPDFAcrobat(
                            cockleFilePdfsPrintedSuccessfully, typeOfCombinedPdf: Models.MergePdf.TypeOfCombinedPdf.All, centerPdf: true);

                        // add combined file to list of cocklefilepdf files
                        if (System.IO.File.Exists(createMergedPdfAcrobat.CombinedPdfFilename))
                        {
                            mergedCockleFile =
                                new CockleFilePdf(
                                    createMergedPdfAcrobat.CombinedPdfFilename,
                                    filesSelectedForConversion.First().Attorney,
                                    filesSelectedForConversion.First().TicketNumber,
                                    SourceFileTypeEnum.Combined_Pdf,
                                    "pdf",
                                    null);
                            cockleFilePdfsPrintedSuccessfully.Add(mergedCockleFile);
                        }
                    }
                    catch (Exception ex)
                    {
                        if (null != createMergedPdfAcrobat &&
                            !string.IsNullOrEmpty(createMergedPdfAcrobat.CombinedPdfFilename) &&
                            System.IO.File.Exists(createMergedPdfAcrobat.CombinedPdfFilename))
                        {
                            System.IO.File.Delete(createMergedPdfAcrobat.CombinedPdfFilename);
                        }
                        if (null != mergedCockleFile &&
                            cockleFilePdfsPrintedSuccessfully.Contains(mergedCockleFile))
                        {
                            cockleFilePdfsPrintedSuccessfully.Remove(mergedCockleFile);
                        }
                        System.Diagnostics.Debug.WriteLine(ex.Message);
                    }
                    Models.MergePdf.CreateMergedPDFAcrobat createMergedBrief_PdfAcrobat = null;
                    CockleFilePdf mergedCockleFile_Brief = null;
                    Models.MergePdf.CreateMergedPDFAcrobat createMergedAppendix_PdfAcrobat = null;
                    CockleFilePdf mergedCockleFile_Appendix = null;
                    try
                    {
                        var app_files = cockleFilePdfsPrintedSuccessfully
                                        .Where(x => x.FileType == SourceFileTypeEnum.App_Index ||
                                               x.FileType == SourceFileTypeEnum.App_File ||
                                               x.FileType == SourceFileTypeEnum.App_Foldout ||
                                               x.FileType == SourceFileTypeEnum.App_ZFold)
                                        .ToList();


                        // if no appendix files, skip creating separate combined pdfs
                        if (app_files.Count > 0)
                        {
                            createMergedAppendix_PdfAcrobat = new Models.MergePdf.CreateMergedPDFAcrobat(
                                app_files, Models.MergePdf.TypeOfCombinedPdf.Appendix, centerPdf: true);
                            // add combined file to list of cocklefilepdf files
                            if (System.IO.File.Exists(createMergedAppendix_PdfAcrobat.CombinedPdfFilename))
                            {
                                mergedCockleFile_Appendix =
                                    new CockleFilePdf(
                                        createMergedAppendix_PdfAcrobat.CombinedPdfFilename,
                                        filesSelectedForConversion.First().Attorney,
                                        filesSelectedForConversion.First().TicketNumber,
                                        SourceFileTypeEnum.Combined_Pdf,
                                        "pdf",
                                        null);
                                cockleFilePdfsPrintedSuccessfully.Add(mergedCockleFile_Appendix);
                            }

                            var brief_files = cockleFilePdfsPrintedSuccessfully
                                              .Where(x => x.FileType == SourceFileTypeEnum.Cover ||
                                                     x.FileType == SourceFileTypeEnum.InsideCv ||
                                                     x.FileType == SourceFileTypeEnum.Motion ||
                                                     x.FileType == SourceFileTypeEnum.Index ||
                                                     x.FileType == SourceFileTypeEnum.Brief ||
                                                     x.FileType == SourceFileTypeEnum.Brief_Foldout ||
                                                     x.FileType == SourceFileTypeEnum.Brief_ZFold)
                                              .ToList();

                            createMergedBrief_PdfAcrobat = new Models.MergePdf.CreateMergedPDFAcrobat(
                                brief_files, Models.MergePdf.TypeOfCombinedPdf.Brief, centerPdf: true);
                            // add combined file to list of cocklefilepdf files
                            if (System.IO.File.Exists(createMergedBrief_PdfAcrobat.CombinedPdfFilename))
                            {
                                mergedCockleFile_Brief =
                                    new CockleFilePdf(
                                        createMergedBrief_PdfAcrobat.CombinedPdfFilename,
                                        filesSelectedForConversion.First().Attorney,
                                        filesSelectedForConversion.First().TicketNumber,
                                        SourceFileTypeEnum.Combined_Pdf,
                                        "pdf",
                                        null);
                                cockleFilePdfsPrintedSuccessfully.Add(mergedCockleFile_Brief);
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        if (null != createMergedBrief_PdfAcrobat &&
                            !string.IsNullOrEmpty(createMergedBrief_PdfAcrobat.CombinedPdfFilename) &&
                            System.IO.File.Exists(createMergedBrief_PdfAcrobat.CombinedPdfFilename))
                        {
                            System.IO.File.Delete(createMergedBrief_PdfAcrobat.CombinedPdfFilename);
                        }
                        if (null != createMergedAppendix_PdfAcrobat &&
                            !string.IsNullOrEmpty(createMergedAppendix_PdfAcrobat.CombinedPdfFilename) &&
                            System.IO.File.Exists(createMergedAppendix_PdfAcrobat.CombinedPdfFilename))
                        {
                            System.IO.File.Delete(createMergedAppendix_PdfAcrobat.CombinedPdfFilename);
                        }
                        if (null != mergedCockleFile_Brief &&
                            cockleFilePdfsPrintedSuccessfully.Contains(mergedCockleFile_Brief))
                        {
                            cockleFilePdfsPrintedSuccessfully.Remove(mergedCockleFile_Brief);
                        }
                        if (null != mergedCockleFile_Appendix &&
                            cockleFilePdfsPrintedSuccessfully.Contains(mergedCockleFile_Appendix))
                        {
                            cockleFilePdfsPrintedSuccessfully.Remove(mergedCockleFile_Appendix);
                        }
                        System.Diagnostics.Debug.WriteLine(ex.Message);
                    }
                }
                #endregion

                return(cockleFilePdfsPrintedSuccessfully);
            });

            if (exceptionThrownInAwait)
            {
                throw new OperationCanceledException();
            }
            return(filesToReturnFromTask);
        }
        public static async Task <List <CockleFilePdf> > CreatePdf(
            IProgress <string> _progress,
            System.Threading.CancellationToken _cancellationToken,
            List <CockleFile> _latestFiles,
            List <CockleFile> _selectedFiles,
            bool _isSingleTicket,
            string _destinationFolderConvertedFiles)
        {
            var pdfCreationModel = new PdfCreationModel(
                _progress,
                _cancellationToken,
                _latestFiles,
                _selectedFiles,
                _isSingleTicket,
                _destinationFolderConvertedFiles);

            bool exceptionThrownInAwait = false; // tracks excptn in await

            List <CockleFile> filesSelectedForConversion;
            bool convertAll = false;

            // get files from grid if null
            if (null == pdfCreationModel.latestFiles)
            {
                filesSelectedForConversion = pdfCreationModel.selectedFiles;
            }
            else
            {
                convertAll = true; filesSelectedForConversion = pdfCreationModel.latestFiles;
            }

            if (filesSelectedForConversion.Count < 1)
            {
                throw new Exception();
            }


            // begin AWAIT
            var filesToReturnFromTask = await System.Threading.Tasks.Task.Run(() =>
            {
                // I think this line can go, as can the OpenMSWordAndCreatePdfs class
                //var files = new OpenMSWordAndCreatePdfs().FilesPrintedSuccessfully;

                //instantiate Word Application & Document
                Word.Application app = new Word.Application();
                app.Visible          = true;
                Word.Document doc    = null;

                //get original printer & prepare to create PDF
                var current_printer   = app.ActivePrinter;
                var adobe_pdf_printer = "Adobe PDF";
                app.ActivePrinter     = adobe_pdf_printer;

                // collection of special class to track files
                var filesPrintedSuccessfully = new List <FilePrintedSuccessfully>();
                //the Word file saved to scratch
                try
                {
                    // counter to track files printed
                    int i = 0;

                    // loop through files
                    foreach (CockleFile fileSelected in filesSelectedForConversion)
                    {
                        // cancel if requested
                        try { pdfCreationModel.cancellationToken.ThrowIfCancellationRequested(); }
                        catch (OperationCanceledException) { exceptionThrownInAwait = true; throw new OperationCanceledException(); }

                        // catch pdf files saved to Current
                        if (System.IO.Path.GetExtension(fileSelected.FullName).Equals(".pdf"))
                        {
                            // don't try to open, just save to scratch and add to list
                            var pdfFileInCurrent = System.IO.Path.Combine(@"C:\scratch", System.IO.Path.GetFileName(fileSelected.FullName));
                            System.IO.File.Copy(fileSelected.FullName, pdfFileInCurrent);
                            // here, just a string & no cover length
                            filesPrintedSuccessfully.Add(
                                new FilePrintedSuccessfully
                            {
                                CockleFile    = null,
                                TempWordFile  = null,
                                PdfFilename   = pdfFileInCurrent,
                                Filetype      = SourceFileTypeEnum.Camera_Ready, // may have to adjust type here
                                LengthOfCover = null
                            });
                            continue;
                        }

                        // Open docx files in Word, clean up, and convert
                        try
                        {
                            doc = app.Documents.Open(FileName: fileSelected.FullName, ReadOnly: true);
                        }
                        catch
                        {
                            System.Diagnostics.Debug.WriteLine($"{fileSelected.FullName} failed to open");
                        }

                        // save to c:\scratch (overwriting existing files)
                        var tempFileSavedToScratch = System.IO.Path.Combine(@"c:\scratch", System.IO.Path.GetFileName(fileSelected.FullName));
                        if (System.IO.File.Exists(tempFileSavedToScratch))
                        {
                            System.IO.File.Delete(tempFileSavedToScratch);
                        }
                        doc.SaveAs2(FileName: tempFileSavedToScratch);

                        // ctrl shift f9 (had problem with links in index: removes links from main story)
                        Word.Range r = doc.StoryRanges[Word.WdStoryType.wdMainTextStory];
                        r.Fields.Unlink();

                        // delete footer
                        MicrosoftWordStaticClass.WordDoc_DeleteFooters(fileSelected, doc);

                        // line to capture length of cover
                        int?lengthOfCover = null;
                        if (fileSelected.FileType == SourceFileTypeEnum.Cover)
                        {
                            lengthOfCover = MicrosoftWordStaticClass.CaptureCoverLength(doc);
                        }

                        // cancel if requested
                        try { pdfCreationModel.cancellationToken.ThrowIfCancellationRequested(); }
                        catch (OperationCanceledException) { exceptionThrownInAwait = true; throw new OperationCanceledException(); }

                        // print to pdf, reporting progress
                        var newPdfConvertedFromWord = string.Empty;
                        newPdfConvertedFromWord     = MicrosoftWordStaticClass.PrintToFile(app, doc.FullName);
                        // halt process here: wait for COM background status to end
                        while (app.BackgroundPrintingStatus > 0)
                        {
                            System.Diagnostics.Debug.WriteLine($"app.BackgroundPrintingStatus is {app.BackgroundPrintingStatus}");
                            // cancel if requested
                            try { pdfCreationModel.cancellationToken.ThrowIfCancellationRequested(); }
                            catch (OperationCanceledException) { exceptionThrownInAwait = true; throw new OperationCanceledException(); }
                        }
                        while (app.BackgroundSavingStatus > 0)
                        {
                            System.Diagnostics.Debug.WriteLine($"app.BackgroundSavingStatus is {app.BackgroundSavingStatus}");
                            // cancel if requested
                            try { pdfCreationModel.cancellationToken.ThrowIfCancellationRequested(); }
                            catch (OperationCanceledException) { exceptionThrownInAwait = true; throw new OperationCanceledException(); }
                        }

                        // add to files_printed list
                        filesPrintedSuccessfully.Add(
                            new FilePrintedSuccessfully
                        {
                            CockleFile    = fileSelected,
                            TempWordFile  = tempFileSavedToScratch,
                            PdfFilename   = newPdfConvertedFromWord,
                            Filetype      = SourceFileTypeEnum.Unrecognized,
                            LengthOfCover = lengthOfCover
                        });

                        // report to ui

                        // make sure file exists before closing
                        while (!System.IO.File.Exists(newPdfConvertedFromWord))
                        {
                            System.Diagnostics.Debug.WriteLine($"Waiting to print to pdf: {newPdfConvertedFromWord}");
                            // cancel if requested
                            try { pdfCreationModel.cancellationToken.ThrowIfCancellationRequested(); }
                            catch (OperationCanceledException) { exceptionThrownInAwait = true; throw new OperationCanceledException(); }
                        }
                        // close document & delete temp file
                        doc.Close(SaveChanges: Word.WdSaveOptions.wdDoNotSaveChanges);
                        System.IO.File.Delete(tempFileSavedToScratch);
                        // increment counter
                        i++;
                    }// end for loop to convert each files
                }
                //catch (OperationCanceledException ex) { }
                catch { }
                finally
                {
                    app.ActivePrinter = current_printer;
                    app?.Quit();
                }
                if (exceptionThrownInAwait)
                {
                    // close app
                    app.ActivePrinter = current_printer;
                    app?.Quit();
                    // try to clean folder
                    filesPrintedSuccessfully.ForEach(f =>
                    {
                        if (System.IO.File.Exists(f.PdfFilename))
                        {
                            System.IO.File.Delete(f.PdfFilename);
                        }
                        if (System.IO.File.Exists(f.TempWordFile))
                        {
                            System.IO.File.Delete(f.TempWordFile);
                        }
                    });
                    return(null);
                }

                // block until all files exist
                while (filesPrintedSuccessfully?.Count != filesSelectedForConversion.Count)
                {
                    ;
                }

                #region POINT OF NO RETURN IN CONVERSION
                // convert files to CockleFilePdf
                var cockleFilePdfsPrintedSuccessfully = new List <CockleFilePdf>();
                foreach (var _f in filesPrintedSuccessfully)
                {
                    if (_f.LengthOfCover == null && _f.CockleFile == null)
                    {
                        cockleFilePdfsPrintedSuccessfully.Add(new CockleFilePdf(_f.PdfFilename, _f.Filetype));
                    }
                    else
                    {
                        cockleFilePdfsPrintedSuccessfully.Add(
                            new CockleFilePdf(_f.CockleFile, _f.PdfFilename, _f.LengthOfCover));
                    }
                }

                // test whether all converted files have same ticket
                bool allConvertedFilesSameTicket = cockleFilePdfsPrintedSuccessfully
                                                   .TrueForAll(f => f.TicketNumber ==
                                                               cockleFilePdfsPrintedSuccessfully.First().TicketNumber);

                // move files to unique folder
                if ((allConvertedFilesSameTicket || pdfCreationModel.isSingleTicket) &&
                    cockleFilePdfsPrintedSuccessfully.Count() > 0)    // PROBLEM HERE !!!
                {
                    var firstFile    = cockleFilePdfsPrintedSuccessfully.First();
                    string timeStamp = string.Format("({0} {1}, {2}, {3})", DateTime.Now.ToString("MMM") /*Oct*/,
                                                     DateTime.Now.ToString("dd") /*09*/, DateTime.Now.ToString("yyy") /*2015*/,
                                                     DateTime.Now.ToString("T").ToLower() /*10:58:44 AM*/ /*, len_text*/)
                                       .Replace(':', ' ');
                    var folderName      = $"{firstFile.TicketNumber} {firstFile.Attorney} {timeStamp}";
                    var scratchLocation = @"c:\scratch";

                    var newFolder = System.IO.Directory.CreateDirectory(System.IO.Path.Combine(scratchLocation, folderName));

                    foreach (var f in cockleFilePdfsPrintedSuccessfully)
                    {
                        try
                        {
                            var new_filename = System.IO.Path.Combine(newFolder.FullName, f.Filename);
                            System.IO.File.Move(f.FullName, new_filename);

                            // associate new location with CockleFilePdf
                            f.FullName = new_filename;
                        }
                        catch (Exception ex)
                        {
                            System.Diagnostics.Debug.WriteLine(ex);
                        }
                    }
                }

                // set destination property
                pdfCreationModel.destinationFolderConvertedFiles = System.IO.Path.GetDirectoryName(cockleFilePdfsPrintedSuccessfully.First().FullName);

                // set ranks of pdfs before returning
                pdfCreationModel.setCockleFilePdfRanks(cockleFilePdfsPrintedSuccessfully);

                // combine files into single pdf
                if (convertAll)
                {
                    var createMergedPdfAcrobat = new Models.MergePdf.CreateMergedPDFAcrobat(cockleFilePdfsPrintedSuccessfully);

                    CockleFilePdf mergedCockleFile = null;

                    // add combined file to list of cocklefilepdf files
                    if (System.IO.File.Exists(createMergedPdfAcrobat.CombinedPdfFilename))
                    {
                        mergedCockleFile =
                            new CockleFilePdf(
                                createMergedPdfAcrobat.CombinedPdfFilename,
                                filesSelectedForConversion.First().Attorney,
                                filesSelectedForConversion.First().TicketNumber,
                                SourceFileTypeEnum.Combined_Pdf,
                                "pdf",
                                null);
                        cockleFilePdfsPrintedSuccessfully.Add(mergedCockleFile);
                    }

                    int len_of_cover = -1;
                    if (cockleFilePdfsPrintedSuccessfully.Any(f => f.FileType == SourceFileTypeEnum.Cover))
                    {
                        len_of_cover = cockleFilePdfsPrintedSuccessfully.Where(
                            f => f.FileType == SourceFileTypeEnum.Cover).FirstOrDefault().CoverLength ?? -1;
                    }

                    // remove combined pdf file
                    var cockleFilePdfsPrintedSuccessfullyMinusCombinedFile = cockleFilePdfsPrintedSuccessfully.ToList();
                    cockleFilePdfsPrintedSuccessfullyMinusCombinedFile.Remove(mergedCockleFile);

                    var createImposedPdfiTextSharp =
                        new Models.Imposition.ImposeFullConvertedTicket(
                            pdfCreationModel.destinationFolderConvertedFiles,
                            cockleFilePdfsPrintedSuccessfullyMinusCombinedFile,
                            len_of_cover,
                            TypeOfBindEnum.ProgramDecidesByPageCount);

                    // add imposed files to list of cocklefilepdf files
                    if (createImposedPdfiTextSharp.ImposedFilesCreated.All(f => System.IO.File.Exists(f.FullName)))
                    {
                        createImposedPdfiTextSharp.ImposedFilesCreated.ForEach(f =>
                        {
                            cockleFilePdfsPrintedSuccessfully.Add(
                                new CockleFilePdf(
                                    f.FullName,
                                    filesSelectedForConversion.First().Attorney,
                                    filesSelectedForConversion.First().TicketNumber,
                                    SourceFileTypeEnum.Imposed_Cover_and_Brief,
                                    "pdf",
                                    null));
                        });
                    }
                }
                #endregion


                return(cockleFilePdfsPrintedSuccessfully);
            });

            if (exceptionThrownInAwait)
            {
                throw new OperationCanceledException();
            }
            return(filesToReturnFromTask);
        }