Ejemplo n.º 1
0
        /// <summary>
        /// Salva como PDF parcialmente.
        /// Usado para renderização de pacotes de arquivos PDF.
        /// </summary>
        /// <param name="p_pdf">Objeto PDF aberto.</param>
        public void SavePartial(PDFjet.NET.PDF p_pdf)
        {
            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 o relatorio " + this.v_reportid.ToString());
                this.v_inc = this.v_percstep / (double) this.v_table.Rows.Count;
                this.v_renderedrows = 0;

                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(p_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(p_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(p_pdf),
                    this.v_settings.v_groupheaderfont.GetFont(p_pdf),
                    this.v_settings.v_groupfooterfont.GetFont(p_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(p_pdf),
                    this.v_settings.v_groupheaderfont.GetFont(p_pdf),
                    this.v_settings.v_groupfooterfont.GetFont(p_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,
                        p_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,
                        p_pdf,
                        v_page
                    );

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

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

                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.");
            }
            catch (System.Exception e)
            {
                throw new Spartacus.Reporting.Exception("Erro ao gerar o arquivo PDF de saída.", e);
            }
        }
        /// <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 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 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);
            }
        }