From be9fcb7b492cb05f11f95f8df75386d1a5639bfd Mon Sep 17 00:00:00 2001 From: xingchenyang Date: Tue, 18 Apr 2017 13:45:31 +0200 Subject: [PATCH] add attribute newPage to create a new page for a row or a table inside body tag --- .../Generators/BodyGenerator.cs | 14 ++ README.md | 6 +- .../ConditionalDisplay/Default.aspx.cs | 2 +- SampleItextSharp/Master.Master | 1 + SampleItextSharp/Master.Master.designer.cs | 22 +-- .../MultipleTables/Default.aspx.cs | 2 +- SampleItextSharp/NewPages/Default.aspx | 11 ++ SampleItextSharp/NewPages/Default.aspx.cs | 70 ++++++++++ .../NewPages/Default.aspx.designer.cs | 24 ++++ SampleItextSharp/NewPages/LogoPdf.jpg | Bin 0 -> 29566 bytes SampleItextSharp/NewPages/test.xml | 132 ++++++++++++++++++ SampleItextSharp/SampleItextSharp.csproj | 13 ++ SampleItextSharp/Web.config | 1 + 13 files changed, 282 insertions(+), 16 deletions(-) create mode 100644 SampleItextSharp/NewPages/Default.aspx create mode 100644 SampleItextSharp/NewPages/Default.aspx.cs create mode 100644 SampleItextSharp/NewPages/Default.aspx.designer.cs create mode 100644 SampleItextSharp/NewPages/LogoPdf.jpg create mode 100644 SampleItextSharp/NewPages/test.xml diff --git a/Moon.PDFTemplateItextSharp/Generators/BodyGenerator.cs b/Moon.PDFTemplateItextSharp/Generators/BodyGenerator.cs index f17ec99..69460fd 100644 --- a/Moon.PDFTemplateItextSharp/Generators/BodyGenerator.cs +++ b/Moon.PDFTemplateItextSharp/Generators/BodyGenerator.cs @@ -55,6 +55,13 @@ public void DrawBody(Hashtable data, IPDFDraw drawer) { Y = Helper.GetFloatAttributeValue("y", bodyNode.Attributes, -1) }; + // add new page if the attribute "newPage" == "true" + var newPageAttribute = itemNode.Attributes?["newPage"]; + if (newPageAttribute != null && newPageAttribute.Value == "true") + { + pdfTemplate.NextPage(); + pdfTemplate.DrawHeader(); + } rowGroup.AddRow(pdfTemplate._buildRow(itemNode, font)); pdfTemplate.DrawRowGroup(rowGroup, data, DocumentGroup.Body); } @@ -66,6 +73,13 @@ public void DrawBody(Hashtable data, IPDFDraw drawer) var keyAttribute = itemNode.Attributes["var"]; if (keyAttribute == null || !data.ContainsKey(keyAttribute.Value)) continue; + // add new page if the attribute "newPage" == "true" + var newPageAttribute = itemNode.Attributes?["newPage"]; + if (newPageAttribute != null && newPageAttribute.Value == "true") + { + pdfTemplate.NextPage(); + pdfTemplate.DrawHeader(); + } var tableParameters = data[keyAttribute.Value]; if (!(tableParameters is TableData)) throw new Exception("table parameter must be of type TableData"); diff --git a/README.md b/README.md index f499084..97360e8 100644 --- a/README.md +++ b/README.md @@ -12,11 +12,11 @@ This project is using iTextSharp to generate the PDF. http://sourceforge.net/projects/itextsharp -Working copy for following patches : +Working copy for following patches : - OK - add a sample asp.net app - OK - support for multiple tables inside body tag - OK - table won't show if associated data is null - OK - add condition on body items to hide item if associated data is null - - OK - add support for dynamic background color inside table cells + - OK - add support for dynamic background color inside table cells + - OK - add attribute newPage to create a new page for a row or a table inside body tag - diff --git a/SampleItextSharp/ConditionalDisplay/Default.aspx.cs b/SampleItextSharp/ConditionalDisplay/Default.aspx.cs index 310fe7b..fd92435 100644 --- a/SampleItextSharp/ConditionalDisplay/Default.aspx.cs +++ b/SampleItextSharp/ConditionalDisplay/Default.aspx.cs @@ -57,7 +57,7 @@ protected void GenerationPdf(object sender, EventArgs e) // save file locally string fileDirectory = Server.MapPath("../Output/"); - string fileName = "SimpleTest-" + String.Format("{0:yyyyMMdd-HHmmss}", DateTime.Now) + ".pdf"; + string fileName = "ConditionalDisplay-" + String.Format("{0:yyyyMMdd-HHmmss}", DateTime.Now) + ".pdf"; using (var filePdf = new FileStream(fileDirectory + fileName, FileMode.Create)) { using (MemoryStream stream = pdfTemplate.Close()) diff --git a/SampleItextSharp/Master.Master b/SampleItextSharp/Master.Master index d36f4a3..f8e722d 100644 --- a/SampleItextSharp/Master.Master +++ b/SampleItextSharp/Master.Master @@ -37,6 +37,7 @@
  • Simple generation
  • Multiple tables
  • Conditional display
  • +
  • New pages
  • diff --git a/SampleItextSharp/Master.Master.designer.cs b/SampleItextSharp/Master.Master.designer.cs index 99f301a..927fe6a 100644 --- a/SampleItextSharp/Master.Master.designer.cs +++ b/SampleItextSharp/Master.Master.designer.cs @@ -1,10 +1,10 @@ //------------------------------------------------------------------------------ -// -// Ce code a été généré par un outil. +// +// This code was generated by a tool. // -// Les modifications apportées à ce fichier peuvent provoquer un comportement incorrect et seront perdues si -// le code est régénéré. -// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// //------------------------------------------------------------------------------ namespace fr.cedricmartel.SampleItextSharp { @@ -13,20 +13,20 @@ namespace fr.cedricmartel.SampleItextSharp { public partial class Master { /// - /// Contrôle HeadContent. + /// HeadContent control. /// /// - /// Champ généré automatiquement. - /// Pour modifier, déplacez la déclaration de champ du fichier de concepteur dans le fichier code-behind. + /// Auto-generated field. + /// To modify move field declaration from designer file to code-behind file. /// protected global::System.Web.UI.WebControls.ContentPlaceHolder HeadContent; /// - /// Contrôle PageContent. + /// PageContent control. /// /// - /// Champ généré automatiquement. - /// Pour modifier, déplacez la déclaration de champ du fichier de concepteur dans le fichier code-behind. + /// Auto-generated field. + /// To modify move field declaration from designer file to code-behind file. /// protected global::System.Web.UI.WebControls.ContentPlaceHolder PageContent; } diff --git a/SampleItextSharp/MultipleTables/Default.aspx.cs b/SampleItextSharp/MultipleTables/Default.aspx.cs index 7262042..c102ee8 100644 --- a/SampleItextSharp/MultipleTables/Default.aspx.cs +++ b/SampleItextSharp/MultipleTables/Default.aspx.cs @@ -55,7 +55,7 @@ protected void GenerationPdf(object sender, EventArgs e) // save file locally string fileDirectory = Server.MapPath("../Output/"); - string fileName = "SimpleTest-" + String.Format("{0:yyyyMMdd-HHmmss}", DateTime.Now) + ".pdf"; + string fileName = "MultipleTables-" + String.Format("{0:yyyyMMdd-HHmmss}", DateTime.Now) + ".pdf"; using (var filePdf = new FileStream(fileDirectory + fileName, FileMode.Create)) { using (MemoryStream stream = pdfTemplate.Close()) diff --git a/SampleItextSharp/NewPages/Default.aspx b/SampleItextSharp/NewPages/Default.aspx new file mode 100644 index 0000000..f724e35 --- /dev/null +++ b/SampleItextSharp/NewPages/Default.aspx @@ -0,0 +1,11 @@ +<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="fr.cedricmartel.SampleItextSharp.NewPages.Default" MasterPageFile="../Master.Master" %> + + +

    + New pages + +

    +

    + +

    +
    diff --git a/SampleItextSharp/NewPages/Default.aspx.cs b/SampleItextSharp/NewPages/Default.aspx.cs new file mode 100644 index 0000000..eb012c0 --- /dev/null +++ b/SampleItextSharp/NewPages/Default.aspx.cs @@ -0,0 +1,70 @@ +using System; +using System.Collections; +using System.Collections.Generic; +using System.IO; +using System.Web.UI; +using Moon.PDFTemplateItextSharp.Model; + +namespace fr.cedricmartel.SampleItextSharp.NewPages +{ + public partial class Default : Page + { + private readonly Hashtable headerData = new Hashtable(); + private readonly Hashtable bodyData = new Hashtable(); + private readonly Hashtable footerData = new Hashtable(); + + 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 newPageTable = new TableData + { + HeadData = new Hashtable(), + LoopData = new List(), + 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} + }; + newPageTable.LoopData.Add(donnees1); + } + newPageTable.FootData.Add("{Total}", 250.5); + bodyData.Add("{NewPageTable}", newPageTable); + + // pdf generation + pdfTemplate.Draw(headerData, bodyData, footerData); + + // save file locally + string fileDirectory = Server.MapPath("../Output/"); + string fileName = "NewPages-" + 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: " + fileName + "