Rev 430 | Details | Compare with Previous | Last modification | View Log | RSS feed
| Rev | Author | Line No. | Line |
|---|---|---|---|
| 430 | espaco | 1 | package br.com.ec.controller.util; |
| 2 | |||
| 3 | import java.io.ByteArrayOutputStream; |
||
| 4 | import java.io.FileOutputStream; |
||
| 5 | import java.io.IOException; |
||
| 6 | import java.util.ArrayList; |
||
| 7 | import java.util.List; |
||
| 8 | |||
| 9 | import com.itextpdf.text.Document; |
||
| 10 | import com.itextpdf.text.DocumentException; |
||
| 11 | import com.itextpdf.text.pdf.PdfCopy; |
||
| 12 | import com.itextpdf.text.pdf.PdfImportedPage; |
||
| 13 | import com.itextpdf.text.pdf.PdfReader; |
||
| 14 | import com.itextpdf.text.pdf.parser.PdfReaderContentParser; |
||
| 15 | import com.itextpdf.text.pdf.parser.SimpleTextExtractionStrategy; |
||
| 16 | import com.itextpdf.text.pdf.parser.TextExtractionStrategy; |
||
| 17 | |||
| 18 | import br.com.ec.core.util.VerificadorUtil; |
||
| 19 | |||
| 20 | public class PdfUtil { |
||
| 21 | |||
| 22 | public static void main(String[] args) throws IOException, DocumentException { |
||
| 23 | String caminhoArquivo = "D://recibo.pdf"; |
||
| 24 | List<byte[]> arquivos = separarPDF(caminhoArquivo); |
||
| 25 | int i = 1; |
||
| 26 | for (byte[] arquivo : arquivos) { |
||
| 27 | // System.out.println(extrairPDF(arquivo)); |
||
| 28 | String[] linhas = extrairPDFEmLinhas(arquivo); |
||
| 29 | |||
| 30 | boolean capturar = false; |
||
| 31 | String valorVencimentos = ""; |
||
| 32 | String codigo = ""; |
||
| 33 | String linhaAnterior = ""; |
||
| 34 | for (String linha : linhas) { |
||
| 35 | if (linha.contains("Referência")) { |
||
| 36 | valorVencimentos = "" + linhaAnterior; |
||
| 37 | } |
||
| 38 | |||
| 39 | if (capturar) { |
||
| 40 | codigo = "" + linha; |
||
| 41 | capturar = false; |
||
| 42 | |||
| 43 | System.out.println("CÓDIGO: " + codigo); |
||
| 44 | System.out.println("VALOR: R$" + valorVencimentos); |
||
| 45 | } |
||
| 46 | if (linha.contains("CC:")) { |
||
| 47 | capturar = true; |
||
| 48 | } |
||
| 49 | linhaAnterior = "" + linha; |
||
| 50 | } |
||
| 51 | |||
| 52 | // System.out.println(caminhoArquivo.substring(0, caminhoArquivo.indexOf(".pdf")) + "-" + String.format("%03d", i + 1) + ".pdf"); |
||
| 53 | // gravarArquivo(caminhoArquivo.substring(0, caminhoArquivo.indexOf(".pdf")) + "-" + String.format("%03d", i + 1) + ".pdf", arquivo); |
||
| 54 | // i++; |
||
| 55 | //break; |
||
| 56 | } |
||
| 57 | } |
||
| 58 | |||
| 59 | public static String[] extrairPDFEmLinhas(byte[] arquivo) throws IOException { |
||
| 60 | StringBuilder texto = new StringBuilder(); |
||
| 61 | texto.append(extrairPDF(arquivo)); |
||
| 62 | return texto.toString().split("\n"); |
||
| 63 | } |
||
| 64 | |||
| 65 | public static String extrairPDF(byte[] arquivo) throws IOException { |
||
| 66 | PdfReader reader = new PdfReader(arquivo); |
||
| 67 | String textoExtraido = null; |
||
| 68 | try { |
||
| 69 | PdfReaderContentParser parser = new PdfReaderContentParser(reader); |
||
| 70 | int quantidadePaginas = reader.getNumberOfPages(); |
||
| 71 | TextExtractionStrategy strategy; |
||
| 72 | |||
| 73 | for (int i = 1; i <= quantidadePaginas; i++) { |
||
| 74 | strategy = parser.processContent(i, new SimpleTextExtractionStrategy()); |
||
| 75 | textoExtraido = strategy.getResultantText().toString(); |
||
| 76 | } |
||
| 77 | } catch (Exception e) { |
||
| 78 | e.printStackTrace(); |
||
| 79 | } finally { |
||
| 80 | reader.close(); |
||
| 81 | } |
||
| 82 | return textoExtraido; |
||
| 83 | } |
||
| 84 | |||
| 85 | public static boolean verificarSeTemTexto(String texto, String textoExtraido) { |
||
| 86 | if (VerificadorUtil.naoEstaNuloOuVazio(textoExtraido)) { |
||
| 87 | if (textoExtraido.contains(texto)) { |
||
| 88 | return true; |
||
| 89 | } |
||
| 90 | } |
||
| 91 | return false; |
||
| 92 | } |
||
| 93 | |||
| 94 | public static List<byte[]> separarPDF(byte[] arquivo) throws IOException { |
||
| 95 | return separarPDF(new PdfReader(arquivo)); |
||
| 96 | } |
||
| 97 | |||
| 98 | public static List<byte[]> separarPDF(String caminhoArquivo) throws IOException { |
||
| 99 | return separarPDF(new PdfReader(caminhoArquivo)); |
||
| 100 | } |
||
| 101 | |||
| 102 | private static List<byte[]> separarPDF(PdfReader reader) { |
||
| 103 | List<byte[]> arquivos = new ArrayList<byte[]>(); |
||
| 104 | try { |
||
| 105 | int quantidadePaginas = reader.getNumberOfPages(); |
||
| 106 | int i = 0; |
||
| 107 | while (i < quantidadePaginas) { |
||
| 108 | Document documento = new Document(reader.getPageSizeWithRotation(1)); |
||
| 109 | ByteArrayOutputStream pdfOutputStream = new ByteArrayOutputStream(); |
||
| 110 | PdfCopy writer = new PdfCopy(documento, pdfOutputStream); |
||
| 111 | documento.open(); |
||
| 112 | PdfImportedPage pagina = writer.getImportedPage(reader, ++i); |
||
| 113 | writer.addPage(pagina); |
||
| 114 | documento.close(); |
||
| 115 | writer.close(); |
||
| 116 | |||
| 117 | arquivos.add(pdfOutputStream.toByteArray()); |
||
| 118 | pdfOutputStream.close(); |
||
| 119 | } |
||
| 120 | } catch (Exception e) { |
||
| 121 | e.printStackTrace(); |
||
| 122 | } finally { |
||
| 123 | reader.close(); |
||
| 124 | } |
||
| 125 | return arquivos; |
||
| 126 | } |
||
| 127 | |||
| 128 | public static void gerarPDF(byte[] bytesPDF, String destinoArquivo) { |
||
| 129 | try { |
||
| 130 | PdfReader reader = new PdfReader(bytesPDF); |
||
| 131 | Document documento = new Document(reader.getPageSizeWithRotation(1)); |
||
| 132 | PdfCopy copy = new PdfCopy(documento, new FileOutputStream(destinoArquivo)); |
||
| 133 | documento.open(); |
||
| 134 | PdfImportedPage page = copy.getImportedPage(reader, 1); |
||
| 135 | copy.addPage(page); |
||
| 136 | documento.close(); |
||
| 137 | copy.close(); |
||
| 138 | } catch (Exception e) { |
||
| 139 | e.printStackTrace(); |
||
| 140 | } |
||
| 141 | } |
||
| 142 | |||
| 143 | public static void separarSalvandoArquivoPDF(String caminhoArquivo) { |
||
| 144 | try { |
||
| 145 | PdfReader reader = new PdfReader(caminhoArquivo); |
||
| 146 | int n = reader.getNumberOfPages(); |
||
| 147 | int i = 0; |
||
| 148 | while (i < n) { |
||
| 149 | String destinoArquivo = caminhoArquivo.substring(0, caminhoArquivo.indexOf(".pdf")) + "-" + String.format("%03d", i + 1) + ".pdf"; |
||
| 150 | Document document = new Document(reader.getPageSizeWithRotation(1)); |
||
| 151 | PdfCopy writer = new PdfCopy(document, new FileOutputStream(destinoArquivo)); |
||
| 152 | document.open(); |
||
| 153 | PdfImportedPage page = writer.getImportedPage(reader, ++i); |
||
| 154 | writer.addPage(page); |
||
| 155 | document.close(); |
||
| 156 | writer.close(); |
||
| 157 | } |
||
| 158 | } catch (Exception e) { |
||
| 159 | e.printStackTrace(); |
||
| 160 | } |
||
| 161 | } |
||
| 162 | |||
| 163 | } |