Rev 182 | Details | Compare with Previous | Last modification | View Log | RSS feed
| Rev | Author | Line No. | Line |
|---|---|---|---|
| 179 | espaco | 1 | package br.com.ec.controller.util; |
| 2 | |||
| 3 | import java.io.ByteArrayOutputStream; |
||
| 4 | import java.io.File; |
||
| 5 | import java.io.FileInputStream; |
||
| 6 | import java.io.FileNotFoundException; |
||
| 7 | import java.io.FileOutputStream; |
||
| 8 | import java.io.IOException; |
||
| 9 | import java.util.ArrayList; |
||
| 10 | import java.util.Iterator; |
||
| 11 | import java.util.List; |
||
| 12 | |||
| 13 | import org.apache.poi.hssf.usermodel.HSSFDataFormat; |
||
| 14 | import org.apache.poi.hssf.usermodel.HSSFSheet; |
||
| 15 | import org.apache.poi.hssf.usermodel.HSSFWorkbook; |
||
| 16 | import org.apache.poi.ss.usermodel.Cell; |
||
| 17 | import org.apache.poi.ss.usermodel.CellStyle; |
||
| 18 | import org.apache.poi.ss.usermodel.FillPatternType; |
||
| 19 | import org.apache.poi.ss.usermodel.HorizontalAlignment; |
||
| 20 | import org.apache.poi.ss.usermodel.IndexedColors; |
||
| 21 | import org.apache.poi.ss.usermodel.Row; |
||
| 22 | import org.apache.poi.ss.usermodel.VerticalAlignment; |
||
| 23 | |||
| 24 | import com.itextpdf.text.Document; |
||
| 25 | import com.itextpdf.text.pdf.PdfCopy; |
||
| 26 | import com.itextpdf.text.pdf.PdfImportedPage; |
||
| 27 | import com.itextpdf.text.pdf.PdfReader; |
||
| 28 | import com.itextpdf.text.pdf.parser.PdfReaderContentParser; |
||
| 29 | import com.itextpdf.text.pdf.parser.SimpleTextExtractionStrategy; |
||
| 30 | import com.itextpdf.text.pdf.parser.TextExtractionStrategy; |
||
| 31 | |||
| 32 | import br.com.ec.core.util.VerificadorUtil; |
||
| 33 | |||
| 34 | public class ExcelUtil { |
||
| 35 | |||
| 36 | public static HSSFSheet criarAbas(HSSFWorkbook workbook, String nomeAba) { |
||
| 37 | HSSFSheet aba = workbook.createSheet(nomeAba); |
||
| 38 | return aba; |
||
| 39 | } |
||
| 40 | |||
| 41 | public static void definirPadraoAba(HSSFSheet aba, Integer larguraColunas, short alturaLinhas) { |
||
| 42 | aba.setDefaultColumnWidth(larguraColunas);//15 |
||
| 43 | aba.setDefaultRowHeight((short)alturaLinhas);//400 |
||
| 44 | } |
||
| 45 | |||
| 46 | public static CellStyle configurarCelulaCabecalho(HSSFWorkbook workbook) { |
||
| 47 | CellStyle headerStyle = workbook.createCellStyle(); |
||
| 48 | headerStyle.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex()); |
||
| 49 | headerStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND); |
||
| 50 | headerStyle.setAlignment(HorizontalAlignment.CENTER); |
||
| 51 | headerStyle.setVerticalAlignment(VerticalAlignment.CENTER); |
||
| 52 | return headerStyle; |
||
| 53 | } |
||
| 54 | |||
| 55 | public static CellStyle configurarCelulaTexto(HSSFWorkbook workbook) { |
||
| 56 | CellStyle textStyle = workbook.createCellStyle(); |
||
| 57 | textStyle.setAlignment(HorizontalAlignment.CENTER); |
||
| 58 | textStyle.setVerticalAlignment(VerticalAlignment.CENTER); |
||
| 59 | return textStyle; |
||
| 60 | } |
||
| 61 | |||
| 62 | public static CellStyle configurarCelulaNumerico(HSSFWorkbook workbook) { |
||
| 63 | CellStyle numberStyle = workbook.createCellStyle(); |
||
| 64 | //Configurando estilos de células (Cores, alinhamento, formatação, etc..) |
||
| 65 | HSSFDataFormat numberFormat = workbook.createDataFormat(); |
||
| 66 | numberStyle.setDataFormat(numberFormat.getFormat("#,##0.00")); |
||
| 67 | numberStyle.setVerticalAlignment(VerticalAlignment.CENTER); |
||
| 68 | return numberStyle; |
||
| 69 | } |
||
| 70 | |||
| 71 | public static void configurarCabecalho(HSSFWorkbook workbook, HSSFSheet aba) { |
||
| 72 | CellStyle headerStyle = configurarCelulaCabecalho(workbook); |
||
| 73 | Row linha = aba.createRow(0); |
||
| 74 | criarCelula(workbook, aba, linha, 0, headerStyle, "CÓDIGO"); |
||
| 75 | // criarCelula(workbook, aba, linha, 1, headerStyle, "NOME"); |
||
| 76 | // criarCelula(workbook, aba, linha, 2, headerStyle, "PREÇO"); |
||
| 77 | } |
||
| 78 | |||
| 79 | public static void criarCelula(HSSFWorkbook workbook, HSSFSheet aba, Row linha, Integer posicaoColuna, CellStyle estilo, String valor) { |
||
| 80 | Cell celula = linha.createCell(posicaoColuna); |
||
| 81 | if (VerificadorUtil.naoEstaNulo(estilo)) { |
||
| 82 | celula.setCellStyle(estilo); |
||
| 83 | } |
||
| 84 | celula.setCellValue(valor); |
||
| 85 | } |
||
| 86 | |||
| 87 | public static void main2(String[] args) throws IOException { |
||
| 88 | HSSFWorkbook wb = new HSSFWorkbook(); |
||
| 89 | |||
| 90 | // ABAS |
||
| 91 | HSSFSheet aba1 = criarAbas(wb, "Planilha 1"); |
||
| 92 | criarAbas(wb, "Planilha 2"); |
||
| 93 | criarAbas(wb, "Planilha 3"); |
||
| 94 | |||
| 95 | configurarCabecalho(wb, aba1); |
||
| 96 | |||
| 97 | Row linha = aba1.createRow(1); |
||
| 98 | criarCelula(wb, aba1, linha, 0, configurarCelulaTexto(wb), "1"); |
||
| 99 | criarCelula(wb, aba1, linha, 1, configurarCelulaTexto(wb), "TESTE"); |
||
| 100 | criarCelula(wb, aba1, linha, 2, configurarCelulaNumerico(wb), "10"); |
||
| 101 | |||
| 102 | try { |
||
| 103 | FileOutputStream stream = new FileOutputStream("d:/planilha.xls"); |
||
| 104 | wb.write(stream); |
||
| 105 | stream.close(); |
||
| 106 | wb.close(); |
||
| 107 | System.out.println("Success!!"); |
||
| 108 | } catch (FileNotFoundException e) { |
||
| 109 | e.printStackTrace(); |
||
| 110 | } catch (IOException e) { |
||
| 111 | e.printStackTrace(); |
||
| 112 | } |
||
| 113 | } |
||
| 114 | |||
| 115 | public static void main3(String[] args) throws IOException { |
||
| 116 | String filePath = "d:/planilha.xls"; |
||
| 117 | try{ |
||
| 118 | // Abrindo o arquivo e recuperando a planilha |
||
| 119 | FileInputStream file = new FileInputStream(new File(filePath)); |
||
| 120 | HSSFWorkbook workbook = new HSSFWorkbook(file); |
||
| 121 | HSSFSheet sheet = workbook.getSheetAt(0); |
||
| 122 | |||
| 123 | List products = new ArrayList(); |
||
| 124 | |||
| 125 | Iterator rowIterator = sheet.rowIterator(); |
||
| 126 | while (rowIterator.hasNext()) { |
||
| 127 | Row row = (Row) rowIterator.next(); |
||
| 128 | |||
| 129 | // Descantando a primeira linha com o header |
||
| 130 | if(row.getRowNum() == 0){ |
||
| 131 | continue; |
||
| 132 | } |
||
| 133 | |||
| 134 | Iterator cellIterator = row.cellIterator(); |
||
| 135 | // Product product = new Product(); |
||
| 136 | // products.add(product); |
||
| 137 | while (cellIterator.hasNext()) { |
||
| 138 | Cell cell = (Cell) cellIterator.next(); |
||
| 139 | switch (cell.getColumnIndex()) { |
||
| 140 | case 0: |
||
| 141 | // product.setId(((Double)cell.getNumericCellValue()).longValue()); |
||
| 142 | cell.setCellValue("2");// Substituindo valores |
||
| 143 | System.out.println(cell.getStringCellValue()); |
||
| 144 | break; |
||
| 145 | case 1: |
||
| 146 | System.out.println(cell.getStringCellValue()); |
||
| 147 | break; |
||
| 148 | case 2: |
||
| 149 | System.out.println(cell.getNumericCellValue()); |
||
| 150 | break; |
||
| 151 | } |
||
| 152 | } |
||
| 153 | } |
||
| 154 | |||
| 155 | // Reescrever |
||
| 156 | try { |
||
| 157 | FileOutputStream stream = new FileOutputStream("d:/planilha.xls"); |
||
| 158 | workbook.write(stream); |
||
| 159 | stream.close(); |
||
| 160 | workbook.close(); |
||
| 161 | System.out.println("Success!!"); |
||
| 162 | } catch (FileNotFoundException e) { |
||
| 163 | e.printStackTrace(); |
||
| 164 | } catch (IOException e) { |
||
| 165 | e.printStackTrace(); |
||
| 166 | } |
||
| 167 | |||
| 168 | file.close(); |
||
| 169 | workbook.close(); |
||
| 170 | } catch (FileNotFoundException e) { |
||
| 171 | e.printStackTrace(); |
||
| 172 | } |
||
| 173 | } |
||
| 174 | |||
| 175 | /* |
||
| 176 | public static void main(String[] args) throws IOException { |
||
| 177 | Avaliacao avaliacao = new Avaliacao(); |
||
| 178 | Vigencia vigencia = new Vigencia(); |
||
| 179 | vigencia.setDescricao("11/2020"); |
||
| 180 | avaliacao.setVigencia(vigencia); |
||
| 181 | List<AvaliacaoFuncionario> participantes = new ArrayList<AvaliacaoFuncionario>(); |
||
| 182 | AvaliacaoFuncionario avaliacaoFuncionario = new AvaliacaoFuncionario(); |
||
| 183 | Funcionario funcionario = new Funcionario(); |
||
| 184 | Pessoa pessoa = new Pessoa(); |
||
| 185 | pessoa.setNome("ADALBERTO SOARES BRITO NETO"); |
||
| 186 | funcionario.setPessoa(pessoa); |
||
| 187 | funcionario.setCodigoContabilidade(48); |
||
| 188 | avaliacaoFuncionario.setFuncionario(funcionario); |
||
| 189 | avaliacaoFuncionario.setValorComissao(new Double(50.0)); |
||
| 190 | participantes.add(avaliacaoFuncionario); |
||
| 191 | avaliacao.setParticipantes(new HashSet<AvaliacaoFuncionario>()); |
||
| 192 | avaliacao.getParticipantes().add(avaliacaoFuncionario); |
||
| 193 | |||
| 194 | String filePath = "d:/importacao.xls"; |
||
| 195 | try { |
||
| 196 | FileInputStream file = new FileInputStream(new File(filePath)); |
||
| 197 | HSSFWorkbook workbook = new HSSFWorkbook(file); |
||
| 198 | HSSFSheet sheet = workbook.getSheetAt(0); |
||
| 199 | |||
| 200 | Iterator rowIterator = sheet.rowIterator(); |
||
| 201 | while (rowIterator.hasNext()) { |
||
| 202 | Row row = (Row) rowIterator.next(); |
||
| 203 | Iterator cellIterator = row.cellIterator(); |
||
| 204 | if (row.getRowNum() < 11) { |
||
| 205 | if (row.getRowNum() == 4) { |
||
| 206 | while (cellIterator.hasNext()) { |
||
| 207 | Cell cell = (Cell) cellIterator.next(); |
||
| 208 | switch (cell.getColumnIndex()) { |
||
| 209 | case 2: |
||
| 210 | cell.setCellValue(vigencia.getDescricao()); |
||
| 211 | System.out.println(cell.getRichStringCellValue()); break; |
||
| 212 | } |
||
| 213 | } |
||
| 214 | } |
||
| 215 | continue; |
||
| 216 | } |
||
| 217 | Integer codigoFolha = null; |
||
| 218 | while (cellIterator.hasNext()) { |
||
| 219 | Boolean atualizar = true; |
||
| 220 | Cell cell = (Cell) cellIterator.next(); |
||
| 221 | switch (cell.getColumnIndex()) { |
||
| 222 | case 1: |
||
| 223 | Integer tipoCelula = cell.getCellType(); |
||
| 224 | if (tipoCelula.equals(Cell.CELL_TYPE_NUMERIC)) { |
||
| 225 | System.out.println(cell.getNumericCellValue()); |
||
| 226 | Double codigo = cell.getNumericCellValue(); |
||
| 227 | codigoFolha = codigo.intValue(); |
||
| 228 | } else { |
||
| 229 | atualizar = false; |
||
| 230 | } |
||
| 231 | break; |
||
| 232 | case 2: |
||
| 233 | if (atualizar) { |
||
| 234 | System.out.println(cell.getStringCellValue()); break; |
||
| 235 | } |
||
| 236 | case 3: |
||
| 237 | if (atualizar) { |
||
| 238 | for (AvaliacaoFuncionario avaliacaoFunc : avaliacao.getParticipantes()) { |
||
| 239 | if (avaliacaoFunc.getFuncionario().getCodigoContabilidade().equals(codigoFolha)) { |
||
| 240 | String valor = StringUtil.formatarValorComDoisDigitos(avaliacaoFunc.getValorComissao().toString()); |
||
| 241 | cell.setCellValue(valor.replace(".", ",")); |
||
| 242 | break; |
||
| 243 | } |
||
| 244 | } |
||
| 245 | System.out.println(cell.getStringCellValue()); |
||
| 246 | } |
||
| 247 | break; |
||
| 248 | } |
||
| 249 | } |
||
| 250 | } |
||
| 251 | |||
| 252 | try { |
||
| 253 | FileOutputStream stream = new FileOutputStream("d:/importacao.xls"); |
||
| 254 | workbook.write(stream); |
||
| 255 | stream.close(); |
||
| 256 | workbook.close(); |
||
| 257 | System.out.println("Success!!"); |
||
| 258 | } catch (FileNotFoundException e) { |
||
| 259 | e.printStackTrace(); |
||
| 260 | } catch (IOException e) { |
||
| 261 | e.printStackTrace(); |
||
| 262 | } |
||
| 263 | |||
| 264 | file.close(); |
||
| 265 | workbook.close(); |
||
| 266 | } catch (FileNotFoundException e) { |
||
| 267 | e.printStackTrace(); |
||
| 268 | } catch (Exception e) { |
||
| 269 | e.printStackTrace(); |
||
| 270 | } |
||
| 271 | } |
||
| 272 | */ |
||
| 273 | |||
| 274 | /* |
||
| 275 | public static void main( String[] args ) |
||
| 276 | { |
||
| 277 | // Criando o arquivo e uma planilha chamada "Product" |
||
| 278 | HSSFWorkbook workbook = new HSSFWorkbook(); |
||
| 279 | HSSFSheet sheet = workbook.createSheet("Product"); |
||
| 280 | |||
| 281 | // Definindo alguns padroes de layout |
||
| 282 | sheet.setDefaultColumnWidth(15); |
||
| 283 | sheet.setDefaultRowHeight((short)400); |
||
| 284 | |||
| 285 | //Carregando os produtos |
||
| 286 | List products = getProducts(); |
||
| 287 | |||
| 288 | int rownum = 0; |
||
| 289 | int cellnum = 0; |
||
| 290 | Cell cell; |
||
| 291 | Row row; |
||
| 292 | |||
| 293 | //Configurando estilos de células (Cores, alinhamento, formatação, etc..) |
||
| 294 | HSSFDataFormat numberFormat = workbook.createDataFormat(); |
||
| 295 | |||
| 296 | CellStyle headerStyle = workbook.createCellStyle(); |
||
| 297 | headerStyle.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex()); |
||
| 298 | headerStyle.setFillPattern(CellStyle.SOLID_FOREGROUND); |
||
| 299 | headerStyle.setAlignment(CellStyle.ALIGN_CENTER); |
||
| 300 | headerStyle.setVerticalAlignment(CellStyle.VERTICAL_CENTER); |
||
| 301 | |||
| 302 | CellStyle textStyle = workbook.createCellStyle(); |
||
| 303 | textStyle.setAlignment(CellStyle.ALIGN_CENTER); |
||
| 304 | textStyle.setVerticalAlignment(CellStyle.VERTICAL_CENTER); |
||
| 305 | |||
| 306 | CellStyle numberStyle = workbook.createCellStyle(); |
||
| 307 | numberStyle.setDataFormat(numberFormat.getFormat("#,##0.00")); |
||
| 308 | numberStyle.setVerticalAlignment(CellStyle.VERTICAL_CENTER); |
||
| 309 | |||
| 310 | // Configurando Header |
||
| 311 | row = sheet.createRow(rownum++); |
||
| 312 | cell = row.createCell(cellnum++); |
||
| 313 | cell.setCellStyle(headerStyle); |
||
| 314 | cell.setCellValue("Code"); |
||
| 315 | |||
| 316 | cell = row.createCell(cellnum++); |
||
| 317 | cell.setCellStyle(headerStyle); |
||
| 318 | cell.setCellValue("Name"); |
||
| 319 | |||
| 320 | cell = row.createCell(cellnum++); |
||
| 321 | cell.setCellStyle(headerStyle); |
||
| 322 | cell.setCellValue("Price"); |
||
| 323 | |||
| 324 | // Adicionando os dados dos produtos na planilha |
||
| 325 | for (Product product : products) { |
||
| 326 | row = sheet.createRow(rownum++); |
||
| 327 | cellnum = 0; |
||
| 328 | |||
| 329 | cell = row.createCell(cellnum++); |
||
| 330 | cell.setCellStyle(textStyle); |
||
| 331 | cell.setCellValue(product.getId()); |
||
| 332 | |||
| 333 | cell = row.createCell(cellnum++); |
||
| 334 | cell.setCellStyle(textStyle); |
||
| 335 | cell.setCellValue(product.getName()); |
||
| 336 | |||
| 337 | cell = row.createCell(cellnum++); |
||
| 338 | cell.setCellStyle(numberStyle); |
||
| 339 | cell.setCellValue(product.getPrice()); |
||
| 340 | } |
||
| 341 | |||
| 342 | try { |
||
| 343 | |||
| 344 | //Escrevendo o arquivo em disco |
||
| 345 | FileOutputStream out = new FileOutputStream(new File("/tmp/products.xls")); |
||
| 346 | workbook.write(out); |
||
| 347 | out.close(); |
||
| 348 | workbook.close(); |
||
| 349 | System.out.println("Success!!"); |
||
| 350 | |||
| 351 | } catch (FileNotFoundException e) { |
||
| 352 | e.printStackTrace(); |
||
| 353 | } catch (IOException e) { |
||
| 354 | e.printStackTrace(); |
||
| 355 | } |
||
| 356 | } |
||
| 357 | } |
||
| 358 | |||
| 359 | //Simulando uma listagem de produtos |
||
| 360 | private static List getProducts(){ |
||
| 361 | List products = new ArrayList(); |
||
| 362 | |||
| 363 | products.add(new Product(1l, "Produto 1", 200.5d)); |
||
| 364 | products.add(new Product(2l, "Produto 2", 1050.5d)); |
||
| 365 | products.add(new Product(3l, "Produto 3", 50d)); |
||
| 366 | products.add(new Product(4l, "Produto 4", 200d)); |
||
| 367 | products.add(new Product(5l, "Produto 5", 450d)); |
||
| 368 | products.add(new Product(6l, "Produto 6", 150.5d)); |
||
| 369 | products.add(new Product(7l, "Produto 7", 300.99d)); |
||
| 370 | products.add(new Product(8l, "Produto 8", 1000d)); |
||
| 371 | products.add(new Product(9l, "Produto 9", 350d)); |
||
| 372 | products.add(new Product(10l, "Produto 10", 200d)); |
||
| 373 | |||
| 374 | return products; |
||
| 375 | } |
||
| 376 | */ |
||
| 377 | |||
| 378 | /* |
||
| 379 | public static void main(String[] args) throws IOException, DocumentException { |
||
| 380 | String caminhoArquivo = "H://recibo.pdf"; |
||
| 381 | List<byte[]> arquivos = separarPDF(caminhoArquivo); |
||
| 382 | int i = 1; |
||
| 383 | for (byte[] arquivo : arquivos) { |
||
| 384 | // System.out.println(extrairPDF(arquivo)); |
||
| 385 | String[] linhas = extrairPDFEmLinhas(arquivo); |
||
| 386 | |||
| 387 | boolean capturar = false; |
||
| 388 | for (String linha : linhas) { |
||
| 389 | if (capturar) { |
||
| 390 | System.out.println(linha); |
||
| 391 | capturar = false; |
||
| 392 | } |
||
| 393 | if (linha.contains("CC:")) { |
||
| 394 | capturar = true; |
||
| 395 | } |
||
| 396 | } |
||
| 397 | |||
| 398 | System.out.println(caminhoArquivo.substring(0, caminhoArquivo.indexOf(".pdf")) + "-" + String.format("%03d", i + 1) + ".pdf"); |
||
| 399 | // gravarArquivo(caminhoArquivo.substring(0, caminhoArquivo.indexOf(".pdf")) + "-" + String.format("%03d", i + 1) + ".pdf", arquivo); |
||
| 400 | i++; |
||
| 401 | //break; |
||
| 402 | } |
||
| 403 | } |
||
| 404 | */ |
||
| 405 | public static void gravarArquivo(String caminhoArquivo, byte[] arquivo) { |
||
| 406 | try { |
||
| 407 | FileOutputStream fos = new FileOutputStream(caminhoArquivo); |
||
| 408 | fos.write(arquivo); |
||
| 409 | fos.flush(); |
||
| 410 | fos.close(); |
||
| 411 | } catch (Exception e) { |
||
| 412 | e.printStackTrace(); |
||
| 413 | } |
||
| 414 | } |
||
| 415 | |||
| 416 | public static String[] extrairPDFEmLinhas(byte[] arquivo) throws IOException { |
||
| 417 | StringBuilder texto = new StringBuilder(); |
||
| 418 | texto.append(extrairPDF(arquivo)); |
||
| 419 | return texto.toString().split("\n"); |
||
| 420 | } |
||
| 421 | |||
| 422 | public static String extrairPDF(byte[] arquivo) throws IOException { |
||
| 423 | PdfReader reader = new PdfReader(arquivo); |
||
| 424 | String textoExtraido = null; |
||
| 425 | try { |
||
| 426 | PdfReaderContentParser parser = new PdfReaderContentParser(reader); |
||
| 427 | int quantidadePaginas = reader.getNumberOfPages(); |
||
| 428 | TextExtractionStrategy strategy; |
||
| 429 | |||
| 430 | for (int i = 1; i <= quantidadePaginas; i++) { |
||
| 431 | strategy = parser.processContent(i, new SimpleTextExtractionStrategy()); |
||
| 432 | textoExtraido = strategy.getResultantText().toString(); |
||
| 433 | } |
||
| 434 | } catch (Exception e) { |
||
| 435 | e.printStackTrace(); |
||
| 436 | } finally { |
||
| 437 | reader.close(); |
||
| 438 | } |
||
| 439 | return textoExtraido; |
||
| 440 | } |
||
| 441 | |||
| 442 | public static boolean verificarSeTemTexto(String texto, String textoExtraido) { |
||
| 443 | if (VerificadorUtil.naoEstaNuloOuVazio(textoExtraido)) { |
||
| 444 | if (textoExtraido.contains(texto)) { |
||
| 445 | return true; |
||
| 446 | } |
||
| 447 | } |
||
| 448 | return false; |
||
| 449 | } |
||
| 450 | |||
| 451 | public static List<byte[]> separarPDF(byte[] arquivo) throws IOException { |
||
| 452 | return separarPDF(new PdfReader(arquivo)); |
||
| 453 | } |
||
| 454 | |||
| 455 | public static List<byte[]> separarPDF(String caminhoArquivo) throws IOException { |
||
| 456 | return separarPDF(new PdfReader(caminhoArquivo)); |
||
| 457 | } |
||
| 458 | |||
| 459 | private static List<byte[]> separarPDF(PdfReader reader) { |
||
| 460 | List<byte[]> arquivos = new ArrayList<byte[]>(); |
||
| 461 | try { |
||
| 462 | int quantidadePaginas = reader.getNumberOfPages(); |
||
| 463 | int i = 0; |
||
| 464 | while (i < quantidadePaginas) { |
||
| 465 | Document documento = new Document(reader.getPageSizeWithRotation(1)); |
||
| 466 | ByteArrayOutputStream pdfOutputStream = new ByteArrayOutputStream(); |
||
| 467 | PdfCopy writer = new PdfCopy(documento, pdfOutputStream); |
||
| 468 | documento.open(); |
||
| 469 | PdfImportedPage pagina = writer.getImportedPage(reader, ++i); |
||
| 470 | writer.addPage(pagina); |
||
| 471 | documento.close(); |
||
| 472 | writer.close(); |
||
| 473 | |||
| 474 | arquivos.add(pdfOutputStream.toByteArray()); |
||
| 475 | pdfOutputStream.close(); |
||
| 476 | } |
||
| 477 | } catch (Exception e) { |
||
| 478 | e.printStackTrace(); |
||
| 479 | } finally { |
||
| 480 | reader.close(); |
||
| 481 | } |
||
| 482 | return arquivos; |
||
| 483 | } |
||
| 484 | |||
| 485 | public static void gerarPDF(byte[] bytesPDF, String destinoArquivo) { |
||
| 486 | try { |
||
| 487 | PdfReader reader = new PdfReader(bytesPDF); |
||
| 488 | Document documento = new Document(reader.getPageSizeWithRotation(1)); |
||
| 489 | PdfCopy copy = new PdfCopy(documento, new FileOutputStream(destinoArquivo)); |
||
| 490 | documento.open(); |
||
| 491 | PdfImportedPage page = copy.getImportedPage(reader, 1); |
||
| 492 | copy.addPage(page); |
||
| 493 | documento.close(); |
||
| 494 | copy.close(); |
||
| 495 | } catch (Exception e) { |
||
| 496 | e.printStackTrace(); |
||
| 497 | } |
||
| 498 | } |
||
| 499 | |||
| 500 | public static void separarSalvandoArquivoPDF(String caminhoArquivo) { |
||
| 501 | try { |
||
| 502 | PdfReader reader = new PdfReader(caminhoArquivo); |
||
| 503 | int n = reader.getNumberOfPages(); |
||
| 504 | int i = 0; |
||
| 505 | while (i < n) { |
||
| 506 | String destinoArquivo = caminhoArquivo.substring(0, caminhoArquivo.indexOf(".pdf")) + "-" + String.format("%03d", i + 1) + ".pdf"; |
||
| 507 | Document document = new Document(reader.getPageSizeWithRotation(1)); |
||
| 508 | PdfCopy writer = new PdfCopy(document, new FileOutputStream(destinoArquivo)); |
||
| 509 | document.open(); |
||
| 510 | PdfImportedPage page = writer.getImportedPage(reader, ++i); |
||
| 511 | writer.addPage(page); |
||
| 512 | document.close(); |
||
| 513 | writer.close(); |
||
| 514 | } |
||
| 515 | } catch (Exception e) { |
||
| 516 | e.printStackTrace(); |
||
| 517 | } |
||
| 518 | } |
||
| 519 | |||
| 520 | } |