Font style looks bold when using PdfPen

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");

            }

        }



3 Replies 1 reply marked as answer

GK Gowthamraj Kumar Syncfusion Team December 27, 2021 02:46 PM UTC

Hi umlprog, 
 
When we draw the PDF grid cell with pdfpen, it will drawn by pen and default brush size, so that the text looks like a bold. We can achieve your requirement by using PdfBrushes alone to change the font color in Pdf Grid cell.  
 
Please find the below code snippet,  
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); 
 
} 
 
Please find the output document from below link, 
 
Please let us know if you need any further assistance with this. 
 
Regards, 
Gowthamraj K 


Marked as answer

UM umlprog December 28, 2021 10:09 AM UTC

That worked great. Thanks! Could we have added this trick to documentation maybe, if not there? 



GK Gowthamraj Kumar Syncfusion Team December 29, 2021 12:04 PM UTC

Hi umlprog,  
 
Thank you for your update. We are glad to know that your problem has been solved. 
 
Please refer the below API reference link for more information, 
Please let us know if you need any further assistance with this.  
  
Regards,  
Gowthamraj K  


Loader.
Up arrow icon