This repository has been archived by the owner on Sep 30, 2022. It is now read-only.
forked from VahidN/iTextSharp.LGPLv2.Core
-
Notifications
You must be signed in to change notification settings - Fork 1
/
HtmlWorkerTests.cs
176 lines (144 loc) · 6.48 KB
/
HtmlWorkerTests.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
using iTextSharp.LGPLv2.Core.FunctionalTests.iTextExamples;
using iTextSharp.text;
using iTextSharp.text.html;
using iTextSharp.text.html.simpleparser;
using iTextSharp.text.pdf;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Collections;
using System.IO;
namespace iTextSharp.LGPLv2.Core.FunctionalTests
{
[TestClass]
public class HtmlWorkerTests
{
[TestMethod]
public void Verify_Base64_Images_CanBe_Processed()
{
var pdfDoc = new Document(PageSize.A4);
var pdfFilePath = TestUtils.GetOutputFileName();
var fileStream = new FileStream(pdfFilePath, FileMode.Create);
PdfWriter.GetInstance(pdfDoc, fileStream);
pdfDoc.AddAuthor(TestUtils.Author);
pdfDoc.Open();
FontFactory.Register(TestUtils.GetTahomaFontPath());
var html = "<img alt='' src='data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAAodJREFUeNpsk0tME1EUhv87UwlCREhRFpi4cGMMRrTE4MaoxBhAsDyMssFHfCQu3BlXGuNKNy5NmqALoqEEMJWCgEUjYojllSpofIUNBNqmIKU6OnQennunUxvgJF86957z/+d27hkGigMlDJfOAmV7AcYsKGqIZljRSvhNE+CMTwEtXmBy2gQb7mCQJUBKkTIQYtfJYCNMAxO9hzq5CYmFiWFY6ISE9VFLRedc1SONeqwf+uJLuKreNPI9nltbLG0orhpqUCM90DRVoEbJ5MSLho1MMg1O0bHOuyoD9crCcxL+xa0HqwL+rEQHsb/CW89reO1aAyEuq+yp+zXvg66rgng8LrDXSmwYpUc8dZkmDsJNL+NCeVVXbWK+O32cpJ7E6OgkwuEwrl8phaHrVsfYD+x03XTPjN3nzZnD0HGxvPppTSLcLwo0I4lldRFK8jdCoZBlJquAbBnr0BD9GUTRvubahclW5qDukqkpIqlodGQ1At3UxZXaIUvauqsyjBV+jZJEJ3s83HO5j+UWI7E6C4mp2EQCTixyV2CvbbKzNmN2zNfHtbzPM3p4FOy/M5CXtwsOKZmmsOi2IHMvyyFhJhgY4BqutQ/aRRstocEngZzswnQnO+x1lqTjy8hIgNdyDc+x5nomxrKJhpcSp2lSrx48WlZhGArynG5hsLLoE7/jQ59f0aR7ZBkdbf7U6Ge+mKYaBvdx8wwZXjtWvfswfTrp3Over29J8NAXYO1t/v/7csZA5U5/Q35nH+aKt8OMR2POPSUFOyRmorvje3BiCt4b9zBANTmwGvP/aMoZRluJbURB8APmnPlQliNLzk8flxbeh9Du8eId5bYQ2SnxH36b/wQYABNFRsIaESsTAAAAAElFTkSuQmCC' />";
var styles = new StyleSheet();
var pdfCell = new PdfPCell
{
Border = 0,
RunDirection = PdfWriter.RUN_DIRECTION_LTR
};
using (var reader = new StringReader(html))
{
var parsedHtmlElements = HtmlWorker.ParseToList(reader, styles);
foreach (IElement htmlElement in parsedHtmlElements)
{
pdfCell.AddElement(htmlElement);
}
}
var table1 = new PdfPTable(1);
table1.AddCell(pdfCell);
pdfDoc.Add(table1);
pdfDoc.Close();
fileStream.Dispose();
TestUtils.VerifyPdfFileIsReadable(pdfFilePath);
}
[TestMethod]
public void Verify_Unicode_Html_To_Pdf_CanBeCreated()
{
var pdfFilePath = TestUtils.GetOutputFileName();
var stream = new FileStream(pdfFilePath, FileMode.Create);
// create a StyleSheet
var styles = new StyleSheet();
// set the default font's properties
styles.LoadTagStyle(HtmlTags.BODY, "encoding", "Identity-H");
styles.LoadTagStyle(HtmlTags.BODY, HtmlTags.FONT, "Tahoma");
styles.LoadTagStyle(HtmlTags.BODY, "size", "16pt");
FontFactory.Register(TestUtils.GetTahomaFontPath());
var unicodeFontProvider = FontFactoryImp.Instance;
unicodeFontProvider.DefaultEmbedding = BaseFont.EMBEDDED;
unicodeFontProvider.DefaultEncoding = BaseFont.IDENTITY_H;
var props = new Hashtable
{
{ "img_provider", new MyImageFactory() },
{ "font_factory", unicodeFontProvider } // Always use Unicode fonts
};
// step 1
var document = new Document();
// step 2
PdfWriter.GetInstance(document, stream);
// step 3
document.AddAuthor(TestUtils.Author);
document.Open();
// step 4
var objects = HtmlWorker.ParseToList(
new StringReader(createHtmlSnippet()),
styles,
props
);
foreach (IElement element in objects)
{
document.Add(element);
}
document.Close();
stream.Dispose();
TestUtils.VerifyPdfFileIsReadable(pdfFilePath);
}
[TestMethod]
public void Verify_Html_To_Pdf_With_colspan_CanBeCreated()
{
var pdfFilePath = TestUtils.GetOutputFileName();
var stream = new FileStream(pdfFilePath, FileMode.Create);
// create a StyleSheet
var styles = new StyleSheet();
// set the default font's properties
styles.LoadTagStyle(HtmlTags.BODY, "encoding", "Identity-H");
styles.LoadTagStyle(HtmlTags.BODY, HtmlTags.FONT, "Tahoma");
styles.LoadTagStyle(HtmlTags.BODY, "size", "16pt");
FontFactory.Register(TestUtils.GetTahomaFontPath());
var unicodeFontProvider = FontFactoryImp.Instance;
unicodeFontProvider.DefaultEmbedding = BaseFont.EMBEDDED;
unicodeFontProvider.DefaultEncoding = BaseFont.IDENTITY_H;
var props = new Hashtable
{
{ "font_factory", unicodeFontProvider }
};
var document = new Document();
PdfWriter.GetInstance(document, stream);
document.AddAuthor(TestUtils.Author);
document.Open();
var objects = HtmlWorker.ParseToList(
new StringReader(@"<table style='width: 100%;font-size:9pt;font-family:Tahoma;'>
<tr>
<td align='center' bgcolor='#00FF00' colspan='2'>Hello</td>
</tr>
<tr>
<td align = 'right'> Month </td>
<td align = 'right'> Savings </td>
</tr>
<tr>
<td align = 'right'> January </td>
<td align = 'right'>$100 </td>
</tr>
</table>"),
styles,
props
);
foreach (IElement element in objects)
{
document.Add(element);
}
document.Close();
stream.Dispose();
TestUtils.VerifyPdfFileIsReadable(pdfFilePath);
}
private string createHtmlSnippet()
{
return @"<b>This is a test</b>
<br/>
<span style='color:blue;font-size:20pt;font-family:tahoma;font-style:italic;font-weight:bold'>
<b>اين يك آزمايش است.<b/>
</span>";
}
}
}