public ActionResult UploadFile() { string id_doc_type = Request.Form["inds_"]; Document doc = new Document(); doc.ns_documento_tipo = int.Parse(id_doc_type); DocumentController docCtrl = new DocumentController(); doc = docCtrl.insertDocument(doc); string fullPath = ""; string fileName = ""; try { var file = Request.Form.Files[0]; string folderName = "Cloud/" + DateTime.Now.Year + "/" + DateTime.Now.Month + "/" + DateTime.Now.Day; string webRootPath = _hostingEnvironment.WebRootPath; string newPath = Path.Combine(webRootPath, folderName); if (!Directory.Exists(newPath)) { Directory.CreateDirectory(newPath); } if (file.Length > 0) { fileName = doc.idns_documento.ToString() + System.IO.Path.GetExtension(file.FileName); //ContentDispositionHeaderValue.Parse(file.ContentDisposition).FileName.Trim('"'); fullPath = Path.Combine(newPath, fileName); using (var stream = new FileStream(fullPath, FileMode.Create)) { file.CopyTo(stream); } } // Generate the Thhumbail var pdfFile = fullPath; var pdfToImg = new NReco.PdfRenderer.PdfToImageConverter(); pdfToImg.ScaleTo = 140; // fit 200x200 box //pdfToImg.GenerateImage( pdfFile, 1, ImageFormat.Jpeg, "Sample1.jpg" ); System.Drawing.Image img = pdfToImg.GenerateImage(pdfFile, 1); img.Save(fullPath.Replace(".pdf", ".gif"), System.Drawing.Imaging.ImageFormat.Gif); //var text = GetText(fullPath); return(Json(doc.idns_documento)); } catch (System.Exception ex) { return(Json("Ha ocurrido un error al subir el archivo: " + ex.Message)); } }
static void Rename_Pdf(string path, string saveDirPath, string name, string saveImageDirPath) { var pdfFile = path; var pdfToImg = new NReco.PdfRenderer.PdfToImageConverter(); pdfToImg.ScaleTo = 4000; // fit 200x200 box pdfToImg.GenerateImage(pdfFile, 1, NReco.PdfRenderer.ImageFormat.Jpeg, saveImageDirPath + "\\" + name + ".jpg"); using (var img = new Bitmap(saveImageDirPath + "\\" + name + ".jpg")) { using (var ocr = new TesseractEngine("./tessdata", "tur", EngineMode.Default)) { using (var page = ocr.Process(img)) { string text = page.GetText(); char[] txtArray = text.ToCharArray(); int[] checkingTCKN = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 1 }; string TCKN = ""; for (int i = 1; i < txtArray.Length; i++) { if ( (txtArray[(i - 1)] != '0' && txtArray[(i - 1)] != '1' && txtArray[(i - 1)] != '2' && txtArray[(i - 1)] != '3' && txtArray[(i - 1)] != '4' && txtArray[(i - 1)] != '5' && txtArray[(i - 1)] != '6' && txtArray[(i - 1)] != '7' && txtArray[(i - 1)] != '8' && txtArray[(i - 1)] != '9') && (txtArray[i] == '1' || txtArray[i] == '2' || txtArray[i] == '3' || txtArray[i] == '4' || txtArray[i] == '5' || txtArray[i] == '6' || txtArray[i] == '7' || txtArray[i] == '8' || txtArray[i] == '9') && (txtArray[(i + 1)] == '0' || txtArray[(i + 1)] == '1' || txtArray[(i + 1)] == '2' || txtArray[(i + 1)] == '3' || txtArray[(i + 1)] == '4' || txtArray[(i + 1)] == '5' || txtArray[(i + 1)] == '6' || txtArray[(i + 1)] == '7' || txtArray[(i + 1)] == '8' || txtArray[(i + 1)] == '9') && (txtArray[(i + 2)] == '0' || txtArray[(i + 2)] == '1' || txtArray[(i + 2)] == '2' || txtArray[(i + 2)] == '3' || txtArray[(i + 2)] == '4' || txtArray[(i + 2)] == '5' || txtArray[(i + 2)] == '6' || txtArray[(i + 2)] == '7' || txtArray[(i + 2)] == '8' || txtArray[(i + 2)] == '9') && (txtArray[(i + 3)] == '0' || txtArray[(i + 3)] == '1' || txtArray[(i + 3)] == '2' || txtArray[(i + 3)] == '3' || txtArray[(i + 3)] == '4' || txtArray[(i + 3)] == '5' || txtArray[(i + 3)] == '6' || txtArray[(i + 3)] == '7' || txtArray[(i + 3)] == '8' || txtArray[(i + 3)] == '9') && (txtArray[(i + 4)] == '0' || txtArray[(i + 4)] == '1' || txtArray[(i + 4)] == '2' || txtArray[(i + 4)] == '3' || txtArray[(i + 4)] == '4' || txtArray[(i + 4)] == '5' || txtArray[(i + 4)] == '6' || txtArray[(i + 4)] == '7' || txtArray[(i + 4)] == '8' || txtArray[(i + 4)] == '9') && (txtArray[(i + 5)] == '0' || txtArray[(i + 5)] == '1' || txtArray[(i + 5)] == '2' || txtArray[(i + 5)] == '3' || txtArray[(i + 5)] == '4' || txtArray[(i + 5)] == '5' || txtArray[(i + 5)] == '6' || txtArray[(i + 5)] == '7' || txtArray[(i + 5)] == '8' || txtArray[(i + 5)] == '9') && (txtArray[(i + 6)] == '0' || txtArray[(i + 6)] == '1' || txtArray[(i + 6)] == '2' || txtArray[(i + 6)] == '3' || txtArray[(i + 6)] == '4' || txtArray[(i + 6)] == '5' || txtArray[(i + 6)] == '6' || txtArray[(i + 6)] == '7' || txtArray[(i + 6)] == '8' || txtArray[(i + 6)] == '9') && (txtArray[(i + 7)] == '0' || txtArray[(i + 7)] == '1' || txtArray[(i + 7)] == '2' || txtArray[(i + 7)] == '3' || txtArray[(i + 7)] == '4' || txtArray[(i + 7)] == '5' || txtArray[(i + 7)] == '6' || txtArray[(i + 7)] == '7' || txtArray[(i + 7)] == '8' || txtArray[(i + 7)] == '9') && (txtArray[(i + 8)] == '0' || txtArray[(i + 8)] == '1' || txtArray[(i + 8)] == '2' || txtArray[(i + 8)] == '3' || txtArray[(i + 8)] == '4' || txtArray[(i + 8)] == '5' || txtArray[(i + 8)] == '6' || txtArray[(i + 8)] == '7' || txtArray[(i + 8)] == '8' || txtArray[(i + 8)] == '9') && (txtArray[(i + 9)] == '0' || txtArray[(i + 9)] == '1' || txtArray[(i + 9)] == '2' || txtArray[(i + 9)] == '3' || txtArray[(i + 9)] == '4' || txtArray[(i + 9)] == '5' || txtArray[(i + 9)] == '6' || txtArray[(i + 9)] == '7' || txtArray[(i + 9)] == '8' || txtArray[(i + 9)] == '9') && (txtArray[(i + 10)] == '0' || txtArray[(i + 10)] == '2' || txtArray[(i + 10)] == '4' || txtArray[(i + 10)] == '6' || txtArray[(i + 10)] == '8') && (txtArray[(i + 11)] != '0' && txtArray[(i + 11)] != '1' && txtArray[(i + 11)] != '2' && txtArray[(i + 11)] != '3' && txtArray[(i + 11)] != '4' && txtArray[(i + 11)] != '5' && txtArray[(i + 11)] != '6' && txtArray[(i + 11)] != '7' && txtArray[(i + 11)] != '8' && txtArray[(i + 11)] != '9') ) { for (int j = 0; j < 11; j++) { checkingTCKN[j] = (int)Char.GetNumericValue(txtArray[(i + j)]); TCKN += txtArray[i + j]; } // Change pdf name with TCKN if (isIt_TCKN(checkingTCKN) == true) { byte[] array = System.IO.File.ReadAllBytes(path); System.IO.File.WriteAllBytes(saveDirPath + "\\" + TCKN + ".pdf", array); break; } else { continue; } } } } } } }