Пример #1
0
        /// <summary>
        /// Salva todos os relatórios em um único PDF.
        /// </summary>
        /// <param name="p_filename">Nome do arquivo PDF a ser salvo.</param>
        public void SaveMerged(string p_filename)
        {
            PDFjet.NET.PDF             v_pdf;
            System.IO.BufferedStream   v_buffer;
            System.IO.FileStream       f;
            Spartacus.Reporting.Report v_report;
            double v_perc, v_percstep, v_lastperc;

            try
            {
                f        = new System.IO.FileStream(p_filename, System.IO.FileMode.Create);
                v_buffer = new System.IO.BufferedStream(f);

                v_pdf = new PDFjet.NET.PDF(v_buffer);

                v_perc     = 0.0;
                v_percstep = 100.0 / (double)this.v_reports.Count;
                v_lastperc = v_percstep;

                for (int k = 0; k < this.v_reports.Count; k++)
                {
                    v_report = this.v_reports[k];

                    v_report.v_perc     = v_perc;
                    v_report.v_percstep = v_percstep;
                    v_report.v_lastperc = v_lastperc;

                    v_report.SavePartial(v_pdf);

                    v_perc      = v_lastperc;
                    v_lastperc += v_percstep;
                }

                v_pdf.Flush();
                v_buffer.Close();
            }
            catch (Spartacus.Reporting.Exception e)
            {
                throw e;
            }
            catch (System.Exception e)
            {
                throw new Spartacus.Reporting.Exception("Erro ao gerar o pacote PDF de saída.", e);
            }
        }
        /// <summary>
        /// Salva como PDF.
        /// </summary>
        /// <param name="p_filename">Nome do arquivo PDF.</param>
        public void Save(string p_filename)
        {
            PDFjet.NET.PDF v_pdf;
            System.IO.BufferedStream v_buffer;
            System.IO.FileStream f;
            PDFjet.NET.Table v_dataheadertable = null, v_datatable;
            float[] v_layout;
            PDFjet.NET.Page v_page;
            System.Collections.Generic.List<System.Collections.Generic.List<PDFjet.NET.Cell>> v_rendered;
            int v_numpages, v_currentpage;
            Spartacus.Utils.Cryptor v_cryptor;
            string v_datafilename;
            System.IO.StreamReader v_reader;

            // se o relatório não tiver dados, não faz nada
            if (this.v_table.Rows.Count == 0)
                return;

            try
            {
                this.v_progress.FireEvent("Spartacus.Reporting.Report", "ExportPDF", this.v_perc, "Renderizando relatorio " + this.v_reportid.ToString() + " no arquivo " + p_filename);
                this.v_inc = this.v_percstep / (double) this.v_table.Rows.Count;
                this.v_renderedrows = 0;

                f = new System.IO.FileStream(p_filename, System.IO.FileMode.Create);
                v_buffer = new System.IO.BufferedStream(f);

                v_pdf = new PDFjet.NET.PDF(v_buffer);

                if (this.v_settings.v_layout == Spartacus.Reporting.PageLayout.LANDSCAPE)
                    v_layout = PDFjet.NET.A4.LANDSCAPE;
                else
                    v_layout = PDFjet.NET.A4.PORTRAIT;

                v_page = new PDFjet.NET.Page(v_pdf, v_layout);

                // tabela de cabecalho de dados

                if (this.v_settings.v_showdataheader)
                {
                    v_dataheadertable = new PDFjet.NET.Table();
                    v_dataheadertable.SetPosition(this.v_settings.v_leftmargin, this.v_settings.v_topmargin  + this.v_header.v_height);

                    v_rendered = this.RenderDataHeader(
                        v_page.GetHeight(),
                        v_page.GetWidth(),
                        this.v_settings.v_dataheaderfont.GetFont(v_pdf)
                    );

                    v_dataheadertable.SetData(v_rendered, PDFjet.NET.Table.DATA_HAS_0_HEADER_ROWS);
                    //v_dataheadertable.SetCellBordersWidth(1.5f);
                }

                // tabela de dados

                v_datatable = new PDFjet.NET.Table();
                //v_datatable.SetPosition(this.v_settings.v_leftmargin, this.v_settings.v_topmargin  + this.v_header.v_height + ((this.v_settings.v_dataheaderfont.v_size + 2) * 1.8 * this.v_numrowsdetail));
                if (this.v_settings.v_showdataheader)
                    v_datatable.SetPosition(this.v_settings.v_leftmargin, this.v_settings.v_topmargin  + this.v_header.v_height + ((this.v_settings.v_dataheaderfont.v_size + 2) * 1.8 * this.v_numrowsdataheader));
                else
                    v_datatable.SetPosition(this.v_settings.v_leftmargin, this.v_settings.v_topmargin  + this.v_header.v_height);
                v_datatable.SetBottomMargin(this.v_settings.v_bottommargin + this.v_footer.v_height);

                this.BuildTemplates(
                    v_page.GetHeight(),
                    v_page.GetWidth(),
                    this.v_settings.v_datafieldfont.GetFont(v_pdf),
                    this.v_settings.v_groupheaderfont.GetFont(v_pdf),
                    this.v_settings.v_groupfooterfont.GetFont(v_pdf)
                );

                v_cryptor = new Spartacus.Utils.Cryptor("spartacus");
                v_datafilename = v_cryptor.RandomString() + ".tmp";
                this.v_datafile = System.IO.File.Open(
                    v_datafilename,
                    System.IO.FileMode.Create,
                    System.IO.FileAccess.ReadWrite
                );

                v_rendered = this.RenderData(
                    v_page.GetHeight(),
                    v_page.GetWidth(),
                    this.v_settings.v_datafieldfont.GetFont(v_pdf),
                    this.v_settings.v_groupheaderfont.GetFont(v_pdf),
                    this.v_settings.v_groupfooterfont.GetFont(v_pdf)
                );

                v_datatable.SetData(v_rendered, PDFjet.NET.Table.DATA_HAS_0_HEADER_ROWS);
                //v_datatable.SetCellBordersWidth(1.5f);

                this.v_datafile.Seek(0, System.IO.SeekOrigin.Begin);
                v_reader = new System.IO.StreamReader(this.v_datafile);

                // salvando PDF

                this.v_header.SetValues(this.v_table);
                this.v_footer.SetValues(this.v_table);

                v_numpages = v_datatable.GetNumberOfPages(v_page);
                v_currentpage = 1;
                while (v_datatable.HasMoreData())
                {
                    this.v_header.SetPageNumber(v_currentpage, v_numpages);
                    this.v_footer.SetPageNumber(v_currentpage, v_numpages);

                    this.v_header.Render(
                        this.v_settings.v_reportheaderfont,
                        this.v_settings.v_leftmargin,
                        this.v_settings.v_topmargin,
                        this.v_settings.v_rightmargin,
                        v_pdf,
                        v_page
                    );

                    if (this.v_settings.v_showdataheader)
                        v_dataheadertable.DrawOn(v_page);
                    v_datatable.ImprovedDrawOn(v_page, v_reader);

                    this.v_footer.Render(
                        this.v_settings.v_reportfooterfont,
                        this.v_settings.v_leftmargin,
                        v_page.GetHeight() - v_settings.v_bottommargin - v_footer.v_height,
                        this.v_settings.v_rightmargin,
                        v_pdf,
                        v_page
                    );

                    if (v_datatable.HasMoreData())
                    {
                        if (this.v_settings.v_showdataheader)
                            v_dataheadertable.ResetRenderedPagesCount();

                        v_page = new PDFjet.NET.Page(v_pdf, v_layout);
                        v_currentpage++;
                    }
                }

                v_pdf.Flush();
                v_buffer.Close();

                v_reader.Close();
                this.v_datafile.Close();
                (new System.IO.FileInfo(v_datafilename)).Delete();

                this.v_perc = this.v_lastperc;
                this.v_progress.FireEvent("Spartacus.Reporting.Report", "ExportPDF", this.v_perc, "Relatorio " + this.v_reportid.ToString() + " renderizado no arquivo " + p_filename);
            }
            catch (System.Exception e)
            {
                throw new Spartacus.Reporting.Exception("Erro ao gerar o arquivo PDF de saída.", e);
            }
        }
        /// <summary>
        /// Salva todos os relatórios em um único PDF.
        /// </summary>
        /// <param name="p_filename">Nome do arquivo PDF a ser salvo.</param>
        public void SaveMerged(string p_filename)
        {
            PDFjet.NET.PDF v_pdf;
            System.IO.BufferedStream v_buffer;
            System.IO.FileStream f;
            Spartacus.Reporting.Report v_report;
            double v_perc, v_percstep, v_lastperc;

            try
            {
                f = new System.IO.FileStream(p_filename, System.IO.FileMode.Create);
                v_buffer = new System.IO.BufferedStream(f);

                v_pdf = new PDFjet.NET.PDF(v_buffer);

                v_perc = 0.0;
                v_percstep = 100.0 / (double) this.v_reports.Count;
                v_lastperc = v_percstep;

                for (int k = 0; k < this.v_reports.Count; k++)
                {
                    v_report = (Spartacus.Reporting.Report) this.v_reports[k];

                    v_report.v_perc = v_perc;
                    v_report.v_percstep = v_percstep;
                    v_report.v_lastperc = v_lastperc;

                    v_report.SavePartial(v_pdf);

                    v_perc = v_lastperc;
                    v_lastperc += v_percstep;
                }

                v_pdf.Flush();
                v_buffer.Close();
            }
            catch (Spartacus.Reporting.Exception e)
            {
                throw e;
            }
            catch (System.Exception e)
            {
                throw new Spartacus.Reporting.Exception("Erro ao gerar o pacote PDF de saída.", e);
            }
        }
        /// <summary>
        /// Converte a fonte em uma opção específica da PDFjet.
        /// </summary>
        /// <returns>Fonte conforme PDFjet.</returns>
        /// <param name="p_pdf">Objeto PDF.</param>
        public PDFjet.NET.Font GetFont(PDFjet.NET.PDF p_pdf)
        {
            PDFjet.NET.Font v_font = null;

            switch (this.v_family)
            {
            case Spartacus.Reporting.FontFamily.COURIER:
                if (this.v_bold && this.v_italic)
                {
                    v_font = new PDFjet.NET.Font(p_pdf, PDFjet.NET.CoreFont.COURIER_BOLD_OBLIQUE);
                    //this.v_nativefont = new System.Drawing.Font("Courier New", (float) this.v_size, System.Drawing.FontStyle.Bold | System.Drawing.FontStyle.Italic);
                }
                if (this.v_bold && !this.v_italic)
                {
                    v_font = new PDFjet.NET.Font(p_pdf, PDFjet.NET.CoreFont.COURIER_BOLD);
                    //this.v_nativefont = new System.Drawing.Font("Courier New", (float) this.v_size, System.Drawing.FontStyle.Bold);
                }
                if (!this.v_bold && this.v_italic)
                {
                    v_font = new PDFjet.NET.Font(p_pdf, PDFjet.NET.CoreFont.COURIER_OBLIQUE);
                    //this.v_nativefont = new System.Drawing.Font("Courier New", (float) this.v_size, System.Drawing.FontStyle.Italic);
                }
                if (!this.v_bold && !this.v_italic)
                {
                    v_font = new PDFjet.NET.Font(p_pdf, PDFjet.NET.CoreFont.COURIER);
                    //this.v_nativefont = new System.Drawing.Font("Courier New", (float) this.v_size, System.Drawing.FontStyle.Regular);
                }
                break;

            case Spartacus.Reporting.FontFamily.HELVETICA:
                if (this.v_bold && this.v_italic)
                {
                    v_font = new PDFjet.NET.Font(p_pdf, PDFjet.NET.CoreFont.HELVETICA_BOLD_OBLIQUE);
                    //this.v_nativefont = new System.Drawing.Font("Helvetica", (float) this.v_size, System.Drawing.FontStyle.Bold | System.Drawing.FontStyle.Italic);
                }
                if (this.v_bold && !this.v_italic)
                {
                    v_font = new PDFjet.NET.Font(p_pdf, PDFjet.NET.CoreFont.HELVETICA_BOLD);
                    //this.v_nativefont = new System.Drawing.Font("Helvetica", (float) this.v_size, System.Drawing.FontStyle.Bold);
                }
                if (!this.v_bold && this.v_italic)
                {
                    v_font = new PDFjet.NET.Font(p_pdf, PDFjet.NET.CoreFont.HELVETICA_OBLIQUE);
                    //this.v_nativefont = new System.Drawing.Font("Helvetica", (float) this.v_size, System.Drawing.FontStyle.Italic);
                }
                if (!this.v_bold && !this.v_italic)
                {
                    v_font = new PDFjet.NET.Font(p_pdf, PDFjet.NET.CoreFont.HELVETICA);
                    //this.v_nativefont = new System.Drawing.Font("Helvetica", (float) this.v_size, System.Drawing.FontStyle.Regular);
                }
                break;

            case Spartacus.Reporting.FontFamily.TIMES:
                if (this.v_bold && this.v_italic)
                {
                    v_font = new PDFjet.NET.Font(p_pdf, PDFjet.NET.CoreFont.TIMES_BOLD_ITALIC);
                    //this.v_nativefont = new System.Drawing.Font("Times New Roman", (float) this.v_size, System.Drawing.FontStyle.Bold | System.Drawing.FontStyle.Italic);
                }
                if (this.v_bold && !this.v_italic)
                {
                    v_font = new PDFjet.NET.Font(p_pdf, PDFjet.NET.CoreFont.TIMES_BOLD);
                    //this.v_nativefont = new System.Drawing.Font("Times New Roman", (float) this.v_size, System.Drawing.FontStyle.Bold);
                }
                if (!this.v_bold && this.v_italic)
                {
                    v_font = new PDFjet.NET.Font(p_pdf, PDFjet.NET.CoreFont.TIMES_ITALIC);
                    //this.v_nativefont = new System.Drawing.Font("Times New Roman", (float) this.v_size, System.Drawing.FontStyle.Italic);
                }
                if (!this.v_bold && !this.v_italic)
                {
                    v_font = new PDFjet.NET.Font(p_pdf, PDFjet.NET.CoreFont.TIMES_ROMAN);
                    //this.v_nativefont = new System.Drawing.Font("Times New Roman", (float) this.v_size, System.Drawing.FontStyle.Regular);
                }
                break;

            default:
                break;
            }

            v_font.SetSize(this.v_size);

            return(v_font);
        }
        /// <summary>
        /// Renderiza um rótulo de número de página no Bloco.
        /// </summary>
        /// <param name="p_object">Objeto a ser renderizado.</param>
        /// <param name="p_posx">Posição X.</param>
        /// <param name="p_posy">Posição Y.</param>
        /// <param name="p_rightmargin">Margem direita.</param>
        /// <param name="p_font">Fonte.</param>
        /// <param name="p_pdf">Objeto PDF.</param>
        /// <param name="p_page">Página onde será renderizado.</param>
        private void RenderPageNumber(Spartacus.Reporting.Object p_object, double p_posx, double p_posy, double p_rightmargin, Spartacus.Reporting.Font p_font, PDFjet.NET.PDF p_pdf, PDFjet.NET.Page p_page)
        {
            PDFjet.NET.TextLine v_text;

            v_text = new PDFjet.NET.TextLine(p_font.GetFont(p_pdf));

            v_text.SetText(p_object.v_value);

            switch (p_object.v_align)
            {
            case Spartacus.Reporting.FieldAlignment.LEFT:
                v_text.SetPosition(p_posx + p_object.v_posx, p_posy + p_object.v_posy);
                break;

            case Spartacus.Reporting.FieldAlignment.RIGHT:
                v_text.SetPosition(p_page.GetWidth() - p_rightmargin - v_text.GetWidth(), p_posy + p_object.v_posy);
                break;

            case Spartacus.Reporting.FieldAlignment.CENTER:
                v_text.SetPosition(p_posx + ((p_page.GetWidth() - p_rightmargin - p_posx) / 2) - (v_text.GetWidth() / 2), p_posy + p_object.v_posy);
                break;

            default:
                break;
            }

            v_text.DrawOn(p_page);

            p_object.v_pdfobject = v_text;
        }
        /// <summary>
        /// Renderiza uma imagem no Bloco.
        /// Essa imagem precisa vir de um arquivo em disco.
        /// </summary>
        /// <param name="p_object">Objeto a ser renderizado.</param>
        /// <param name="p_posx">Posição X.</param>
        /// <param name="p_posy">Posição Y.</param>
        /// <param name="p_rightmargin">Margem direita.</param>
        /// <param name="p_pdf">Objeto PDF.</param>
        /// <param name="p_page">Página onde o objeto será renderizado.</param>
        private void RenderImage(Spartacus.Reporting.Object p_object, double p_posx, double p_posy, double p_rightmargin, PDFjet.NET.PDF p_pdf, PDFjet.NET.Page p_page)
        {
            PDFjet.NET.Image        v_image;
            System.IO.FileInfo      v_info;
            char[]                  v_ch;
            string[]                v_temp;
            Spartacus.Utils.Cryptor v_cryptor;
            string                  v_path;

            if (p_object.v_pdfobject == null)
            {
                v_ch    = new char[1];
                v_ch[0] = '.';

                v_cryptor = new Spartacus.Utils.Cryptor("spartacus");
                try
                {
                    v_path = v_cryptor.Decrypt(p_object.v_value);
                }
                catch (System.Exception)
                {
                    v_path = p_object.v_value;
                }

                v_info = new System.IO.FileInfo(v_path);
                if (v_info.Exists)
                {
                    v_temp = v_path.Split(v_ch);
                    switch (v_temp[v_temp.Length - 1].ToUpper())
                    {
                    case "BMP":
                        v_image = new PDFjet.NET.Image(p_pdf, new System.IO.FileStream(v_path, System.IO.FileMode.Open, System.IO.FileAccess.Read), PDFjet.NET.ImageType.BMP);
                        break;

                    case "JPG":
                        v_image = new PDFjet.NET.Image(p_pdf, new System.IO.FileStream(v_path, System.IO.FileMode.Open, System.IO.FileAccess.Read), PDFjet.NET.ImageType.JPG);
                        break;

                    case "JPEG":
                        v_image = new PDFjet.NET.Image(p_pdf, new System.IO.FileStream(v_path, System.IO.FileMode.Open, System.IO.FileAccess.Read), PDFjet.NET.ImageType.JPG);
                        break;

                    case "PNG":
                        v_image = new PDFjet.NET.Image(p_pdf, new System.IO.FileStream(v_path, System.IO.FileMode.Open, System.IO.FileAccess.Read), PDFjet.NET.ImageType.PNG);
                        break;

                    case "JET":
                        v_image = new PDFjet.NET.Image(p_pdf, new System.IO.FileStream(v_path, System.IO.FileMode.Open, System.IO.FileAccess.Read), PDFjet.NET.ImageType.JET);
                        break;

                    default:
                        v_image = null;
                        break;
                    }

                    if (v_image != null)
                    {
                        switch (p_object.v_align)
                        {
                        case Spartacus.Reporting.FieldAlignment.LEFT:
                            v_image.SetPosition(p_posx + p_object.v_posx, p_posy + p_object.v_posy);
                            break;

                        case Spartacus.Reporting.FieldAlignment.RIGHT:
                            v_image.SetPosition(p_page.GetWidth() - p_rightmargin - v_image.GetWidth(), p_posy + p_object.v_posy);
                            break;

                        case Spartacus.Reporting.FieldAlignment.CENTER:
                            v_image.SetPosition(p_posx + ((p_page.GetWidth() - p_rightmargin - p_posx) / 2) - (v_image.GetWidth() / 2), p_posy + p_object.v_posy);
                            break;

                        default:
                            break;
                        }
                        v_image.DrawOn(p_page);

                        p_object.v_pdfobject = v_image;
                    }
                }
            }
            else
            {
                ((PDFjet.NET.Image)p_object.v_pdfobject).DrawOn(p_page);
            }
        }
        /// <summary>
        /// Renderiza o Bloco em uma Página.
        /// </summary>
        /// <param name="p_font">Fonte.</param>
        /// <param name="p_posx">Posição X.</param>
        /// <param name="p_posy">Posição Y.</param>
        /// <param name="p_rightmargin">Margem direita.</param>
        /// <param name="p_pdf">Objeto PDF.</param>
        /// <param name="p_page">Página onde o Bloco será renderizado.</param>
        public void Render(Spartacus.Reporting.Font p_font, double p_posx, double p_posy, double p_rightmargin, PDFjet.NET.PDF p_pdf, PDFjet.NET.Page p_page)
        {
            PDFjet.NET.Line v_line;
            int             k;

            // renderizando objetos do bloco
            for (k = 0; k < this.v_objects.Count; k++)
            {
                switch (this.v_objects[k].v_type)
                {
                case Spartacus.Reporting.ObjectType.IMAGE:
                    this.RenderImage(this.v_objects[k], p_posx, p_posy, p_rightmargin, p_pdf, p_page);
                    break;

                case Spartacus.Reporting.ObjectType.PAGENUMBER:
                    if (this.v_objects[k].v_font != null)
                    {
                        this.RenderPageNumber(this.v_objects[k], p_posx, p_posy, p_rightmargin, this.v_objects[k].v_font, p_pdf, p_page);
                    }
                    else
                    {
                        this.RenderPageNumber(this.v_objects[k], p_posx, p_posy, p_rightmargin, p_font, p_pdf, p_page);
                    }
                    break;

                case Spartacus.Reporting.ObjectType.TEXT:
                    if (this.v_objects[k].v_font != null)
                    {
                        this.RenderText(this.v_objects[k], p_posx, p_posy, p_rightmargin, this.v_objects[k].v_font, p_pdf, p_page);
                    }
                    else
                    {
                        this.RenderText(this.v_objects[k], p_posx, p_posy, p_rightmargin, p_font, p_pdf, p_page);
                    }
                    break;

                default:
                    break;
                }
            }

            // borda superior
            if (this.v_border.v_top)
            {
                v_line = new PDFjet.NET.Line(p_posx, p_posy, p_page.GetWidth() - p_rightmargin, p_posy);
                v_line.DrawOn(p_page);
            }

            // borda inferior
            if (this.v_border.v_bottom)
            {
                v_line = new PDFjet.NET.Line(p_posx, p_posy + this.v_height, p_page.GetWidth() - p_rightmargin, p_posy + this.v_height);
                v_line.DrawOn(p_page);
            }

            // borda esquerda
            if (this.v_border.v_left)
            {
                v_line = new PDFjet.NET.Line(p_posx, p_posy, p_posx, p_posy + this.v_height);
                v_line.DrawOn(p_page);
            }

            // borda direita
            if (this.v_border.v_right)
            {
                v_line = new PDFjet.NET.Line(p_page.GetWidth() - p_rightmargin, p_posy, p_page.GetWidth() - p_rightmargin, p_posy + this.v_height);
                v_line.DrawOn(p_page);
            }
        }