Repository navigation
Expand file tree
/
Copy pathHtmlWorkerTests.cs
More file actions
131 lines (107 loc) · 5.02 KB
/
Copy pathHtmlWorkerTests.cs
File metadata and controls
131 lines (107 loc) · 5.02 KB
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
using System.Collections;
using System.IO;
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;
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();
PdfPCell 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 props = new Hashtable
{
{ "img_provider", new MyImageFactory() },
{ "font_factory", new 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);
}
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>";
}
}
public class UnicodeFontProvider : FontFactoryImp
{
/// <summary>
/// Provides a font with BaseFont.IDENTITY_H encoding.
/// </summary>
public override Font GetFont(string fontname, string encoding, bool embedded, float size, int style, BaseColor color, bool cached)
{
if(string.IsNullOrWhiteSpace(fontname))
{
//TODO: set a default font
}
return FontFactory.GetFont(fontname, BaseFont.IDENTITY_H, BaseFont.EMBEDDED, size, style, color);
}
}
}