Skip to content

Commit ef0cb39

Browse files
author
Christophe Peugnet
committed
v0.5 (table and enbedded Font)
1 parent eff3bdc commit ef0cb39

File tree

9 files changed

+200
-90
lines changed

9 files changed

+200
-90
lines changed
23.2 KB
Binary file not shown.

Blazor-PDF/Blazor-PDF/Blazor-PDF.csproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@
55
<RootNamespace>Blazor_PDF</RootNamespace>
66
</PropertyGroup>
77

8+
<ItemGroup>
9+
<Content Include="Assets\Moder DOS 437.ttf" />
10+
</ItemGroup>
11+
812
<ItemGroup>
913
<PackageReference Include="iTextSharp.LGPLv2.Core" Version="1.6.5" />
1014
</ItemGroup>

Blazor-PDF/Blazor-PDF/PDF/page4.cs

Lines changed: 21 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -6,104 +6,39 @@ namespace Blazor_PDF.PDF
66
{
77
public class Page4
88
{
9-
public static void PageFonts(Document pdf, PdfWriter writer)
9+
public static void PageTable(Document pdf, PdfWriter writer)
1010
{
11-
var title = new Paragraph("Fonts standard", new Font(Font.HELVETICA, 20, Font.BOLD));
12-
pdf.Add(title);
13-
14-
15-
// Fonts
16-
17-
title = new Paragraph("Times Roman")
18-
{
19-
SpacingBefore = 18f,
20-
SpacingAfter = 9f
21-
};
22-
pdf.Add(title);
23-
24-
25-
BaseFont bfTimes = BaseFont.CreateFont(BaseFont.TIMES_ROMAN, encoding:BaseFont.CP1252, embedded:false);
26-
Examples(pdf, bfTimes);
2711

28-
29-
title = new Paragraph("Courier")
30-
{
31-
SpacingBefore = 18f,
32-
SpacingAfter = 9f
33-
};
34-
pdf.Add(title);
35-
36-
BaseFont bfCourier= BaseFont.CreateFont(BaseFont.COURIER, encoding: BaseFont.CP1252, embedded: false);
37-
Examples(pdf, bfCourier);
38-
39-
40-
title = new Paragraph("Helvetica")
41-
{
42-
SpacingBefore = 18f,
43-
SpacingAfter = 9f
44-
};
12+
// TABLES
13+
var title = new Paragraph("My First Table", new Font(Font.HELVETICA, 20, Font.BOLD));
4514
pdf.Add(title);
4615

47-
BaseFont bfHelvetica = BaseFont.CreateFont(BaseFont.HELVETICA, encoding: BaseFont.CP1252, embedded: false);
48-
Examples(pdf, bfHelvetica);
4916

17+
Table datatable = new Table(3);
18+
datatable.Padding = 2;
19+
datatable.Spacing = 0;
5020

51-
title = new Paragraph("Symbol")
52-
{
53-
SpacingBefore = 18f,
54-
SpacingAfter = 9f
55-
};
56-
pdf.Add(title);
21+
float[] headerwidths = { 6, 20, 32 };
5722

58-
BaseFont bSymbol = BaseFont.CreateFont(BaseFont.SYMBOL, encoding: BaseFont.CP1252, embedded: false);
59-
Examples(pdf, bSymbol);
23+
datatable.Widths = headerwidths;
24+
datatable.DefaultHorizontalAlignment = Element.ALIGN_LEFT;
6025

61-
62-
title = new Paragraph("ZapfDingBats®")
26+
int m = 0;
27+
for (int i = 0; i < 12; i++)
6328
{
64-
SpacingBefore = 18f,
65-
SpacingAfter = 9f
66-
};
67-
pdf.Add(title);
68-
69-
BaseFont bZapfDingBats = BaseFont.CreateFont(BaseFont.ZAPFDINGBATS, encoding: BaseFont.CP1252, embedded: false);
70-
Examples(pdf, bZapfDingBats);
71-
}
72-
73-
74-
private static void Examples(Document pdf, BaseFont basefont)
75-
{
76-
string text = "0123456789 ABDCEFGHIJKLMNOPQRSTXYWZ abcdefghijklmnopqrstuvwxyz @&*$€";
77-
float fontSize = 12;
78-
79-
Font font = new Font(basefont, fontSize, Font.NORMAL, BaseColor.Black);
80-
var paragraph = new Paragraph(text, font);
81-
pdf.Add(paragraph);
82-
83-
84-
if (basefont.PostscriptFontName == BaseFont.SYMBOL) return;
85-
if (basefont.PostscriptFontName == BaseFont.ZAPFDINGBATS) return;
86-
87-
88-
font = new Font(basefont, fontSize, Font.BOLD, BaseColor.Black);
89-
paragraph = new Paragraph(text, font);
90-
pdf.Add(paragraph);
91-
92-
font = new Font(basefont, fontSize, Font.ITALIC, BaseColor.Black);
93-
paragraph = new Paragraph(text, font);
94-
pdf.Add(paragraph);
29+
m += i;
30+
datatable.AddCell(i.ToString());
31+
datatable.AddCell("Blazor is super simple.");
32+
datatable.AddCell("right?");
33+
}
9534

96-
font = new Font(basefont, fontSize, Font.UNDERLINE, BaseColor.Black);
97-
paragraph = new Paragraph(text, font);
98-
pdf.Add(paragraph);
9935

100-
font = new Font(basefont, fontSize, Font.BOLD + Font.ITALIC, BaseColor.Black);
101-
paragraph = new Paragraph(text, font);
102-
pdf.Add(paragraph);
36+
datatable.AddCell("=" + m.ToString());
37+
datatable.AddCell("Yes");
38+
datatable.AddCell("Strongly agree");
10339

104-
font = new Font(basefont, fontSize, Font.STRIKETHRU, BaseColor.Black);
105-
paragraph = new Paragraph(text, font);
106-
pdf.Add(paragraph);
40+
pdf.Add(datatable);
41+
10742
}
10843
}
10944
}

Blazor-PDF/Blazor-PDF/PDF/page5.cs

Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
using System;
2+
using System.Linq;
3+
using iTextSharp.text;
4+
using iTextSharp.text.pdf;
5+
6+
7+
namespace Blazor_PDF.PDF
8+
{
9+
public class Page5
10+
{
11+
public static void PageFonts(Document pdf, PdfWriter writer)
12+
{
13+
14+
// FONTS
15+
var title = new Paragraph("Fonts standard", new Font(Font.HELVETICA, 20, Font.BOLD));
16+
pdf.Add(title);
17+
18+
19+
// --- TIMES ROMAN
20+
title = new Paragraph("Times Roman")
21+
{
22+
SpacingBefore = 18f,
23+
SpacingAfter = 9f
24+
};
25+
pdf.Add(title);
26+
27+
28+
BaseFont bfTimes = BaseFont.CreateFont(BaseFont.TIMES_ROMAN, encoding:BaseFont.CP1252, embedded:false);
29+
Pattern(pdf, bfTimes);
30+
31+
32+
// --- COURIER
33+
title = new Paragraph("Courier")
34+
{
35+
SpacingBefore = 18f,
36+
SpacingAfter = 9f
37+
};
38+
pdf.Add(title);
39+
40+
BaseFont bfCourier= BaseFont.CreateFont(BaseFont.COURIER, encoding: BaseFont.CP1252, embedded: false);
41+
Pattern(pdf, bfCourier);
42+
43+
44+
// --- HELVETICA
45+
title = new Paragraph("Helvetica")
46+
{
47+
SpacingBefore = 18f,
48+
SpacingAfter = 9f
49+
};
50+
pdf.Add(title);
51+
52+
BaseFont bfHelvetica = BaseFont.CreateFont(BaseFont.HELVETICA, encoding: BaseFont.CP1252, embedded: false);
53+
Pattern(pdf, bfHelvetica);
54+
55+
56+
// --- SYMBOL
57+
title = new Paragraph("Symbol")
58+
{
59+
SpacingBefore = 18f,
60+
SpacingAfter = 9f
61+
};
62+
pdf.Add(title);
63+
64+
BaseFont bSymbol = BaseFont.CreateFont(BaseFont.SYMBOL, encoding: BaseFont.CP1252, embedded: false);
65+
Pattern(pdf, bSymbol);
66+
67+
68+
// --- ZAPDINGBATS
69+
title = new Paragraph("ZapfDingBats®")
70+
{
71+
SpacingBefore = 18f,
72+
SpacingAfter = 9f
73+
};
74+
pdf.Add(title);
75+
76+
BaseFont bZapfDingBats = BaseFont.CreateFont(BaseFont.ZAPFDINGBATS, encoding: BaseFont.CP1252, embedded: false);
77+
Pattern(pdf, bZapfDingBats);
78+
79+
80+
// BREAK RETURN : so easy ^^
81+
pdf.NewPage();
82+
83+
84+
// FONTS EMBEDDED INTO THE PDF
85+
title = new Paragraph("Font embedded", new Font(Font.HELVETICA, 20, Font.BOLD));
86+
pdf.Add(title);
87+
88+
BaseFont myfont = BaseFont.CreateFont(@"Assets/Moder DOS 437.ttf", BaseFont.CP1252, BaseFont.EMBEDDED);
89+
Font font = new Font(myfont, 14);
90+
91+
string s = "Add your own font in your document as here the Moder DOS";
92+
93+
pdf.Add(new Paragraph(s, font));
94+
95+
96+
Table datatable = new Table(16);
97+
datatable.Padding = 2;
98+
datatable.Spacing = 0;
99+
datatable.Border = 0;
100+
float[] headerwidths = Enumerable.Range(0, 16).Select(i => 1.6f).ToArray();
101+
datatable.Widths = headerwidths;
102+
datatable.DefaultHorizontalAlignment = Element.ALIGN_CENTER;
103+
104+
int m = 9;
105+
string charater;
106+
107+
for (int r = 0; r < 16; r++)
108+
{
109+
for (int c = 0; c < 16; c++)
110+
{
111+
if (m > 31)
112+
{
113+
charater = Convert.ToChar(m).ToString();
114+
datatable.AddCell(new Paragraph(charater, font));
115+
}
116+
m++;
117+
}
118+
}
119+
pdf.Add(datatable);
120+
121+
}
122+
123+
124+
125+
private static void Pattern(Document pdf, BaseFont basefont)
126+
{
127+
string text = "0123456789 ABDCEFGHIJKLMNOPQRSTXYWZ abcdefghijklmnopqrstuvwxyz @&*$€";
128+
float fontSize = 12;
129+
130+
Font font = new Font(basefont, fontSize, Font.NORMAL, BaseColor.Black);
131+
var paragraph = new Paragraph(text, font);
132+
pdf.Add(paragraph);
133+
134+
135+
if (basefont.PostscriptFontName == BaseFont.SYMBOL) return;
136+
if (basefont.PostscriptFontName == BaseFont.ZAPFDINGBATS) return;
137+
138+
139+
font = new Font(basefont, fontSize, Font.BOLD, BaseColor.Black);
140+
paragraph = new Paragraph(text, font);
141+
pdf.Add(paragraph);
142+
143+
font = new Font(basefont, fontSize, Font.ITALIC, BaseColor.Black);
144+
paragraph = new Paragraph(text, font);
145+
pdf.Add(paragraph);
146+
147+
font = new Font(basefont, fontSize, Font.UNDERLINE, BaseColor.Black);
148+
paragraph = new Paragraph(text, font);
149+
pdf.Add(paragraph);
150+
151+
font = new Font(basefont, fontSize, Font.BOLD + Font.ITALIC, BaseColor.Black);
152+
paragraph = new Paragraph(text, font);
153+
pdf.Add(paragraph);
154+
155+
font = new Font(basefont, fontSize, Font.STRIKETHRU, BaseColor.Black);
156+
paragraph = new Paragraph(text, font);
157+
pdf.Add(paragraph);
158+
}
159+
}
160+
}

Blazor-PDF/Blazor-PDF/PDF/report.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,9 @@ private byte[] ReportPDF()
7979
else if ( _pagenumber == 3 )
8080
Page3.PageImage(pdf, writer);
8181
else if (_pagenumber == 4)
82-
Page4.PageFonts(pdf, writer);
82+
Page4.PageTable(pdf, writer);
83+
else if (_pagenumber == 5)
84+
Page5.PageFonts(pdf, writer);
8385

8486
pdf.Close();
8587

Blazor-PDF/Blazor-PDF/Pages/Index.razor

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,21 +31,30 @@
3131
</div>
3232
</div>
3333
</div>
34+
3435
<br/>
36+
3537
<div class="row">
38+
<div class="col ">
39+
<div class="card h-100">
40+
<h5 class="card-header">Tables</h5>
41+
<div class="card-body">
42+
<p class="card-text">Create your table, columns, rows, Cells...</p>
43+
<a href="#" class="btn btn-primary" @onclick="@(p => createFile(4, "tables.pdf"))">Create File</a>
44+
</div>
45+
</div>
46+
</div>
3647
<div class="col ">
3748
<div class="card h-100">
3849
<h5 class="card-header">Fonts</h5>
3950
<div class="card-body">
4051
<p class="card-text">iTextSharp include the support of 5 type of fonts with variant styles</p>
41-
<a href="#" class="btn btn-primary" @onclick="@(p => createFile(4, "fonts.pdf"))">Create File</a>
52+
<a href="#" class="btn btn-primary" @onclick="@(p => createFile(5, "fonts.pdf"))">Create File</a>
4253
</div>
4354
</div>
4455
</div>
4556
<div class="col">
4657
</div>
47-
<div class="col">
48-
</div>
4958
</div>
5059
</div>
5160

12.7 KB
Loading

Blazor-PDF/Output/fonts.pdf

9.3 KB
Binary file not shown.

Blazor-PDF/Output/tables.pdf

1.63 KB
Binary file not shown.

0 commit comments

Comments
 (0)