Код: Выделить всё
private readonly Color _green = ColorConstants.GREEN;
private readonly Color _red = ColorConstants.RED;
private const string Css = "table, td, th { font-size: 9; } table { width: 99 %; border-collapse: collapse; } td, th { border: 1px solid #262A6E; text-align: left; padding: 8px; } tr:nth-child(even) { background - color: #dddddd; }";
private Cell GetCell(string htmlText, int fontSize, bool colspan, int? columns, string colour)
{
var paragraph = new Paragraph();
htmlText = "" + Css + "" + htmlText + "";
using var memoryStream = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(htmlText));
var converterProperties = new ConverterProperties();
var elements = HtmlConverter.ConvertToElements(memoryStream, converterProperties);
foreach (var element in elements)
{
if (element is Paragraph textElement)
{
textElement.SetFontSize(fontSize);
if (!string.IsNullOrEmpty(colour))
{
textElement.SetFontColor(colour.Equals("Green") ? _green : _red);
}
paragraph.Add(textElement);
}
else
{
paragraph.Add(element.ToString());
}
}
var path = hostEnvironment.ContentRootPath;
var fontPath = System.IO.Path.Combine(path, "wwwroot", "fonts", "Arial.ttf");
if (!System.IO.File.Exists(fontPath))
{
//Never hits this line, file exists and is valid
Console.WriteLine("Font file not found!");
}
//FontProgramFactory.RegisterSystemFontDirectories();
//FontProgram fontProgram = FontProgramFactory.CreateFont(fontPath);
//Code breaks at line 163 registryNames.Get("fonts")
//Console.WriteLine($"Font type: {fontProgram.GetFontNames()}");
//Despite code breaking on continue, Font type is correct, yet font is not applied
//Code breaks at line 163 registryNames.Get("fonts")
var font = PdfFontFactory.CreateFont(fontPath, PdfFontFactory.EmbeddingStrategy.PREFER_EMBEDDED);
//Despite code breaking on continue, font is not applied
paragraph.SetFont(font);
var cell = new Cell();
if (colspan)
{
cell = new Cell(1, Convert.ToInt32(columns));
}
cell.SetPaddingLeft(5f);
cell.SetPaddingRight(5f);
cell.SetPaddingBottom(5f);
if (!string.IsNullOrEmpty(colour))
{
cell.SetBackgroundColor(colour.Equals("Green") ? _green : _red);
}
cell.Add(paragraph);
return cell;
}
Будем очень благодарны за любую помощь.
Подробнее здесь: https://stackoverflow.com/questions/792 ... -and-fonts
Мобильная версия