Então .. eu montei um codigo de exemplo para gerar os boletos ...
Porém, não consigo gerar ele em carnê, ele é gerado Normal ...
E tb não estou conseguindo usar essa linha: "File tamplatePersonalizado = new File(ClassLoader.getSystemResource("/resources/boleto/Carne3PorPagina.pdf").getFile());"
qdo descomento ela, acaba dando um erro do NullPointerException
public void geraBoleto(List<TituloTeste> titulos/*, Empresa empresa*/){
System.out.println("Gerando B");
List<Boleto> boletos = new ArrayList<Boleto>();
for(TituloTeste titulo : titulos){
System.out.println("Gerando B tit: "+titulo.getNumTit());
System.out.println("cedente");
Cedente cedente = new Cedente("Sport Academia", "00.000.208/0001-00");/*empresa.getNomeFantasia(), empresa.getCnpj()*/
System.out.println("sacado");
Sacado sacado = new Sacado("Diego Adriano","222.222.222-22");
System.out.println("endereco");
Endereco endereco = new Endereco();
endereco.setLogradouro("RUA SAO MAETHEUS");
endereco.setLocalidade("EXTREMA");
endereco.setCep("37640-000");
endereco.setBairro("CENTRO");
endereco.setNumero("70");
System.out.println("add endereco");
sacado.addEndereco(endereco);
System.out.println("conta b");
ContaBancaria contaBancaria = new ContaBancaria(BancosSuportados.CAIXA_ECONOMICA_FEDERAL.create());
contaBancaria.setNumeroDaConta(new NumeroDaConta(20151,"1"));
contaBancaria.setCarteira(new Carteira(8));
contaBancaria.setAgencia(new Agencia(2051, "6"));
System.out.println("titulo boleto");
Titulo titBoleto = new Titulo(contaBancaria, sacado, cedente);
titBoleto.setNumeroDoDocumento(titulo.getNumTit());
titBoleto.setNossoNumero("32345678901234");
titBoleto.setDigitoDoNossoNumero("123");
titBoleto.setValor(new BigDecimal(titulo.getVlrTit()));
titBoleto.setDataDoDocumento(new Date());
titBoleto.setDataDoVencimento(new Date());
titBoleto.setTipoDeDocumento(TipoDeTitulo.DM_DUPLICATA_MERCANTIL);
titBoleto.setAceite(Aceite.N);
titBoleto.setDesconto(BigDecimal.ZERO);
titBoleto.setDeducao(BigDecimal.ZERO);
titBoleto.setMora(BigDecimal.ZERO);
titBoleto.setAcrecimo(BigDecimal.ZERO);
titBoleto.setValorCobrado(BigDecimal.ZERO);
System.out.println("new boleto");
Boleto boleto = new Boleto(titBoleto);
System.out.println("boleto pgto");
boleto.setLocalPagamento("Pagável preferencialmente na Caixa Econômica ou em qualquer Banco até o Vencimento.");
boleto.setInstrucao4("PARA PAGAMENTO 4 até 04/xx/xxxx de 4 dias atrás COBRAR O VALOR DE: R$ 01,00");
boleto.setInstrucao5("PARA PAGAMENTO 5 até 05/xx/xxxx COBRAR O VALOR DE: R$ 02,00");
boleto.setInstrucao8("NÃO RECEBER APÓS 15 DIAS DO VENCIMENTO.");
System.out.println("add boletos");
boletos.add(boleto);
}
//imprime(boletos);
mostraNaTela(boletos);
}
public void mostraNaTela(List<Boleto> boletos){
System.out.println("Mostra");//groupInOnePDF(boletos, "null");
try {
//INFORMANDO O TAMPLATE PERSONALIZADO new File(ClassLoader.getSystemResource("/boleto/BoletoCarne3PorPagina.pdf").getFile());
//File tamplatePersonalizado = new File(ClassLoader.getSystemResource("/resources/boleto/Carne3PorPagina.pdf").getFile());
System.out.println("CARREGOU BOLETO");
File boletosPorPagina = groupInPages(boletos, "Carne3PorPagina.pdf"/*, tamplatePersonalizado*/);
System.out.println("ABRINDO");
// Descomente se estiver usando java 6 ou superior
java.awt.Desktop.getDesktop().open(boletosPorPagina);
} catch (Exception e) {
System.err.println("Erro ao Mostrar: "+e.getMessage());
}
}
public static File groupInPages(List<Boleto> boletos, String filePath/*, File tamplatePersonalizado*/){
System.out.println("GIP");
File arq = null;
BoletoViewer boletoViewer = new BoletoViewer(boletos.get(0));
//boletoViewer.setTemplate(tamplatePersonalizado);
List<byte[]> boletosEmBytes = new ArrayList<byte[]>(boletos.size());
for(Boleto boleto : boletos){System.out.println("FOR");
boletosEmBytes.add(boletoViewer.setBoleto(boleto).getPdfAsByteArray());
}
try {
System.out.println("TRY ARQ");
arq = org.jrimum.bopepo.pdf.Files.bytesToFile(filePath, mergeFilesInPages(boletosEmBytes));
} catch (Exception e) {
System.err.println("Erro ao Gerar Boleto: "+e.getLocalizedMessage());
}
return arq;
}
public static byte[] mergeFilesInPages(List<byte[]> pdfFilesAsByteArray ) throws DocumentException, IOException, com.lowagie.text.DocumentException{
System.out.println("MFINP");
Document document = new Document();
ByteArrayOutputStream byteOS = new ByteArrayOutputStream();
PdfWriter pdfWriter = PdfWriter.getInstance(document, byteOS);
document.open();
PdfContentByte cb = pdfWriter.getDirectContent();
//float positionAnterior = 0;
float pagePosition = 0;
for (byte[] in : pdfFilesAsByteArray) {
PdfReader reader = new PdfReader(in);
for (int i = 1; i <= reader.getNumberOfPages(); i++) {
float documentHeight = cb.getPdfDocument().getPageSize().getHeight();
//import the page from source pdf
PdfImportedPage page = pdfWriter.getImportedPage(reader, i);
pagePosition =+ page.getHeight();//positionAnterior;
if ( (documentHeight - pagePosition) <= 0 /*page.getHeight()*/) {
document.newPage();
pagePosition = page.getHeight();
//pagePosition = 0;
//positionAnterior = 0;
}
//add the page to the destination pdf
cb.addTemplate(page, 0, documentHeight-pagePosition/*pagePosition*/);
//positionAnterior += page.getHeight();
}
}
byteOS.flush();
document.close();
byte[] arquivoEmBytes = byteOS.toByteArray();
byteOS.close();
return arquivoEmBytes;
}