예제 #1
0
        private string GetRouteFile(tblDocumento x)
        {
            eSystemModules   Modulo          = (eSystemModules)x.IdTipoDocumento;
            eEstadoDocumento EstadoDocumento = (eEstadoDocumento)x.IdEstadoDocumento;

            string _CodigoInforme = string.Format("{0}_{1}_{2}_{3}_{4}.{5}", Modulo.ToString(), x.IdEmpresa.ToString(), x.NroDocumento.ToString("#000"), (EstadoDocumento == eEstadoDocumento.Certificado ? x.FechaEstadoDocumento.ToString("MM-yyyy") : DateTime.Now.ToString("MM-yyyy")), x.VersionOriginal ?? 1, x.NroVersion);
            string _FileName      = string.Format("{0}.pdf", _CodigoInforme.Replace("-", "_"));
            //string _pathFile = string.Format("https://www.BcmWeb_30.net/PDFDocs/{0}", _FileName);
            HttpServerUtility _Server     = HttpContext.Current.Server;
            string            _MapPath    = _Server.MapPath(".");
            string            _ServerPath = _MapPath.Substring(0, _MapPath.ToLowerInvariant().IndexOf("api"));
            string            _pathFile   = String.Format("{0}PDFDocs\\{1}", _ServerPath, _FileName);

            if (!File.Exists(_pathFile))
            {
                _pathFile = string.Empty;
            }

            return(_pathFile);
        }
예제 #2
0
        public string GenerarPDF_Documento(bool Mostrar)
        {
            _ServerPath        = _Server.MapPath(".").Replace("\\Documentos", string.Empty);
            _tempFilePath      = string.Format("{0}\\PDFDocs\\tempPDF", _ServerPath);
            _ImagenApliredPath = string.Format("{0}\\LogosEmpresa\\LogoAplired.png", _ServerPath);

            try
            {
                if (!Directory.Exists(_tempFilePath))
                {
                    Directory.CreateDirectory(_tempFilePath);
                }

                long           IdDocumento     = long.Parse(Session["IdDocumento"].ToString());
                long           IdEmpresa       = long.Parse(Session["IdEmpresa"].ToString());
                long           IdUsuario       = long.Parse(Session["UserId"].ToString());
                long           IdTipoDocumento = long.Parse(Session["IdTipoDocumento"].ToString());
                eSystemModules Modulo          = (eSystemModules)IdTipoDocumento;
                string         TipoDocumento   = Modulo.ToString();
                string         NombreModulo    = string.Empty;

                if (GenerarArchivosTemporales())
                {
                    List <PdfReader> reader = new List <PdfReader>();
                    PdfImportedPage  page;
                    int rotation;
                    int i = 0;
                    int n = 0;
                    _TableOfContent  = new List <objContenido>();
                    _ActualPageWidth = PageSize.LETTER.Width;

                    using (Entities db = new Entities())
                    {
                        tblDocumento dataDocumento = (from d in db.tblDocumento
                                                      where d.IdEmpresa == IdEmpresa &&
                                                      d.IdDocumento == IdDocumento &&
                                                      d.IdTipoDocumento == IdTipoDocumento
                                                      select d).FirstOrDefault();

                        eEstadoDocumento EstadoDocumento = (eEstadoDocumento)dataDocumento.IdEstadoDocumento;

                        //string _docPassowrd = string.Format("BCMWEB.{0}.{1}", (dataDocumento.Negocios ? "N" : "T"), IdEmpresa.ToString("000"));
                        //string _ownerPassowrd = string.Format("{0}.{1}.{2}.BCMWEB", TipoDocumento, (dataDocumento.Negocios ? "N" : "T"), IdEmpresa.ToString("000"));
                        string _CodigoInforme = string.Format("{0}_{1}_{2}_{3}_{4}.{5}", TipoDocumento, IdEmpresa.ToString(), dataDocumento.NroDocumento.ToString("#000"), (EstadoDocumento == eEstadoDocumento.Certificado ? dataDocumento.FechaEstadoDocumento.ToString("MM-yyyy") : DateTime.Now.ToString("MM-yyyy")), dataDocumento.VersionOriginal, dataDocumento.NroVersion);
                        _FileName  = string.Format("{0}.pdf", _CodigoInforme.Replace("-", "_"));
                        _pathFile  = String.Format("{0}\\PDFDocs\\{1}", _ServerPath, _FileName);
                        _strDocURL = String.Format("{0}/PDFDocs/{1}", _AppUrl, _FileName);
                        string _LogoEmpresaPath = string.Format("{0}{1}", _ServerPath, dataDocumento.tblEmpresa.LogoURL.Replace("/", "\\").Replace("~", ""));

                        _ImagenEmpresa           = Image.GetInstance(_LogoEmpresaPath);
                        _ImagenEmpresa.Alignment = Element.ALIGN_CENTER;

                        if (File.Exists(_pathFile))
                        {
                            File.Delete(_pathFile);
                        }

                        string _pattern = string.Format("tmp{0}_{1}???_{2}*.pdf",
                                                        TipoDocumento,
                                                        IdEmpresa.ToString("000"),
                                                        dataDocumento.IdDocumento.ToString("000"));

                        List <string> _pdfFiles = Directory.GetFiles(_tempFilePath, _pattern,
                                                                     SearchOption.AllDirectories).OrderBy(q => q).ToList();

                        _Documento          = new Document();
                        _pdfWrite           = PdfWriter.GetInstance(_Documento, new FileStream(_pathFile, FileMode.Create));
                        _pdfWrite.PageEvent = _PDF_Events;
                        //_pdfWrite.SetEncryption(
                        //      System.Text.Encoding.UTF8.GetBytes(_docPassowrd)
                        //    , System.Text.Encoding.UTF8.GetBytes(_ownerPassowrd)
                        //    , PdfWriter.AllowPrinting
                        //    , PdfWriter.ENCRYPTION_AES_256);

                        string[] docKeywords = new string[]
                        {
                            _FileName,
                            dataDocumento.tblEmpresa.NombreComercial,
                            TipoDocumento,
                        };

                        _Documento.Open();
                        _Documento.AddAuthor("www.bcmWeb.net");
                        _Documento.AddCreator("www.bcmweb.net");
                        _Documento.AddKeywords(string.Join(",", docKeywords));

                        int _PaginaInicioCapitulo = 1;
                        foreach (string _fileName in _pdfFiles)
                        {
                            List <string> _fileSplit = _fileName.Split('_').ToList();
                            long          _IdModulo  = long.Parse(_fileSplit.Last().Split('.').First());

                            tblModulo dataModulo = db.tblModulo.Where(x => x.IdEmpresa == IdEmpresa && x.IdModulo == _IdModulo).FirstOrDefault();

                            _TableOfContent.Add(new objContenido
                            {
                                Capitulo = dataModulo.Nombre,
                                Indent   = false,
                                Page     = _PaginaInicioCapitulo
                            });

                            reader.Add(new PdfReader(_fileName));
                            i = 0;
                            n = reader[reader.Count - 1].NumberOfPages;
                            while (i < n)
                            {
                                i++;
                                _Documento.SetPageSize(reader[reader.Count - 1].GetPageSizeWithRotation(i));
                                _Documento.NewPage();
                                page = _pdfWrite.GetImportedPage(reader[reader.Count - 1], i);
                                PdfContentByte cb = _pdfWrite.DirectContent;
                                rotation = reader[reader.Count - 1].GetPageRotation(i);
                                if (rotation == 90 || rotation == 270)
                                {
                                    cb.AddTemplate(page, 0, -1.0F, 1.0F, 0, 0, reader[reader.Count - 1].GetPageSizeWithRotation(i).Height);
                                }
                                else
                                {
                                    cb.AddTemplate(page, 1.0F, 0, 0, 1.0F, 0, 0);
                                }
                            }
                            _ActualPageWidth       = PageSize.LETTER.Width;
                            _PaginaInicioCapitulo += n;
                        }
                        long IdModuloPadre = IdTipoDocumento * 1000000;
                        NombreModulo = db.tblModulo.Where(x => x.IdEmpresa == IdEmpresa && x.IdModulo == IdModuloPadre).FirstOrDefault().Nombre;

                        GenerarIndice(NombreModulo);
                        _Documento.Close();
                        foreach (PdfReader _reader in reader)
                        {
                            _reader.Close();
                            _reader.Dispose();
                        }

                        if (Modulo == eSystemModules.PMI)
                        {
                            MoverDocumentosEscenarios(dataDocumento.Negocios, _CodigoInforme);
                        }

                        _pdfFiles = Directory.GetFiles(_tempFilePath, _pattern,
                                                       SearchOption.AllDirectories).OrderBy(q => q).ToList();

                        foreach (string _fileName in _pdfFiles)
                        {
                            File.Delete(_fileName);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                //_Documento.Close();
                throw ex;
            }
            return(_strDocURL);
        }