private void _queuePdf() { while (_isRunning) { try { TOArqPdf fileName = null; if (_cqFilesPdf.TryDequeue(out fileName)) { this._processPdf(fileName); } else { lock (_syncPdf) Monitor.Wait(_syncPdf, 300); } } catch { } } }
private void _moveFile(string fileName) { try { if (_isFileReady(fileName)) { string pathBkp = this.Config.PathBkp + Path.DirectorySeparatorChar + DateTime.Now.ToString("yyyy-MM-dd"); if (!Directory.Exists(pathBkp)) { Directory.CreateDirectory(pathBkp); } string onlyFileName = fileName.Substring(fileName.LastIndexOf(Path.DirectorySeparatorChar) + 1); string fileBkp = pathBkp + Path.DirectorySeparatorChar + onlyFileName; if (!File.Exists(fileBkp)) { File.Move(fileName, fileBkp); switch (this.Config.FileType) { case FileTypes.PDF: TOArqPdf toPdf = new TOArqPdf(); toPdf.FileName = fileBkp; toPdf.Config = this.Config; PdfManager.Instance.AddPdfFile(toPdf); break; case FileTypes.TXT: TOArqTxt toTxt = new TOArqTxt(); toTxt.FileName = fileBkp; toTxt.Config = this.Config; TxtManager.Instance.AddTxtFile(toTxt); break; } } else { string ext = fileName.Substring(fileName.LastIndexOf('.'), fileName.Length - fileName.LastIndexOf('.')); string newext = DateTime.Now.ToString(".yyyy-MM-dd-HH-mm-ss-fff") + ext; string newFileName = fileBkp.Substring(0, fileBkp.LastIndexOf('.')) + newext; File.Move(fileName, newFileName); switch (this.Config.FileType) { case FileTypes.PDF: TOArqPdf toPdf = new TOArqPdf(); toPdf.FileName = newFileName; toPdf.Config = this.Config; PdfManager.Instance.AddPdfFile(toPdf); break; case FileTypes.TXT: TOArqTxt toTxt = new TOArqTxt(); toTxt.FileName = newFileName; toTxt.Config = this.Config; TxtManager.Instance.AddTxtFile(toTxt); break; } } } else { logger.Debug("File not ready: " + fileName); } } catch (Exception ex) { logger.Error("Erro ao mover o arquivo: " + ex.Message, ex); } }
// Efetua o split do arquivo pdf private void _processPdf(TOArqPdf to) { PdfReader reader = null; Document sourceDocument = null; PdfCopy pdfCopy = null; PdfImportedPage page = null; int currentCod = 0; string arqname; string completePath; try { logger.Info("============================================================"); logger.Info("Processando arquivo: " + to.FileName); reader = new PdfReader(to.FileName); sourceDocument = new Document(reader.GetPageSizeWithRotation(1)); string pathTimeStamp = DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss-fff"); completePath = to.Config.PathProcessed + "\\" + pathTimeStamp; if (!Directory.Exists(completePath)) { Directory.CreateDirectory(completePath); } string onlyFile = to.FileName.Substring(to.FileName.LastIndexOf("\\") + 1); int pageCount = reader.NumberOfPages; for (int i = 1; i <= pageCount; i++) { ITextExtractionStrategy strategy = new SimpleTextExtractionStrategy(); string currentText = PdfTextExtractor.GetTextFromPage(reader, i, strategy); currentText = Encoding.UTF8.GetString(ASCIIEncoding.Convert(Encoding.Default, Encoding.UTF8, Encoding.Default.GetBytes(currentText))); int idCliente = 0; switch (to.Config.Type) { case TypeWatcher.BMF: idCliente = PdfTools.ExtractBMFClientID(currentText); break; case TypeWatcher.BOVESPA: idCliente = PdfTools.ExtractBovespaClientID(currentText); break; case TypeWatcher.POSICAO_BMF: idCliente = PdfTools.ExtractPosicaoBmfClientID(currentText); break; } logger.InfoFormat("Arquivo: [{0}] Pagina: [{1}] IdCliente [{2}]", onlyFile, i.ToString("D5"), idCliente.ToString("D8")); if (idCliente >= 0) { if (currentCod != idCliente) { // Salvar o anterior if (pdfCopy != null) { pdfCopy.Close(); sourceDocument.Close(); sourceDocument = new Document(reader.GetPageSizeWithRotation(i)); } arqname = string.Format("{0}\\{1}\\{2}.pdf", to.Config.PathProcessed, pathTimeStamp, idCliente.ToString("D8")); if (File.Exists(arqname)) { arqname = string.Format("{0}\\{1}\\{2}-{3}.pdf", to.Config.PathProcessed, pathTimeStamp, idCliente.ToString("D8"), new Random().Next(1, 100000).ToString("D6")); } pdfCopy = new PdfCopy(sourceDocument, new FileStream(arqname, FileMode.Create)); sourceDocument.Open(); currentCod = idCliente; } page = pdfCopy.GetImportedPage(reader, i); pdfCopy.AddPage(page); // Verifica se pagina corrente eh igual ao numero de paginas, para fechar o arquivo if (i == pageCount) { pdfCopy.Close(); pdfCopy = null; } } else { logger.Error("Não foi possivel extrair o codigo de cliente da pagina: " + i); continue; } } EmailManager.Instance.ProcessEmailPath(completePath, to.Config); } catch (Exception ex) { logger.Error("Problemas no processamento do Pdf: " + ex.Message, ex); } finally { if (reader != null) { reader.Close(); reader = null; } if (sourceDocument != null) { sourceDocument.Close(); sourceDocument = null; } if (pdfCopy != null) { pdfCopy.Close(); pdfCopy = null; } page = null; } }
public void AddPdfFile(TOArqPdf file) { _cqFilesPdf.Enqueue(file); lock (_syncPdf) Monitor.Pulse(_syncPdf); }