private string CreateDocumentBasedOnPageCount(ScannedDocument barCodeText)
        {
            Aspose.Pdf.Document pdfDocument1 = new Aspose.Pdf.Document(barCodeText.FullPath);
            Aspose.Pdf.Document pdfDocument2 = new Aspose.Pdf.Document();
            string returnDocumentName = string.Empty;

            for (int i = 1; i <= barCodeText.PageCount; i++)
            {
                pdfDocument2.Pages.Add(pdfDocument1.Pages[i]);
            }

            returnDocumentName = ConfigurationValues.PathToWorkingFolder + Utility.GetFileNameFromDateTimeString() + ".pdf";
            pdfDocument2.Save(returnDocumentName);
            pdfDocument1.Dispose();

            Aspose.Pdf.Document pdfDocument3 = new Aspose.Pdf.Document(barCodeText.FullPath);

            for (int i = 1; i <= barCodeText.PageCount; i++)
            {
                pdfDocument3.Pages.Delete(1);
            }

            File.Delete(barCodeText.FullPath);
            pdfDocument3.Save(barCodeText.FullPath);
            pdfDocument3.Dispose();
            return returnDocumentName;
        }
        private OperationResult ValidatePageCounts(string pathTofile)
        {
            OperationResult operationResult = new OperationResult();
            bool foundBarCode = false;
            string currentUser = Utility.GetUserName();
            int testCounter = 1;
            string currentPatientID = string.Empty;
            string currentTabID = string.Empty;

            Aspose.Pdf.Document pdfDocument = new Aspose.Pdf.Document(pathTofile);
            barCodeText = new List<ScannedDocument>();

            int pageCount = pdfDocument.Pages.Count;

            for (var i = 1; i <= pageCount; i++)
            {
                var converter = new PdfConverter();
                converter.BindPdf(pathTofile);
                converter.StartPage = i;
                converter.EndPage = i;
                converter.RenderingOptions.BarcodeOptimization = true;
                converter.Resolution = new Aspose.Pdf.Devices.Resolution(300);
                converter.DoConvert();
                MemoryStream stream = new MemoryStream();
                converter.GetNextImage(stream, ImageFormat.Png);
                using (BarCodeReader reader = new BarCodeReader(stream, BarCodeReadType.Code39Standard))
                {
                    while (reader.Read())
                    {
                        string[] barCodeDocument = reader.GetCodeText().Split('-');
                        ScannedDocument scannedDocument = new ScannedDocument();
                        scannedDocument.PatientID = barCodeDocument[0];
                        scannedDocument.TabID = barCodeDocument[1];
                        scannedDocument.CurrentPageNumber = int.Parse(barCodeDocument[2]);
                        scannedDocument.PageCount = int.Parse(barCodeDocument[3]);
                        scannedDocument.Text = reader.GetCodeText();
                        scannedDocument.FullPath = pathTofile;
                        scannedDocument.User = currentUser;
                        barCodeText.Add(scannedDocument);
                        foundBarCode = true;
                    }
                }
                converter.Close();
                converter.Dispose();

                if (foundBarCode != true)
                {
                    operationResult.Success = false;
                    operationResult.ErrorMessage = "Job Failed: Could not Read Bar Code";
                    return operationResult;
                }
                foundBarCode = false;
            }

            //Step 1 Check to be sure the number of pages in the document
            //matches the number of pages in the bar code
            int pdfPageCount = 0;
            int pdfPageCountCheck = 0;
            Aspose.Pdf.Document pdfDocumentPageCount = new Aspose.Pdf.Document(barCodeText[0].FullPath);
            pdfPageCount = pdfDocumentPageCount.Pages.Count;

            currentPatientID = barCodeText[0].PatientID;
            currentTabID = barCodeText[0].TabID;

            for (int i = 0; i < barCodeText.Count; i++)
            {
                try
                {
                    if (int.Parse(barCodeText[i].PatientID) < 1)
                    {
                        operationResult.Success = false;
                        operationResult.ErrorMessage = "Patient ID Invalid";
                        return operationResult;
                    }
                }
                catch 
                {
                    operationResult.Success = false;
                    operationResult.ErrorMessage = "Patient ID Invalid";
                    return operationResult;
                }
                try
                {
                    if (int.Parse(barCodeText[i].TabID) < 1)
                    {
                        operationResult.Success = false;
                        operationResult.ErrorMessage = "Tab ID Invalid";
                        return operationResult;
                    }
                }
                catch
                {
                    operationResult.Success = false;
                    operationResult.ErrorMessage = "Tab ID Invalid";
                    return operationResult;
                }

                if (testCounter == barCodeText[i].CurrentPageNumber && barCodeText[i].PageCount == barCodeText[i].CurrentPageNumber && barCodeText[i].PatientID == currentPatientID && barCodeText[i].TabID == currentTabID)
                {
                    //string[] barCodePageCount = barCodeText[i].Text.Split('-');
                    pdfPageCountCheck = pdfPageCountCheck + barCodeText[i].PageCount;
                    testCounter = 1;

                    if (barCodeText[i].PageCount == barCodeText[i].CurrentPageNumber)
                    {

                        try
                        {
                            currentPatientID = barCodeText[i + 1].PatientID;
                            currentTabID = barCodeText[i + 1].TabID;
                        }
                        catch { }
                    }
                }
                else
                {
                    if (testCounter == barCodeText[i].CurrentPageNumber && barCodeText[i].PatientID == currentPatientID && barCodeText[i].TabID == currentTabID)
                    {
                        testCounter++;
                    }
                    else
                    {
                        operationResult.Success = false;
                        operationResult.ErrorMessage = "Job Failed: Documents out of Order";
                        return operationResult;
                    }
                }
            }

            if (pageCount != pdfPageCountCheck)
            {
                operationResult.Success = false;
                operationResult.ErrorMessage = "Job Failed: Documents out of Order";
                return operationResult;
            }

            operationResult.Success = true;
            operationResult.ErrorMessage = "Job Confirmed";
            return operationResult;
        }