Я использовал модуль ItextSharp для преобразования приведенного ниже html в pdf-страницу.
<div style="font-size: 18pt; font-weight: bold;">
mma<br>mmar</div><br> <br>
<div style="font-size: 14pt;">Click to View Pricing
</div>
<br>
<div>
<table>
<tr><td> <a href="#" onclick="location.href='http://www.mma.com/fci'; return false;" style="color: Blue; font-size: 10pt; text-decoration: underline;"> FCI</a>:</td>
<td><a href="#" onclick="location.href='http://www.mma.com/access/?pn=78211-014'; return false;" style="color: Blue; font-size: 10pt; text-decoration: underline;"> 78211-014</a></td></tr><tr><td></td> <td>
<a href="#" onclick="location.href='http://www.mma.com/access/?pn=78211-009'; return false;"
style="color: Blue; font-size: 10pt; text-decoration: underline;">78211-009</td></tr><tr><td></td> <td>
<a href="#" onclick="location.href='http://www.mma.com/access/?pn=78211-006'; return false;"
style="color: Blue; font-size: 10pt; text-decoration: underline;">78211-006</td></tr><tr><td></td> <td>
<a href="#" onclick="location.href='http://www.mma.com/access/?pn=78211-007'; return false;"
style="color: Blue; font-size: 10pt; text-decoration: underline;">78211-007</td></tr><tr><td></td> <td>
<a href="#" onclick="location.href='http://www.mma.com/access/?pn=78211-003'; return false;"
style="color: Blue; font-size: 10pt; text-decoration: underline;">78211-003</td></tr><tr><td></td> <td>
<a href="#" onclick="location.href='http://www.mma.com/access/?pn=78211-005'; return false;"
style="color: Blue; font-size: 10pt; text-decoration: underline;">78211-005</td></tr><tr><td></td> <td>
<a href="#" onclick="location.href='http://www.mma.com/access/?pn=78211-008'; return false;"
style="color: Blue; font-size: 10pt; text-decoration: underline;">78211-008</td></tr><tr><td></td> <td>
<a href="#" onclick="location.href='http://www.mma.com/access/?pn=78211-004'; return false;"
style="color: Blue; font-size: 10pt; text-decoration: underline;">78211-004</td></tr><tr><td></td> <td>
<a href="#" onclick="location.href='http://www.mma.com/access/?pn=78211-012'; return false;"
style="color: Blue; font-size: 10pt; text-decoration: underline;">78211-012</td></tr><tr><td></td> <td>
<a href="#" onclick="location.href='http://www.mma.com/access/?pn=78211-007LF'; return false;"
style="color: Blue; font-size: 10pt; text-decoration: underline;">78211-007LF</td></tr><tr><td></td> <td>
<a href="#" onclick="location.href='http://www.mma.com/access/?pn=78211-015LF'; return false;"
style="color: Blue; font-size: 10pt; text-decoration: underline;">78211-015LF</td></tr><tr><td></td> <td>
<a href="#" onclick="location.href='http://www.mma.com/access/?pn=78211-003LF'; return false;"
style="color: Blue; font-size: 10pt; text-decoration: underline;">78211-003LF</td></tr><tr><td></td> <td>
<a href="#" onclick="location.href='http://www.mma.com/access/?pn=78211-009LF'; return false;"
style="color: Blue; font-size: 10pt; text-decoration:
underline;">78211-009LF</td></tr><tr><td></td> <td>
<a href="#" onclick="location.href='http://www.mma.com/access/?pn=78211-005LF'; return false;"
style="color: Blue; font-size: 10pt; text-decoration: underline;">78211-005LF</td></tr><tr><td></td> <td>
<a href="#" onclick="location.href='http://www.mma.com/access/?pn=78211-010LF'; return false;"
style="color: Blue; font-size: 10pt; text-decoration: underline;">78211-010LF</td></tr><tr><td></td> <td>
<a href="#" onclick="location.href='http://www.mma.com/access/?pn=78211-006LF'; return false;"
style="color: Blue; font-size: 10pt; text-decoration: underline;">78211-006LF</td></tr><tr><td></td> <td>
<a href="#" onclick="location.href='http://www.mma.com/access/?pn=78211-014LF'; return false;"
style="color: Blue; font-size: 10pt; text-decoration: underline;">78211-014LF</td></tr><tr><td></td> <td>
<a href="#" onclick="location.href='http://www.mma.com/access/?pn=78211-004LF'; return false;"
style="color: Blue; font-size: 10pt; text-decoration: underline;">78211-004LF</td></tr><tr><td></td> <td>
<a href="#" onclick="location.href='http://www.mma.com/access/?pn=78211-012LF'; return false;"
style="color: Blue; font-size: 10pt; text-decoration: underline;">78211-012LF</td></tr><tr><td></td> <td>
<a href="#" onclick="location.href='http://www.mma.com/access/?pn=78211-008LF'; return false;"
style="color: Blue; font-size: 10pt; text-decoration: underline;">78211-008LF</td></tr><tr><td></td> <td>
<a href="#" onclick="location.href='http://www.mma.com/access/?pn=78211-011LF'; return false;"
style="color: Blue; font-size: 10pt; text-decoration: underline;">78211-011LF</td></tr><tr><td></td> <td><a href="#" onclick="location.href='http://www.mma.com/access/?pn=78211-013LF'; return false;"
style="color: Blue; font-size: 10pt; text-decoration: underline;">78211-013LF</td></tr><tr><td></td> <td><a href="#" onclick="location.href='http://www.mma.com/access/?pn=78211-010'; return false;" style="color: Blue; font-size: 10pt; text-decoration: underline;">78211-010</td></tr><tr><td></td>
<td><a href="#" onclick="location.href='http://www.mma.com/access/?pn=78211-015'; return false;"
style="color: Blue; font-size: 10pt; text-decoration: underline;">78211-015</td></tr><tr><td>
<a href="#" onclick="location.href='http://www.mma.com/souriau'; return false;"
style="color: Blue; font-size: 10pt; text-decoration: underline;"> Souriau</a>:</td>
<td><a href="#" onclick="location.href='http://www.mma.com/access/?pn=24JR124-3'; return false;" style="color: Blue; font-size: 10pt; text-decoration: underline;"> 24JR124-3</a></td></tr></table></div>
Код С# для генерации html выше:
var html = new StringBuilder(@"<div style=""font-size: 18pt; font-weight: bold;"">Authorized Distributor</div><br> <br><div style=""font-size: 14pt;"">Click to View Pricing, Inventory, Delivery & Lifecycle Information:</div><br>");
List<MfrBrandView> mfrBrands = MfrBrandView.Load(fileId);
var uniquesuppliers = mfrBrands.GroupBy(t => new {t.Manufacturer,t.SupplierVirtualDirectory}).Select(g => g.First());
html.Append("<div><table>");
foreach (var uniquesupplier in uniquesuppliers)
{
var mfrPartNums =
mfrBrands.Where(t => t.ManufacturerId == uniquesupplier.ManufacturerId).Select(
t => t.ManufacturerPartNumber).ToList();
html.AppendFormat(@"<tr><td> <a href=""http://www.mma.com/{0}"" style=""color: Blue; font-size: 10pt; text-decoration: underline;""> {1}</a>:</td> <td><a href=""http://www.mma.com/access/?pn={2}"" style=""color: Blue; font-size: 10pt; text-decoration: underline;""> {2}</a></td></tr>", uniquesupplier.SupplierVirtualDirectory, uniquesupplier.MouserizationLabel, mfrPartNums[0]);
for (int i = 1; i < mfrPartNums.Count(); i++)
{
html.AppendFormat(
@"<tr><td></td> <td><a href=""http://www.mma.com/access/?pn={0}"" style=""color: Blue; font-size: 10pt; text-decoration: underline;"">{0}</td></tr>",
mfrPartNums[i]);
}
}
html.Append("</table></div>");
return html.ToString();
Код для генерации страницы после формирования html:
PdfWriter writer = null;
PdfDocument doc = null;
//PdfReader reader = new PdfReader(content);
// reader.Info["Title"]
try
{
doc = new PdfDocument();
doc.SetPageSize(PageSize.LETTER);
writer = PdfWriter.GetInstance(doc, content);
writer.CloseStream = false;
doc.Open();
doc.NewPage();
foreach (IElement element in iTextSharp.text.html.simpleparser.HTMLWorker.ParseToList(new StringReader(html), null))
{
doc.Add(element);
}
}
ниже, где я получаю исключение, говоря "Невозможно применить объект типа 'iTextSharp.text.html.simpleparser.CellWrapper' для ввода" iTextSharp.text.Paragraph "".
iTextSharp.text.html.simpleparser.HTMLWorker.ParseToList(new StringReader(html), null)
Что-то не так с html, который я пытаюсь разобрать? Есть ли лучший способ сделать это? Большое спасибо за вашу помощь. Он очень ценится.