public static int GetPDFPageCountFromFolder(string _pdfFolder)
        {
            try
            {
                int _count = 0;
                DirectoryInfo di = new DirectoryInfo(_pdfFolder);
                FileInfo[] rgFiles = di.GetFiles("*.*");

                foreach (FileInfo fi in rgFiles)
                {
                    Syncfusion.Pdf.PDFDocument _pdfDocument
                        = new Syncfusion.Pdf.PDFDocument();
                    _pdfDocument 
                        = Syncfusion.Pdf.PDFDocument.Load(fi.FullName);

                    _count = _count + _pdfDocument.Pages.Count;
                    _pdfDocument.Close();
                }

                return _count;
            }
            catch
            {
                return 0;
            }
        }
        /*
        public static bool CreatePDFToReviewV2(string sAdobePath, string sAdobeNewFilePath)
        {

            PdfSharp.Pdf.PdfDocument TOBECOPIEDpdf = new PdfSharp.Pdf.PdfDocument();
            PdfSharp.Pdf.PdfDocument NEWpdf = new PdfSharp.Pdf.PdfDocument();
            //PdfDocumentOpenMode.Import
            PdfSharp.Pdf.PdfPage PDPage;

            DataSet ds = new DataSet();
            DataTable MyTable = null;
            DataRow MyRow = null;
            DataView dv = null;

            ReviewPages = 0;

            int lFileCount = 0;
            int lCount1 = 0;

            DirectoryInfo di = new DirectoryInfo(sAdobePath);
            FileInfo[] rgFiles = di.GetFiles("*.*");

            using (SqlConnection cn = new SqlConnection(Database.WaldenConnect))
            {
                cn.Open();
                using (SqlCommand cm = cn.CreateCommand())
                {

                    cm.CommandText = "delete from ScanFiles";
                    cm.ExecuteNonQuery();

                    string sSQL = string.Empty;
                    System.Data.SqlClient.SqlDataAdapter sSQLAdp = null;
                    sSQL = "select FileName,FileDate from ScanFiles"
                        + " where AccountID = " + Common.Practice_ID
                        + " order by FileName desc";
                    using (sSQLAdp = new System.Data.SqlClient.SqlDataAdapter(sSQL, cn))
                    {
                        sSQLAdp.Fill(ds, "ScanFiles");
                        MyTable = ds.Tables[0];
                        foreach (FileInfo fi in rgFiles)
                        {
                            MyRow = MyTable.NewRow();
                            MyRow["FileName"] = fi.FullName;
                            MyRow["FileDate"] = fi.CreationTime.ToString();
                            MyTable.Rows.Add(MyRow);
                            lFileCount++;
                        }
                        string[] srcFilesNames = new string[lFileCount];
                        dv = MyTable.DefaultView;

                        //dv.Sort = "scan_file_date desc";
                        dv.Sort = "FileDate";

                        for (int b = 0; b < dv.Count; b++)
                        {
                            TOBECOPIEDpdf = PdfReader.Open(dv[b].Row[0].ToString(), PdfDocumentOpenMode.Import);
                            srcFilesNames[lCount1] = dv[b].Row[0].ToString();
                            lCount1++;
                            //Write document file name and page number on each page
                            for (int i = 0; i < TOBECOPIEDpdf.PageCount; i++)
                            {
                                PDPage = TOBECOPIEDpdf.Pages[i];
                                NEWpdf.AddPage(PDPage);
                                ReviewPages++;
                            }
                        }
                    }
                    NEWpdf.Save(sAdobeNewFilePath);
                }
            }
            return true;
         
        }
        */
        public static bool DeletePDFPages(string _documentName, int _pageToDelete)
        {
            try
            {
                //Page numbers start with 0 so we
                //have to reduce number by 1
                _pageToDelete = _pageToDelete - 1 ;

                string _fileToProcess = string.Empty;

                //Need to make copy of file.  This is the file we will
                //process
                _fileToProcess = _documentName.Replace(".pdf", "1.pdf");
                File.Copy(_documentName, _fileToProcess, true); 
                
                Syncfusion.Pdf.PDFDocument _pdfDocumentTemp
                    = new Syncfusion.Pdf.PDFDocument();
                
                Syncfusion.Pdf.PDFDocument _pdfDocument
                    = new Syncfusion.Pdf.PDFDocument();

                _pdfDocumentTemp 
                    = Syncfusion.Pdf.PDFDocument.Create();

                Syncfusion.Pdf.IPDFPage _pdfPage;                
                
                //Open up file to process
                _pdfDocument =
                    Syncfusion.Pdf.PDFDocument.Load(_fileToProcess);

                //Place all pages in new document
                for (int i = 0; i < _pdfDocument.Pages.Count; i++)
                {
                    _pdfPage = _pdfDocument.Pages[i];
                    _pdfDocumentTemp.Pages.Add(_pdfPage);
                }

                //Delete page from new document
                _pdfPage = _pdfDocumentTemp.Pages[_pageToDelete];
                _pdfDocumentTemp.Pages.Remove(_pdfPage);
                //Save document overwriting file
                _pdfDocumentTemp.Save(_documentName);

                //Close document
                _pdfDocument.Close();
                //Delete temporary file
                 File.Delete(_fileToProcess); 

                return true;
            }
            catch (Exception ex)
            {
                Common.Log(ex.Message);
                return false;
            }
        }
        public static int GetPDFPageCountFromID(string _documentID)
        {
            string _documentPath = DatabaseCalls.ReturnSingleStringValue("select "
                + " ScanPath from ScanPatientDocuments"
                + " where ScanID = " + _documentID);
            
            int _count = 0;

            Syncfusion.Pdf.PDFDocument _pdfDocument
                = new Syncfusion.Pdf.PDFDocument();
            _pdfDocument = Syncfusion.Pdf.PDFDocument.Load(
                Common.StoreComputer 
                + Common.StoreShare + _documentPath);
            _count = _pdfDocument.Pages.Count;
            _pdfDocument.Close(); 

            return _count;
        }
        public static int GetPDFPageCount(string _pdfFile)
        {
            try
            {
                int _count = 0;
                Syncfusion.Pdf.PDFDocument _pdfDocument
                    = new Syncfusion.Pdf.PDFDocument();
                _pdfDocument = Syncfusion.Pdf.PDFDocument.Load(_pdfFile);

                _count = _pdfDocument.Pages.Count;
                _pdfDocument.Close();
                return _count;
            }
            catch 
            {
                return 0;
            }
        }
        public static bool MergeTwoDocuments(string _documentToMerge,
            string _MergedDocument)
        {

            try
            {
                Syncfusion.Pdf.PDFDocument _toBeCopied1Pdf 
                    = new Syncfusion.Pdf.PDFDocument();
                Syncfusion.Pdf.PDFDocument _toBeCopied2Pdf 
                    = new Syncfusion.Pdf.PDFDocument();
                Syncfusion.Pdf.PDFDocument Mergedpdf = 
                    new Syncfusion.Pdf.PDFDocument();
                Syncfusion.Pdf.IPDFPage _pdfPage;

                //Merged document may be read only Need to change attributes so it can
                //be written to
                MakeDocumentReadable(_MergedDocument);

                //Create string name with a 1 added 
                string sTempName = _MergedDocument.Replace(".pdf", "1.pdf");

                //Copy current document to name with 1 added
                File.Copy(_MergedDocument, sTempName, true);

                //Open the New document  By opening it first
                //these pages will be placed at the top of the
                //document
                _toBeCopied1Pdf =
                    Syncfusion.Pdf.PDFDocument.Load(_documentToMerge);
                //Write document file name and page number on each page
                //Adding pages to the document with the name that is in 
                //the database for that folder.  These are the pages of the
                //new document.  They are done in this sequence so that
                //the most recent document will be at the top
                try
                {
                    int zz = _toBeCopied1Pdf.Pages.Count;
                }
                catch (Exception er)
                {
                    Common.Log(er.Message);
                }

                for (int i = 0; i < _toBeCopied1Pdf.Pages.Count; i++)
                {
                    _pdfPage = (Syncfusion.Pdf.IPDFPage)_toBeCopied1Pdf.Pages[i];
                    Mergedpdf.Pages.Add(_pdfPage);
                }

                //Adding pages to the document with the name that is in 
                //the database for that folder.  These are the pages that
                //are currently in the document
                _toBeCopied2Pdf = Syncfusion.Pdf.PDFDocument.Load(sTempName);
                //Write document file name and page number on each page
                for (int i = 0; i < _toBeCopied2Pdf.Pages.Count; i++)
                {
                    _pdfPage = _toBeCopied2Pdf.Pages[i];
                    Mergedpdf.Pages.Add(_pdfPage);
                }
                //Save new document over the old document. 
                //It will be overwritten
                Mergedpdf.Save(_MergedDocument);

                //Close all created documents
                _toBeCopied1Pdf.Close();
                _toBeCopied2Pdf.Close();
                //Delete temporary documents
                File.Delete(sTempName);
                File.Delete(_documentToMerge);
                return true;
            }
            catch(Exception er)
            {
                string s1 = er.Message;
                return false;
            }
        }
        public static bool SplitTwoPDFDocuments(string _document1,string _document2,
            int _startPage, int _numberOfPages)
        {
            try
            {
                string _fileToProcess = string.Empty;

                _fileToProcess = _document2.Replace(".pdf", "1.pdf");

                File.Copy(_document1, _fileToProcess, true);

                Syncfusion.Pdf.PDFDocument _newPDF
                    = new Syncfusion.Pdf.PDFDocument();

                Syncfusion.Pdf.PDFDocument _firstSplitPDF
                    = new Syncfusion.Pdf.PDFDocument();

                Syncfusion.Pdf.PDFDocument _secondSplitPDF
                    = new Syncfusion.Pdf.PDFDocument();
                Syncfusion.Pdf.IPDFPage _pdfPage;

                _firstSplitPDF = Syncfusion.Pdf.PDFDocument.Create();
                _secondSplitPDF = Syncfusion.Pdf.PDFDocument.Create();

                _newPDF = Syncfusion.Pdf.PDFDocument.Load(_fileToProcess);

                for (int i = 0; i < _startPage; i++)
                {
                    _pdfPage = _newPDF.Pages[i];
                    _firstSplitPDF.Pages.Add(_pdfPage);
                }

                for (int i = _startPage; i < _numberOfPages; i++)
                {
                    _pdfPage = _newPDF.Pages[i];
                    _secondSplitPDF.Pages.Add(_pdfPage);
                }
                _firstSplitPDF.Save(_document1);
                _secondSplitPDF.Save(_document2);

                _newPDF.Close();
                _newPDF = null;

                _firstSplitPDF.Close();
                _firstSplitPDF = null;
                _secondSplitPDF.Close();
                _secondSplitPDF = null;
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }
        public static bool CreateSinglePDFFromMultiplePDFForReview1(string sAdobePath, string sAdobeNewFilePath)
        {
            int lFileCount = 0;
            

            Syncfusion.Pdf.PDFDocument _newPDF
                = new Syncfusion.Pdf.PDFDocument();

            Syncfusion.Pdf.IPDFPage _pdfPage;

            DataSet ds = new DataSet();
            DataTable MyTable = null;
            DataRow MyRow = null;
            DataView dv = null;

            int ReviewPages = 0;

            int lCount1 = 0;
            //11/13/2006 Error handling now included
            try
            {
                DirectoryInfo di = new DirectoryInfo(sAdobePath);
                //Only check for PDF files
                FileInfo[] rgFiles = di.GetFiles("*.pdf");

                   
                string[] _pdfFileList;
                _pdfFileList = new string[rgFiles.Length];

                ArrayList _pdfArrayList = new ArrayList();

                using (SqlConnection cn = new SqlConnection(Database.WaldenConnect))
                {
                    cn.Open();
                    using (SqlCommand cm = cn.CreateCommand())
                    {
                        cm.CommandText = "delete from ScanFiles";
                        cm.ExecuteNonQuery();

                        string sSQL = string.Empty;
                        System.Data.SqlClient.SqlDataAdapter sSQLAdp = null;
                        sSQL = "select FileName,FileDate from ScanFiles"
                            + " where AccountID = " + Common.Practice_ID
                            + " order by FileDate desc";
                        using (sSQLAdp = new System.Data.SqlClient.SqlDataAdapter(sSQL, cn))
                        {
                            sSQLAdp.Fill(ds, "ScanFiles");
                            MyTable = ds.Tables[0];
                            foreach (FileInfo fi in rgFiles)
                            {
                                MyRow = MyTable.NewRow();
                                MyRow["FileName"] = fi.FullName;
                                MyRow["FileDate"] = fi.CreationTime.ToString();
                                MyTable.Rows.Add(MyRow);
                                lFileCount++;
                            }
                            {
                                string[] srcFilesNames = new string[lFileCount];
                                dv = MyTable.DefaultView;
                                dv.Sort = "FileDate";

                                for (int b = 0; b < dv.Count; b++)
                                {

                                    Syncfusion.Pdf.Parsing.PdfLoadedDocument _toBeCopiedPDF
                                        = new Syncfusion.Pdf.Parsing.PdfLoadedDocument(dv[b].Row[0].ToString());                                     
                                    
                                    //  Syncfusion.Pdf.PDFDocument _toBeCopiedPDF
                                    //    = Syncfusion.Pdf.PDFDocument.Load(dv[b].Row[0].ToString());
                                    
                                    //_toBeCopiedPDF = Syncfusion.Pdf.PDFDocument.Load(dv[b].Row[0].ToString());
                                    srcFilesNames[lCount1] = dv[b].Row[0].ToString();
                                    //_pdfFileList[lCount1] = dv[b].Row[0].ToString();
                                    //_pdfArrayList.Add(dv[b].Row[0].ToString()); 
                                    lCount1++;
                                    //Write document file name and page number on each page
                                   for (int i = 0; i < _toBeCopiedPDF.Pages.Count; i++)
                                    {
                                        _pdfPage = (Syncfusion.Pdf.IPDFPage) _toBeCopiedPDF.Pages[i];
                                        _newPDF.Pages.Add(_pdfPage);
                                        ReviewPages++;
                                    }
                                    //_toBeCopiedPDF.Close();
                                }

                                //Syncfusion.Pdf.PDFDocument.Merge(sAdobeNewFilePath, _pdfFileList);
                                //Syncfusion.Pdf.PDFDocument.CloseDocuments(_pdfArrayList);


                                _newPDF.Save(sAdobeNewFilePath);  

                                _newPDF.Close(); 
                                //_toBeCopiedPDF.Close();
                                _newPDF = null;
                                //_toBeCopiedPDF = null;
                                return true;
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Common.Log(ex.Message);
                return false;
            }
        }