When setting color of a font with PdfPen, font style looks bold.
What PdfPen width should be set or how to change font color in a cell? Please see code below.
public void ShowFont()
{
using (PdfDocument pdfDocument = new PdfDocument())
{
//Create the page
var section = pdfDocument.Sections.Add();
var pdfPage = section.Pages.Add();
PdfGrid grid = new PdfGrid();
var font = new PdfTrueTypeFont(new Font("Arial", 9), 9, true);
for (int j = 0; j < 5; j++)
{
grid.Columns.Add();
}
int rows = 5;
for (int i = 0; i < rows; i++)
{
grid.Rows.Add();
var textPen = new PdfPen(Color.Black);
for (int j = 0; j < 5; j++)
{
var cell = grid.Rows[i].Cells[j];
cell.Style.Font = font;
cell.Style.TextPen = textPen;
cell.Value = $"Cell at i:{i},j:{j}";
}
}
grid.Draw(pdfPage, PointF.Empty, new PdfGridLayoutFormat { Break = PdfLayoutBreakType.FitPage, Layout = PdfLayoutType.Paginate });
//Save the document
pdfDocument.Save("ArialFont.pdf");
//Close the document
pdfDocument.Close(true);
//This will open the PDF file so, the result will be seen in default PDF viewer
System.Diagnostics.Process.Start("ArialFont.pdf");
}
}
|
using (PdfDocument pdfDocument = new PdfDocument())
{
//Create the page
var section = pdfDocument.Sections.Add();
var pdfPage = section.Pages.Add();
PdfGrid grid = new PdfGrid();
var font = new PdfTrueTypeFont(new Font("Arial", 9), 9, true);
for (int j = 0; j < 5; j++)
{
grid.Columns.Add();
}
int rows = 5;
for (int i = 0; i < rows; i++)
{
grid.Rows.Add();
var textPen = new PdfPen(Color.Black);
for (int j = 0; j < 5; j++)
{
var cell = grid.Rows[i].Cells[j];
cell.Style.Font = font;
cell.Style.TextBrush = PdfBrushes.Blue;
cell.Value = $"Cell at i:{i},j:{j}";
}
}
grid.Draw(pdfPage, PointF.Empty, new PdfGridLayoutFormat { Break = PdfLayoutBreakType.FitPage, Layout = PdfLayoutType.Paginate });
//Save the document
pdfDocument.Save("../../ArialFont.pdf");
//Close the document
pdfDocument.Close(true);
} |
That worked great. Thanks! Could we have added this trick to documentation maybe, if not there?