Skip to content

Commit

Permalink
Merge pull request #3 from xingchenyang/master
Browse files Browse the repository at this point in the history
add attribute newPage to create a new page for a row or a table inside body tag
  • Loading branch information
cedricmartel authored Apr 18, 2017
2 parents 8f5eabf + be9fcb7 commit 45be876
Show file tree
Hide file tree
Showing 13 changed files with 282 additions and 16 deletions.
14 changes: 14 additions & 0 deletions Moon.PDFTemplateItextSharp/Generators/BodyGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -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");
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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


2 changes: 1 addition & 1 deletion SampleItextSharp/ConditionalDisplay/Default.aspx.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
1 change: 1 addition & 0 deletions SampleItextSharp/Master.Master
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
<li><a href="/SimpleTest/Default.aspx">Simple generation</a></li>
<li><a href="/MultipleTables/Default.aspx">Multiple tables</a></li>
<li><a href="/ConditionalDisplay/Default.aspx">Conditional display</a></li>
<li><a href="/NewPages/Default.aspx">New pages</a></li>
</ul>
</div>
</div>
Expand Down
22 changes: 11 additions & 11 deletions SampleItextSharp/Master.Master.designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion SampleItextSharp/MultipleTables/Default.aspx.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
11 changes: 11 additions & 0 deletions SampleItextSharp/NewPages/Default.aspx
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="fr.cedricmartel.SampleItextSharp.NewPages.Default" MasterPageFile="../Master.Master" %>

<asp:Content ContentPlaceHolderID="PageContent" runat="server">
<h2>
New pages
<asp:Button runat="server" Text="Génération PDF" OnClick="GenerationPdf" CssClass="btn btn-primary btn-sm btn-lg" />
</h2>
<p class="well">
<asp:Label runat="server" ID="Resulat" ></asp:Label>
</p>
</asp:Content>
70 changes: 70 additions & 0 deletions SampleItextSharp/NewPages/Default.aspx.cs
Original file line number Diff line number Diff line change
@@ -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<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}
};
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: <a href='../Output/" + fileName + "'>" + fileName + "</a><br/><br/><iframe src='../Output/" + fileName + "' width='1024' height='600' />";
}
}
}
24 changes: 24 additions & 0 deletions SampleItextSharp/NewPages/Default.aspx.designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file added SampleItextSharp/NewPages/LogoPdf.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
132 changes: 132 additions & 0 deletions SampleItextSharp/NewPages/test.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
<?xml version="1.0" encoding="utf-8"?>
<pagedef pagesize="A4" margin_left="10" margin_right="10" margin_top="10" margin_bottom="10" pageorientation="portrait">

<header>
<row>
<font fontstyle="BoldItalic" fontsize="11">
<image src="{logoUrl}" align="center" height="80" width="100" >
<var name="{logoUrl}" />
</image>
<textbox text="" width="5%"/>
<textblock width="70%" height="50" align="left" text="{titreDocument}" >
<var name="{titreDocument}" />
</textblock>
</font>
</row>

<!--séparateur-->
<row>
<font fontsize="16">
<textbox width="100%" />
</font>
</row>
</header>

<body>
<row>
<textblock width="100%" align="left" text="Text before a new page" />
</row>
<row>
<textblock width="100%" align="left" text="Another text before a new page" />
</row>

<row newPage="true">
<textblock width="100%" align="left" text="Text with the attribute newPage" />
</row>

<table newPage="true" tablewidth="100%" cellperrow="6" cellwidth="1,2,2,1,1,1" width="100%" var="{NewPageTable}">
<tablehead>
<tablerow>
<tablecell border="Top, Bottom" backgroundcolor="#9BCFF9">
<textbox text="Date"></textbox>
</tablecell>
<tablecell border="Top, Bottom" backgroundcolor="#9BCFF9">
<textbox text="Centre"></textbox>
</tablecell>
<tablecell border="Top, Bottom" backgroundcolor="#9BCFF9">
<textbox text="Frais"></textbox>
</tablecell>
<tablecell border="Top, Bottom" backgroundcolor="#9BCFF9">
<textbox text="Nombre"></textbox>
</tablecell>
<tablecell border="Top, Bottom" backgroundcolor="#9BCFF9">
<textbox text="Base"></textbox>
</tablecell>
<tablecell border="Top, Bottom" backgroundcolor="#9BCFF9">
<textbox text="Montant" align="right"></textbox>
</tablecell>
</tablerow>
</tablehead>

<tableloop>
<tablerow>
<tablecell border="Top, Bottom">
<textbox text="{Date}">
<var name="{Date}" formatter="datetime" formatterparameters="formatstring=dd/MM/yyyy" />
</textbox>
</tablecell>
<tablecell border="Top, Bottom">
<textbox text="{Centre}">
<var name="{Centre}" />
</textbox>
</tablecell>
<tablecell border="Top, Bottom">
<textbox text="{Frais}">
<var name="{Frais}" />
</textbox>
</tablecell>
<tablecell border="Top, Bottom">
<textbox text="{Nombre}">
<var name="{Nombre}" />
</textbox>
</tablecell>
<tablecell border="Top, Bottom">
<textbox text="{Base}">
<var name="{Base}" />
</textbox>
</tablecell>
<tablecell border="Top, Bottom">
<textbox text="{Montant}" align="right">
<var name= "{Montant}" formatter="number" formatterparameters="formatstring=0.00€" />
</textbox>
</tablecell>
</tablerow>
</tableloop>
</table>

<row>
<textblock width="100%" align="left" text="text after the last table" />
</row>
</body>


<footer y="50" absolute="yes">

<!--separating bar with padding-->
<row>
<textbox text=""/>
</row>
<row>
<line x1="-1" x2="-1" width="100%" linethickness="0.1" />
</row>
<row>
<textbox text=""/>
</row>

<!--footer text-->
<row>
<font fontsize="8" fonttype="Helvetica">

<textblock width="50%" align="left" text="{titreDocument}" >
<var name="{titreDocument}" />
</textblock>

<pagenumber width="50%" text="Page {__PAGE__} of {__TOTALPAGE__}" align="right">
<var name="{__PAGE__}" optional="yes" />
<var name="{__TOTALPAGE__}" optional="yes"/>
</pagenumber>
</font>
</row>
</footer>

</pagedef>
13 changes: 13 additions & 0 deletions SampleItextSharp/SampleItextSharp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
<IISExpressAnonymousAuthentication />
<IISExpressWindowsAuthentication />
<IISExpressUseClassicPipelineMode />
<UseGlobalApplicationHostFile />
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
Expand Down Expand Up @@ -76,6 +77,11 @@
<Content Include="MultipleTables\test.xml">
<SubType>Designer</SubType>
</Content>
<Content Include="NewPages\Default.aspx" />
<Content Include="NewPages\LogoPdf.jpg" />
<Content Include="NewPages\test.xml">
<SubType>Designer</SubType>
</Content>
<Content Include="packages.config" />
<Content Include="fonts\glyphicons-halflings-regular.woff2" />
<Content Include="fonts\glyphicons-halflings-regular.woff" />
Expand Down Expand Up @@ -128,6 +134,13 @@
<Compile Include="MultipleTables\Default.aspx.designer.cs">
<DependentUpon>Default.aspx</DependentUpon>
</Compile>
<Compile Include="NewPages\Default.aspx.cs">
<DependentUpon>Default.aspx</DependentUpon>
<SubType>ASPXCodeBehind</SubType>
</Compile>
<Compile Include="NewPages\Default.aspx.designer.cs">
<DependentUpon>Default.aspx</DependentUpon>
</Compile>
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="SimpleTest\Default.aspx.cs">
<DependentUpon>Default.aspx</DependentUpon>
Expand Down
1 change: 1 addition & 0 deletions SampleItextSharp/Web.config
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
<system.web>
<compilation debug="true" targetFramework="4.5" />
<httpRuntime targetFramework="4.5" />
<pages enableEventValidation="false" />
</system.web>

</configuration>

0 comments on commit 45be876

Please sign in to comment.