Example #1
0
        /// <summary>
        /// Obtem o caminho do PDF a partir do cadastro do faturamento no banco de dados, e se nao preenchido, faz download e update no campo
        /// </summary>
        /// <param name="aintTipo">1 = caminho local (c:\...) | 2 = Url completo (http://...) | 3 = Url relativa</param>
        /// <returns></returns>
        public string getCaminhoPdfNf(int aintTipo)
        {
            string lstrRet = this.CaminhoPdfNf;

            if (string.IsNullOrEmpty(lstrRet))
            {
                if (EmpresaEmissao == null)
                {
                    EmpresaEmissao = new Empresa();
                }

                if (EmpresaEmissaoID > 0 && EmpresaEmissao.ID == 0)
                {
                    EmpresaEmissao.ID = EmpresaEmissaoID;
                }

                if (EmpresaEmissao.ID > 0 && string.IsNullOrEmpty(EmpresaEmissao.CcmPmsp))
                {
                    Empresa lobjEmpDB = m_empresaRepository.All.Where(p => p.ID == EmpresaEmissao.ID).FirstOrDefault <Empresa>();
                    if (lobjEmpDB != null)
                    {
                        EmpresaEmissao = lobjEmpDB;
                    }
                }

                if (NumeroNF == null || NumeroNF == 0 || string.IsNullOrEmpty(CodigoVerificacao) || string.IsNullOrEmpty(EmpresaEmissao.CcmPmsp))
                {
                    throw new Exception("Não é possível baixar a nota fiscal porque um dos seguintes campos campos vazios: numero da nota, codigo de verificacao ou CCM da emissor");
                }
                else
                {
                    lstrRet = AppDomain.CurrentDomain.BaseDirectory + @"\Arquivos\PdfNotaFiscal\";
                    if (Directory.Exists(lstrRet) == false)
                    {
                        Directory.CreateDirectory(lstrRet);
                    }

                    lstrRet += "NF" + NumeroNF + "CCM" + EmpresaEmissao.CcmPmsp + "VERIF" + CodigoVerificacao + ".gif";
                    if (File.Exists(lstrRet))
                    {
                        File.Delete(lstrRet);
                    }

                    new WebClient().DownloadFile(getUrlLinkNfSitePmsp(), lstrRet);

                    Image img = Image.FromFile(lstrRet);

                    int lintNovoWidth  = 1000;
                    int lintNovoHeight = 1000;
                    int lintOrigWidth  = img.Width;  // largura original
                    int lintOrigHeight = img.Height; // altura original

                    // redimensiona se necessario
                    if (lintOrigWidth > lintNovoWidth || lintOrigHeight > lintNovoHeight)
                    {
                        if (lintOrigWidth > lintOrigHeight)
                        {
                            // imagem horizontal
                            lintNovoHeight = (lintOrigHeight * lintNovoWidth) / lintOrigWidth;
                        }
                        else
                        {
                            // imagem vertical
                            lintNovoWidth = (lintOrigWidth * lintNovoHeight) / lintOrigHeight;
                        }
                    }

                    Bitmap newImage = new Bitmap(lintNovoWidth, lintNovoHeight);
                    using (Graphics gr = Graphics.FromImage(newImage))
                    {
                        gr.SmoothingMode     = SmoothingMode.HighQuality;
                        gr.InterpolationMode = InterpolationMode.HighQualityBicubic;
                        gr.PixelOffsetMode   = PixelOffsetMode.HighQuality;
                        gr.DrawImage(img, new Rectangle(0, 0, lintNovoWidth, lintNovoHeight));
                    }
                    lstrRet = lstrRet.Replace(".gif", ".jpg");
                    if (File.Exists(lstrRet))
                    {
                        File.Delete(lstrRet);
                    }

                    newImage.SetResolution(96, 96);
                    newImage.Save(lstrRet, ImageFormat.Jpeg);

                    PdfDocument lobjPdfMatriz = new PdfDocument();
                    PdfPage     lobjPagina    = lobjPdfMatriz.Pages.Add();
                    XGraphics   lobjGraphics  = XGraphics.FromPdfPage(lobjPagina);

                    if (File.Exists(lstrRet))
                    {
                        XImage lobjLogo = XImage.FromFile(lstrRet);
                        lobjGraphics.DrawImage(lobjLogo, 10, 10);
                        lobjLogo.Dispose();
                        lobjLogo = null;
                    }

                    lobjPdfMatriz.Close();

                    lstrRet = lstrRet.Replace(".jpg", ".pdf");
                    if (File.Exists(lstrRet))
                    {
                        File.Delete(lstrRet);
                    }

                    lobjPdfMatriz.Save(lstrRet);

                    CaminhoPdfNf = lstrRet;

                    if (this.ID > 0)
                    {
                        Faturamento lobjFatUpdate = m_faturamentoRepository.All.Where(p => p.ID == this.ID).FirstOrDefault <Faturamento>();
                        if (lobjFatUpdate != null)
                        {
                            lobjFatUpdate.CaminhoPdfNf = lstrRet;
                            m_faturamentoRepository.InsertOrUpdate(lobjFatUpdate);
                            m_faturamentoRepository.Save();
                        }
                    }
                }
            }

            if (File.Exists(lstrRet) && lstrRet.ToLower().StartsWith(AppDomain.CurrentDomain.BaseDirectory.ToLower()))
            {
                if (aintTipo == 2)
                {
                    lstrRet = CaminhoPdfNf.Substring(AppDomain.CurrentDomain.BaseDirectory.Length);
                }
                else if (aintTipo == 3)
                {
                }
            }
            else
            {
                lstrRet = string.Empty;
            }

            return(lstrRet);
        }