public ActionResult UploadOCRFile(HttpPostedFileBase uploadOCRFile) { string result = GlobalConst.ConstantChar.Blank; try { OCRModel ocrModel = new OCRModel(); HttpPostedFileBase File = Request.Files[GlobalConst.ConstantChar.UploadOCRFile]; bool r = _storageServices.CreateOCRUploadFolder(Server.MapPath(ConfigurationManager.AppSettings[GlobalConst.ConstantChar.StoragePath]), GlobalConst.ConstantChar.OCRUploads); string OCRPath = Server.MapPath(ConfigurationManager.AppSettings[GlobalConst.ConstantChar.StoragePath].ToString()) + GlobalConst.ConstantChar.DoubleSlash + GlobalConst.ConstantChar.OCRUploads + GlobalConst.ConstantChar.DoubleSlash; string OCRFilePath = OCRPath + Path.GetFileName(uploadOCRFile.FileName); File.SaveAs(OCRFilePath); Reader bc = new Reader(); bc.RegistrationKey = WebConfigurationManager.AppSettings[GlobalConst.ConstantChar.BarCodeKey]; bc.RegistrationName = WebConfigurationManager.AppSettings[GlobalConst.ConstantChar.BarCodeKeyName]; bc.BarcodeTypesToFind.Code39 = true; iTextSharp.text.pdf.PdfReader reader = null; reader = new iTextSharp.text.pdf.PdfReader(OCRFilePath); FoundBarcode[] barcodes = bc.ReadFrom(OCRFilePath).Where(val => val.Value == GlobalConst.ConstantChar.BarCodeCHECK).ToArray(); int i = 0; for (i = 0; i <= barcodes.Length - 1; i++) { string newOCRfileName = Guid.NewGuid().ToString() + GlobalConst.Extension.PDF; string newOCRfilepath = OCRPath + newOCRfileName; string[] barcodename1 = barcodes[i].Value.Split(GlobalConst.ConstantChar.RoundBracketStart); string barcodename = barcodename1[0]; int pagenoE = 0; int pagenoS = barcodes[i].Page; //page starting bool flag = false; if (i == barcodes.Length - 1) { pagenoE = reader.NumberOfPages; } else { int num = 1; while (flag == false) { if (i + num < barcodes.Length) { if (barcodename == GlobalConst.ConstantChar.BarCodeCHECK) { pagenoE = barcodes[i + num].Page; flag = true; } } else { flag = true; pagenoE = reader.NumberOfPages; } num = num + 1; } } if (barcodename == GlobalConst.ConstantChar.BarCodeCHECK) { ocrModel.OcrFileName = newOCRfileName; ocrModel.CreatedBy = Convert.ToInt32(Request.Cookies[GlobalConst.SessionKeys.userDetailMgmt].Values[GlobalConst.SessionKeys.userID]); ocrModel.CreatedOn = DateTime.Now; _ocr.AddOCRRecord(Mapper.Map <LMGEDI.Core.Data.Model.OCR>(ocrModel)); ExtractPage(OCRFilePath, newOCRfilepath, pagenoS + 1, pagenoE - 1); result = GlobalConst.ConstantChar.True; } } reader.Dispose(); System.IO.File.Delete(OCRFilePath); } catch (Exception ex) { _arCommonService.CreateErrorLog(ex.Message, ex.StackTrace); } return(Json(result)); }
/// <summary> /// 获取指定PDF文件的总页码数 /// </summary> /// <param name="strSourceFile">PDF文件路径</param> /// <returns></returns> public static int GetPdfFilePages(string strSourceFile, out string errorMsg) { errorMsg = string.Empty; int iPages = -1; if (System.IO.File.Exists(strSourceFile)) { iTextSharp.text.pdf.PdfReader reader = null; try { reader = new iTextSharp.text.pdf.PdfReader(strSourceFile); iPages = reader.NumberOfPages; } catch (iTextSharp.text.exceptions.IllegalPdfSyntaxException) { iPages = -10; errorMsg = "iTextSharp IllegalPdfSyntaxException"; } catch (iTextSharp.text.exceptions.InvalidImageException) { iPages = -10; errorMsg = "iTextSharp InvalidImageException"; } catch (iTextSharp.text.exceptions.InvalidPdfException) { iPages = -10; errorMsg = "iTextSharp InvalidPdfException"; } catch (iTextSharp.text.exceptions.BadPasswordException) { iPages = -10; errorMsg = "iTextSharp BadPasswordException"; } catch (StackOverflowException es) { iPages = -1; errorMsg = string.Format("iTextSharp Reader StackOverflowException Error:{0}", es.Message); } catch (Exception ex) { iPages = -1; errorMsg = ex.Message; } finally { if (reader != null) { reader.Dispose(); } } } else { iPages = -1; } if (errorMsg.Length > 0) { IOHelp.WriteLog(string.Format("pdffile={0}, error={1}", strSourceFile, errorMsg), 5, "checkpdfError"); } return(iPages); }