public static bool CombineCoverAndInsideCover(List <CockleFilePdf> files, string src)
        {
            try
            {
                using (var stream = new System.IO.FileStream(src, System.IO.FileMode.Create))
                {
                    // initiate iTextSharp processes
                    iTextSharp.text.Document    pdfdoc  = new iTextSharp.text.Document(iTextSharp.text.PageSize.LETTER);
                    iTextSharp.text.pdf.PdfCopy pdfcopy = new iTextSharp.text.pdf.PdfCopy(pdfdoc, stream);
                    pdfdoc.Open();

                    // merge pdfs in folder
                    CockleFilePdf f;
                    for (int i = 0; i < files.Count; i++)
                    {
                        f = files[i];
                        // read file
                        iTextSharp.text.pdf.PdfReader reader = new iTextSharp.text.pdf.PdfReader(f.FullName);
                        int filePageCount = reader.NumberOfPages;
                        pdfcopy.AddDocument(new iTextSharp.text.pdf.PdfReader(reader));
                    }
                    pdfcopy.Close();
                    pdfdoc.CloseDocument();
                }
                return(true);
            }
            catch (Exception excpt) { System.Diagnostics.Debug.WriteLine(excpt); return(false); }
        }
        private static void MergeFiles(string destinationFile, string ExportPath)
        {
            #region new code

            using (FileStream stream = new FileStream(destinationFile, FileMode.Create))
            {
                iTextSharp.text.Document    pdfDoc = new iTextSharp.text.Document(iTextSharp.text.PageSize.A4);
                iTextSharp.text.pdf.PdfCopy pdf    = new iTextSharp.text.pdf.PdfCopy(pdfDoc, stream);
                pdfDoc.Open();
                var files = Directory.GetFiles(ExportPath);

                int i = 1;
                foreach (string file in files)
                {
                    // MemoryStream content= new MemoryStream(
                    var reader = new iTextSharp.text.pdf.PdfReader(file);
                    pdf.AddDocument(reader);
                    i++;
                    reader.Close();
                }

                if (pdfDoc != null)
                {
                    pdfDoc.Close();
                }
            }

            #endregion
        }
        private void MergeFiles(string destinationFile, string ExportPath)
        {
            #region old code
            //        if (System.IO.File.Exists(destinationFile))
            //            System.IO.File.Delete(destinationFile);

            //        string[] sSrcFile;
            //        sSrcFile = new string[sourceFiles.Count()];

            //        string[] arr = new string[sourceFiles.Count()];
            //        for (int i = 0; i <= sourceFiles.Length - 1; i++)
            //        {
            //            if (sourceFiles[i] != null)
            //            {
            //                if (sourceFiles[i].Trim() != "")
            //                    arr[i] = sourceFiles[i].ToString();
            //            }
            //        }

            //        if (arr != null)
            //        {
            //            sSrcFile = new string[sourceFiles.Count()];

            //            for (int ic = 0; ic <= arr.Length - 1; ic++)
            //            {
            //                sSrcFile[ic] = arr[ic].ToString();
            //            }
            //        }
            //        try
            //        {
            //            int f = 0;

            //            using (iTextSharp.text.Document document = new iTextSharp.text.Document(iTextSharp.text.PageSize.A4))
            //            {

            //                iTextSharp.text.pdf.PdfWriter writer = iTextSharp.text.pdf.PdfWriter.GetInstance(document, new FileStream(destinationFile, FileMode.Create));

            //                document.Open();
            //                iTextSharp.text.pdf.PdfContentByte cb = writer.DirectContent;
            //                iTextSharp.text.pdf.PdfImportedPage page;

            //                int rotation;
            //                while (f < sSrcFile.Length)
            //                {

            //                    iTextSharp.text.pdf.PdfReader reader = new iTextSharp.text.pdf.PdfReader(sSrcFile[f]);
            //                    int n = reader.NumberOfPages;
            //                    int i = 0;
            //                    while (i < n)
            //                    {
            //                        i++;

            //                        document.SetPageSize(iTextSharp.text.PageSize.A4);
            //                        document.NewPage();
            //                        page = writer.GetImportedPage(reader, i);

            //                        rotation = reader.GetPageRotation(i);
            //                        if (rotation == 90 || rotation == 270)
            //                        {
            //                            cb.AddTemplate(page, 0, -1f, 1f, 0, 0, reader.GetPageSizeWithRotation(i).Height);
            //                        }
            //                        else
            //                        {
            //                            cb.AddTemplate(page, 1f, 0, 0, 1f, 0, 0);
            //                        }
            //                    }

            //                    f++;
            //                   // reader.Dispose();
            //                    //if (f < sSrcFile.Length)
            //                    //{

            //                    //    reader = new iTextSharp.text.pdf.PdfReader(sSrcFile[f]);
            //                    //    n = reader.NumberOfPages;

            //                    //}



            //                }

            //                document.Close();

            //                // reader.Close();
            //               //  reader.Dispose();
            //            }
            //           // reader.Close();
            //           // reader.Dispose();
            //           //reader = new PdfReader();
            //           // sSrcFile= new string[0];
            //        }

            //        catch (Exception e)
            //        {
            //            // Response.Write(e.Message);
            //        }


            //    }


            //}
            #endregion

            #region new code

            using (FileStream stream = new FileStream(destinationFile, FileMode.Create))
            {
                iTextSharp.text.Document    pdfDoc = new iTextSharp.text.Document(iTextSharp.text.PageSize.A4);
                iTextSharp.text.pdf.PdfCopy pdf    = new iTextSharp.text.pdf.PdfCopy(pdfDoc, stream);
                pdfDoc.Open();
                var files = Directory.GetFiles(ExportPath);

                int i = 1;
                foreach (string file in files)
                {
                    var reader = new iTextSharp.text.pdf.PdfReader(file);
                    pdf.AddDocument(reader);
                    i++;
                    reader.Close();
                }

                if (pdfDoc != null)
                {
                    pdfDoc.Close();
                }
            }

            #endregion
        }
        public static bool CombineBriefPages_AddingBlanks(
            List <CockleFilePdf> srcFiles, string src, TypeOfBindEnum bind)
        {
            // new attempt Dec 28, to account for divider pages (or any page without text)
            // text has to start on odd-numbered page, if followed by divider page

            // first, add 2 pages for each divider page, to account for front and back.
            // then, when everything is together, cycle through doc to add extra dividers...
            // ... so that text always falls on odd-numbered page


            // should work for both Saddle Stitch and Perfect Bind


            // create new list without cover, ordered by rank
            List <CockleFilePdf> files = new List <CockleFilePdf>(
                srcFiles
                .Where(f => f.FileType != SourceFileTypeEnum.Cover)
                .Where(f => f.FileType != SourceFileTypeEnum.InsideCv)
                .Where(f => f.FileType != SourceFileTypeEnum.SidewaysPage)
                .Where(f => f.FileType != SourceFileTypeEnum.Brief_Foldout)
                .Where(f => f.FileType != SourceFileTypeEnum.Brief_ZFold)
                .Where(f => f.FileType != SourceFileTypeEnum.App_Foldout)
                .Where(f => f.FileType != SourceFileTypeEnum.App_ZFold)
                .Where(f => f.FileType != SourceFileTypeEnum.Unrecognized)
                .OrderBy(f => f.Rank));

            if (files.Count < 1)
            {
                return(false);
            }

            // what if files.Count == 1 ??? just return ???

            int  pageCount         = 0;
            bool hasDividers       = false;
            bool firstAppFileFound = false;
            int  firstPageOfApp    = -1;

            try
            {
                using (var stream = new System.IO.FileStream(src, System.IO.FileMode.Create))
                {
                    // initiate iTextSharp processes
                    iTextSharp.text.Document    pdfdoc  = new iTextSharp.text.Document(iTextSharp.text.PageSize.LETTER);
                    iTextSharp.text.pdf.PdfCopy pdfcopy = new iTextSharp.text.pdf.PdfCopy(pdfdoc, stream);
                    pdfdoc.Open();

                    // merge pdfs in folder
                    CockleFilePdf f;
                    for (int i = 0; i < files.Count; i++)
                    {
                        f = files[i];
                        // read file
                        iTextSharp.text.pdf.PdfReader reader = new iTextSharp.text.pdf.PdfReader(f.FullName);
                        int filePageCount = reader.NumberOfPages;

                        // set up pdfstamper
                        iTextSharp.text.pdf.PdfStamper stamper = new iTextSharp.text.pdf.PdfStamper(reader, stream);

                        // look for divider pages here, add blank if exists
                        List <int> divider_pages = new List <int>();
                        iTextSharp.text.pdf.parser.PdfReaderContentParser parser = new iTextSharp.text.pdf.parser.PdfReaderContentParser(reader);
                        for (int j = 1; j <= reader.NumberOfPages; j++)
                        {
                            iTextSharp.text.pdf.parser.SimpleTextExtractionStrategy extract = new iTextSharp.text.pdf.parser.SimpleTextExtractionStrategy();
                            var    extractedText = parser.ProcessContent(j, extract);
                            string textFromPage  = extractedText.GetResultantText();

                            int cnt     = textFromPage.ToCharArray().Count();
                            int mch_cnt = System.Text.RegularExpressions.Regex.Matches(textFromPage, @"A(PPENDIX|ppendix)").Count;

                            if (System.Text.RegularExpressions.Regex.Matches(textFromPage, @"\S").Count == 0)
                            {
                                // collect blank pages
                                divider_pages.Add(j);
                            }
                            else if (cnt < 50 && mch_cnt > 0)
                            {
                                // collect other divider pages
                                divider_pages.Add(j);
                            }
                        }
                        if (divider_pages.Count > 0)
                        {
                            hasDividers = true;

                            int k = 0; // adjust for total page number change
                            foreach (int page in divider_pages)
                            {
                                stamper.InsertPage(page + k, reader.GetPageSizeWithRotation(1));
                                filePageCount = reader.NumberOfPages;
                                k++;
                            }
                        }

                        // add blank page if needed to make even number
                        if (files[i].FileType == SourceFileTypeEnum.Index ||
                            files[i].FileType == SourceFileTypeEnum.Brief ||
                            files[i].FileType == SourceFileTypeEnum.App_Index ||
                            files[i].FileType == SourceFileTypeEnum.Motion ||
                            files[i].FileType == SourceFileTypeEnum.Divider_Page)
                        {
                            f.AssignNeedsBlankPage(files, reader.NumberOfPages);
                            if (f.NeedsBlankPage)
                            {
                                //PdfStamper stamper2 = new PdfStamper(reader, stream);
                                stamper.InsertPage(reader.NumberOfPages + 1, reader.GetPageSizeWithRotation(1));
                                filePageCount = reader.NumberOfPages;
                            }
                        }

                        // with last document in 'files', add extra pages to make divisible by 4
                        if (bind == TypeOfBindEnum.SaddleStitch && i == files.Count - 1)
                        {
                            if (bind == TypeOfBindEnum.SaddleStitch &&
                                (pageCount + reader.NumberOfPages) % 4 != 0)
                            {
                                //PdfStamper stamper3 = new PdfStamper(reader, stream);
                                while ((pageCount + reader.NumberOfPages) % 4 != 0)
                                {
                                    stamper.InsertPage(reader.NumberOfPages + 1, reader.GetPageSizeWithRotation(1));
                                }
                            }
                        }

                        // get first page of first app file
                        if (!firstAppFileFound && files[i].FileType == SourceFileTypeEnum.App_File)
                        {
                            firstAppFileFound = true;
                            firstPageOfApp    = pageCount + 1;
                        }

                        // add document to 'src'
                        pdfcopy.AddDocument(new iTextSharp.text.pdf.PdfReader(reader));
                        pageCount += reader.NumberOfPages;
                    }

                    pdfcopy.Close();
                    pdfdoc.CloseDocument();
                }

                // final cycle, if dividers, to make sure text starts on odd-sided pages
                if (bind == TypeOfBindEnum.PerfectBind && hasDividers)
                {
                    string dest = (System.IO.Path.GetDirectoryName(src) + @"\temp " + DateTime.Now.ToString("ddMMyyyyhhmmssffff"));

                    using (var stream = new System.IO.FileStream(dest, System.IO.FileMode.Create))
                    {
                        iTextSharp.text.pdf.PdfReader  reader  = new iTextSharp.text.pdf.PdfReader(src);
                        iTextSharp.text.pdf.PdfStamper stamper = new iTextSharp.text.pdf.PdfStamper(reader, stream);

                        // get all blank pages in appendix
                        iTextSharp.text.pdf.parser.PdfReaderContentParser parser = new iTextSharp.text.pdf.parser.PdfReaderContentParser(reader);
                        List <List <int> > groupsOfBlanks = new List <List <int> >();
                        List <int>         group_list     = new List <int>();
                        int x;
                        for (x = firstPageOfApp; x <= reader.NumberOfPages; x++)
                        {
                            iTextSharp.text.pdf.parser.SimpleTextExtractionStrategy extract = new iTextSharp.text.pdf.parser.SimpleTextExtractionStrategy();
                            var    extractedText = parser.ProcessContent(x, extract);
                            string textFromPage  = extractedText.GetResultantText();
                            // find blank pages and cluster into group_list
                            if (System.Text.RegularExpressions.Regex.Matches(textFromPage, @"\S").Count == 0)
                            {
                                // capture blank page cluster (??? but what if only 1 page ???)
                                if (group_list.Count == 0 || group_list.Contains(x - 1))
                                {
                                    group_list.Add(x);
                                }
                            }
                            else
                            {
                                // find first page after cluster
                                if (group_list.Count > 0)
                                {
                                    if (group_list.Last() % 2 == 1)
                                    {
                                        // add blank page
                                        stamper.InsertPage(group_list.Last() + 1, reader.GetPageSizeWithRotation(1));
                                    }
                                }
                                // clear list
                                group_list.Clear();
                            }
                        }
                        stamper.Close();
                        reader.Close();
                    }
                    System.IO.File.Delete(src);
                    System.IO.File.Move(dest, src);
                }
            }
            catch (Exception excpt)
            {
                System.Diagnostics.Debug.WriteLine(excpt); return(false);
            }
            return(true);
        }