forked from finalcut/SharpPDFLabel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Label.cs
99 lines (85 loc) · 3.26 KB
/
Label.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
using iTextSharp.text;
using iTextSharp.text.pdf;
using System.Collections.Generic;
using System.IO;
namespace SharpPDFLabel
{
public class Label
{
private List<byte[]> _images;
private List<TextChunk> _textChunks;
private Enums.Alignment _hAlign;
/// sets align to CENTER
public Label()
: this(Enums.Alignment.CENTER)
{
}
/// <param name="hAlign">horizontal alignment: LEFT, CENTER, RIGHT</param>
public Label(Enums.Alignment hAlign)
{
_images = new List<byte[]>();
_textChunks = new List<TextChunk>();
_hAlign = hAlign;
}
public PdfPCell GetLabelCell()
{
// Create a new Phrase and add the image to it
var cellContent = new Phrase();
foreach (var img in _images)
{
var pdfImg = iTextSharp.text.Image.GetInstance(img);
cellContent.Add(new Chunk(pdfImg, 0, 0));
}
foreach (var txt in _textChunks)
{
var font = FontFactory.GetFont(txt.FontName, BaseFont.CP1250, txt.EmbedFont, txt.FontSize, txt.FontStyle);
cellContent.Add(new Chunk("\n" + txt.Text, font));
}
//Create a new cell specifying the content
var cell = new PdfPCell(cellContent)
{
HorizontalAlignment = (int)_hAlign,
VerticalAlignment = Element.ALIGN_BOTTOM //TODO was ALIGN_TOP and this works fine for text, but Images don't line up without ALIGN_BOTTOM
};
return cell;
}
private void CopyStream(Stream input, Stream output)
{
byte[] b = new byte[32768];
int r;
while ((r = input.Read(b, 0, b.Length)) > 0)
output.Write(b, 0, r);
}
/// <summary>
/// Add an image to the labels
/// Currently adds images and then text in that specific order
/// </summary>
/// <param name="img"></param>
public void AddImage(Stream img)
{
var mem = new MemoryStream();
CopyStream(img, mem);
_images.Add(mem.GetBuffer());
}
/// <summary>
/// Add a chunk of text to the labels
/// </summary>
/// <param name="text">The text to add e.g "I am on a label"</param>
/// <param name="fontName">The name of the font e.g. "Verdana"</param>
/// <param name="fontSize">The font size in points e.g. 12</param>
/// <param name="embedFont">If the font you are using may not be on the target machine, set this to true</param>
/// <param name="fontStyles">An array of required font styles</param>
public void AddText(string text, string fontName, int fontSize, bool embedFont = false, params Enums.FontStyle[] fontStyles)
{
int fontStyle = 0;
if (fontStyles != null)
{
foreach (var item in fontStyles)
{
fontStyle += (int)item;
}
}
_textChunks.Add(new TextChunk() { Text = text, FontName = fontName, FontSize = fontSize, FontStyle = fontStyle, EmbedFont = embedFont });
}
}
}