Exemplo n.º 1
0
        public bool GerarBoletos(string nomeArquivo,
                                 bool formatoCarne,
                                 bool ocultarInstrucoes,
                                 bool mostrarDemonstrativo,
                                 bool mostrarComprovanteEntrega,
                                 bool mostrarComprovanteEntregaLivre,
                                 bool ocultarReciboSacado,
                                 bool mostrarEnderecoCedente,
                                 bool ocultarEnderecoSacado,
                                 bool mostrarCodigoCarteira,
                                 bool ocultarLinhaPontilhadaCodigoBarras,
                                 bool adicionarQuebraPagina,
                                 string htmlHeader,
                                 string htmlFooter,
                                 ref string mensagemErro)
        {
            mensagemErro = "";
            try
            {
                if (!setupOk)
                {
                    mensagemErro = "Realize o setup da cobrança antes de executar este método.";
                    return(false);
                }
                if (string.IsNullOrWhiteSpace(nomeArquivo))
                {
                    mensagemErro = "Nome do arquivo não informado." + Environment.NewLine;
                    return(false);
                }
                if (quantidadeBoletos == 0)
                {
                    mensagemErro = "Nenhum boleto encontrado." + Environment.NewLine;
                    return(false);
                }
                var extensaoArquivo = nomeArquivo.Substring(nomeArquivo.Length - 3).ToUpper();
                if (extensaoArquivo != "HTM" && extensaoArquivo != "PDF")
                {
                    mensagemErro = "Tipo do arquivo inválido: HTM ou PDF" + Environment.NewLine;
                    return(false);
                }
                var html = new StringBuilder();
                foreach (Boleto boletoTmp in boletos)
                {
                    using (BoletoBancario imprimeBoleto = new BoletoBancario
                    {
                        Boleto = boletoTmp,
                        FormatoCarne = formatoCarne,
                        OcultarInstrucoes = ocultarInstrucoes,
                        ExibirDemonstrativo = mostrarDemonstrativo,
                        MostrarComprovanteEntrega = mostrarComprovanteEntrega,
                        MostrarComprovanteEntregaLivre = mostrarComprovanteEntregaLivre,
                        MostrarEnderecoCedente = mostrarEnderecoCedente,
                        OcultarEnderecoSacado = ocultarEnderecoSacado,
                        OcultarReciboSacado = ocultarReciboSacado,
                        MostrarCodigoCarteira = mostrarCodigoCarteira,
                        OcultarLinhaPontilhadaCodigoBarras = ocultarLinhaPontilhadaCodigoBarras
                    })
                    {
                        if (adicionarQuebraPagina)
                        {
                            html.Append("<div style=\"page-break-after: always;\">");
                        }
                        if (htmlHeader != "")
                        {
                            html.Append(htmlHeader);
                        }
                        html.Append(imprimeBoleto.MontaHtml());
                        if (htmlFooter != "")
                        {
                            html.Append(htmlFooter);
                        }
                        if (adicionarQuebraPagina)
                        {
                            html.Append("</div>");
                        }
                    }
                }
                switch (extensaoArquivo.ToUpper())
                {
                case "HTM":
                    GerarArquivoTexto(html.ToString(), nomeArquivo);
                    break;

                case "PDF":
                    GerarArquivoPDF(html.ToString(), nomeArquivo);
                    break;

                default:
                    break;
                }
                return(true);
            }
            catch (Exception ex)
            {
                while (ex != null)
                {
                    mensagemErro += ex.Message + Environment.NewLine;
                    ex            = ex.InnerException;
                }
                return(false);
            }
        }
Exemplo n.º 2
0
        public bool GerarBoletos(string nomeArquivo, ref string mensagemErro)
        {
            mensagemErro = "";
            try
            {
                if (!setupOk)
                {
                    mensagemErro = "Realize o setup da cobrança antes de executar este método.";
                    return(false);
                }
                if (string.IsNullOrWhiteSpace(nomeArquivo))
                {
                    mensagemErro = "Nome do arquivo não informado." + Environment.NewLine;
                    return(false);
                }
                if (quantidadeBoletos == 0)
                {
                    mensagemErro = "Nenhum boleto encontrado." + Environment.NewLine;
                    return(false);
                }
                var extensaoArquivo = nomeArquivo.Substring(nomeArquivo.Length - 3).ToUpper();
                if (extensaoArquivo != "HTM" && extensaoArquivo != "PDF")
                {
                    mensagemErro = "Tipo do arquivo inválido: HTM ou PDF" + Environment.NewLine;
                    return(false);
                }
                var html = new StringBuilder();
                foreach (Boleto boletoTmp in boletos)
                {
                    //if (html.Length != 0)
                    //{
                    //    // Já existe um boleto, inclui quebra de linha.
                    //    html.Append("</br></br></br></br></br></br></br></br></br></br>");
                    //}
                    using (BoletoBancario imprimeBoleto = new BoletoBancario
                    {
                        //CodigoBanco = (short)boletoTmp.Banco.Codigo,
                        Boleto = boletoTmp,
                        OcultarInstrucoes = false,
                        MostrarComprovanteEntrega = true,
                        MostrarEnderecoCedente = true
                    })
                    {
                        html.Append("<div style=\"page-break-after: always;\">");
                        html.Append(imprimeBoleto.MontaHtml());
                        html.Append("</div>");
                    }
                }
                switch (extensaoArquivo.ToUpper())
                {
                case "HTM":
                    GerarArquivoTexto(html.ToString(), nomeArquivo);
                    break;

                case "PDF":
                    GerarArquivoPDF(html.ToString(), nomeArquivo);
                    break;

                default:
                    break;
                }
                return(true);
            }
            catch (Exception ex)
            {
                while (ex != null)
                {
                    mensagemErro += ex.Message + Environment.NewLine;
                    ex            = ex.InnerException;
                }
                return(false);
            }
        }