예제 #1
0
        protected void GenerationPdf(object sender, EventArgs e)
        {
            // templates load
            var template = Server.MapPath("test.xml");
            var pdfTemplate = new Moon.PDFTemplateItextSharp.PDFTemplateItextSharp(template);
            // TODO fonts externes 

            //  parameters load
            headerData.Add("{titreDocument}", "SERVICE DEPARTEMENTAL D'INCENDIE \nET DE SECOURS DES DEUX-SEVRES");
            headerData.Add("{logoUrl}", Server.MapPath("LogoPdf.jpg"));
            footerData.Add("{titreDocument}", "Titre du document");

            // data load
            var firstTable = new TableData
            {
                HeadData = new Hashtable(),
                LoopData = new List<Hashtable>(),
                FootData = new Hashtable()
            };
            DateTime debut = new DateTime(2016, 1, 1);
            for (int i = 0; i < 100; i++)
            {
                var donnees1 = new Hashtable
                {
                    {"{Date}", debut.AddDays(i)},
                    {"{Centre}", "Centre 1"},
                    {"{Frais}", 5},
                    {"{Nombre}", "200,00"},
                    {"{Base}", "5,00"},
                    {"{Montant}", i}
                };
                firstTable.LoopData.Add(donnees1);
            }
            firstTable.FootData.Add("{Total}", 250.5);
            bodyData.Add("{FirstTable}", firstTable);

            // Here we set the condition value for conditional output
            bodyData.Add("{Condition}", Condition.Checked);

            // pdf generation
            pdfTemplate.Draw(headerData, bodyData, footerData);

            // save file locally
            string fileDirectory = Server.MapPath("../Output/");
            string fileName = "SimpleTest-" + String.Format("{0:yyyyMMdd-HHmmss}", DateTime.Now) + ".pdf";
            using (var filePdf = new FileStream(fileDirectory + fileName, FileMode.Create))
            {
                using (MemoryStream stream = pdfTemplate.Close())
                {
                    byte[] content = stream.ToArray();
                    filePdf.Write(content, 0, content.Length);
                }
            }

            Resulat.Text = "Generated PDF: <a href='../Output/" + fileName + "'>" + fileName + "</a><br/><br/><iframe src='../Output/" + fileName + "' width='1024' height='600' />";
        }
예제 #2
0
        /// <summary>
        /// Render 
        /// </summary>
        public void DrawTable(TableData data, IPDFDraw drawer)
        {
            tableData = data;
            pdfDrawer = drawer;

            if (tableData.LoopData != null)
            {
                PdfPTable table = DrawTableHead();
                DrawTableLoop(ref table);
                DrawTableFoot(ref table);

                if (table.Rows.Count > 0)
                {
                    var pdfDraw = (PDFDrawItextSharp.PDFDrawItextSharp)pdfDrawer;
                    pdfDrawer.NextRow(1, DocumentGroup.Table);
                    pdfDraw.DrawTable(table, pdfDraw.Current_x, pdfDraw.Current_y);
                    pdfDraw.NextRow(table.TotalHeight, DocumentGroup.Table);
                }
            }
        }
        /// <summary>
        /// Draw PDF using the template and this data.
        /// </summary>
        /// <param name="headerData"></param>
        /// <param name="loopData"></param>
        /// <param name="bodyData"></param>
        /// <param name="footerData"></param>
        /// <param name="tableHeadData"></param>
        /// <param name="tableLoopData"></param>
        /// <param name="tableFootData"></param>
        public void Draw(
            Hashtable headerData,
            List<Hashtable> loopData,
            Hashtable bodyData,
            Hashtable footerData,
            Hashtable tableHeadData,
            List<Hashtable> tableLoopData,
            Hashtable tableFootData)
        {
            if (headerData != null)
            {
                this.headerData = headerData;
            }
            if (bodyData != null)
            {
                this.bodyData = bodyData;
            }
            if (footerData != null)
            {
                this.footerData = footerData;
            }

            var tableData = new TableData
            {
                HeadData = tableHeadData,
                LoopData = tableLoopData,
                FootData = tableFootData
            };

            if (DrawCallCounter > 0)
            {
                // how many times the Draw() hv been call! if it was call b4, this time we draw to a new page!
                NextPage();
            }

            DrawHeader();

            if (principalTableGenerator != null && tableData.LoopData != null)
                principalTableGenerator.DrawTable(tableData, PdfDrawer);

            if (loopData != null)
            {
                _drawLoop(loopData);
            }

            if (bodyGenerator != null)
                bodyGenerator.DrawBody(bodyData, PdfDrawer);

            _drawFooter();

            EachPageCount.Add(PageCount);
            DrawCallCounter++;
        }