Font in PdfGrid

I am still struggling with formats and styles in a PDFGrid. I tried to generate a white text in front of a blue background. When I set the FontStyle to greater 10 and to Bold, I get white text but with a black font inside the letters. It looks a bit like an outline font. For not bold or smaller font sizes this is ot observed. Furthermore, the larger letters seem to have no sharp borders.

I stripped down my code to a working example which is included below.

Is it possible to habe a pure white pen with a font size of 16 and bold?


Hartmut


Attachment: IExportService_66f43ef2.zip

3 Replies

KS Karmegam Seerangan Syncfusion Team May 14, 2024 03:35 PM UTC

Hi Hartmut,


Upon further analysis of your code, we recommend utilizing the TextBrush  property in the PDF grid. Please refer to the code snippet below for implementation:

PdfGridRow headerRow = masterGrid.Headers[0];

 

for (int x = 0; x < headerRow.Cells.Count; x++)

{

   //Get the cell. 

   if (x == 0 || x == 1)

   {

       PdfGridCell headerCell = headerRow.Cells[x];

 

       headerCell.StringFormat.LineAlignment = PdfVerticalAlignment.Middle;

       headerCell.StringFormat.Alignment = PdfTextAlignment.Center;

       headerCell.Value = "Cell" + x.ToString();

 

       PdfGridCellStyle headerCellStyle = new PdfGridCellStyle();

 

       headerCellStyle.BackgroundBrush = PdfBrushes.Blue;

  

       headerCellStyle.Borders.Top = new PdfPen(Color.Blue);

       headerCellStyle.Borders.Left = new PdfPen(Color.Blue);

       headerCellStyle.Borders.Right = new PdfPen(Color.Blue);

       headerCellStyle.Borders.Bottom = new PdfPen(Color.White);

       headerCellStyle.Borders.Bottom.Width = 2;

       headerCellStyle.Font = new PdfStandardFont(PdfFontFamily.Courier, 10);

       headerCellStyle.TextPen = PdfPens.White;

   headerCellStyle.TextBrush=PdfBrushes.White;

       headerCell.Style = headerCellStyle;

   }

 

   if (x == 2 || x == 3)

   {

       PdfGridCell headerCell = headerRow.Cells[x];

 

       headerCell.StringFormat.LineAlignment = PdfVerticalAlignment.Middle;

       headerCell.StringFormat.Alignment = PdfTextAlignment.Center;

       headerCell.Value = "Cell" + x.ToString();

 

       PdfGridCellStyle headerCellStyle = new PdfGridCellStyle();

 

       headerCellStyle.BackgroundBrush = PdfBrushes.Blue;

       headerCellStyle.Borders.Top = new PdfPen(Color.Blue);

       headerCellStyle.Borders.Left = new PdfPen(Color.Blue);

       headerCellStyle.Borders.Right = new PdfPen(Color.Blue);

       headerCellStyle.Borders.Bottom = new PdfPen(Color.White);

       headerCellStyle.Borders.Bottom.Width = 2;

       headerCellStyle.Font = new PdfStandardFont(PdfFontFamily.Courier, 20);

       headerCellStyle.TextPen = PdfPens.White;

   headerCellStyle.TextBrush = PdfBrushes.White;

   headerCell.Style = headerCellStyle;

   }

 

   if (x == 4 || x == 5)

   {

       PdfGridCell headerCell = headerRow.Cells[x];

 

       headerCell.StringFormat.LineAlignment = PdfVerticalAlignment.Middle;

       headerCell.StringFormat.Alignment = PdfTextAlignment.Center;

       headerCell.Value = "Cell" + x.ToString();

 

       PdfGridCellStyle headerCellStyle = new PdfGridCellStyle();

 

       headerCellStyle.BackgroundBrush = PdfBrushes.Blue;

       headerCellStyle.Borders.Top = new PdfPen(Color.Blue);

       headerCellStyle.Borders.Left = new PdfPen(Color.Blue);

       headerCellStyle.Borders.Right = new PdfPen(Color.Blue);

       headerCellStyle.Borders.Bottom = new PdfPen(Color.White);

       headerCellStyle.Borders.Bottom.Width = 2;

       headerCellStyle.Font = new PdfStandardFont(PdfFontFamily.Courier, 20, PdfFontStyle.Bold);

       headerCellStyle.TextPen = PdfPens.White;

   headerCellStyle.TextBrush = PdfBrushes.White;

   headerCell.Style = headerCellStyle;

   }

 

}

 

Please try it on your end and let us know if you require any further assistance.


Regards,

Karmegam




HA Hartmut replied to Karmegam Seerangan May 15, 2024 03:29 PM UTC


Hello Karmegam,

  headerCellStyle.TextBrush = PdfBrushes.White;

works in the complete solution. What is the difference between PdfPen and TextBrush?


Furthermore, when I try from your documentation:

//Create PDF graphics for the page.
PdfGraphics graphics = page.Graphics;
//Use the font installed in the machine
PdfFont font = new PdfTrueTypeFont(new Font("Arial", 14));
//Draw the text.
graphics.DrawString("Hello World!!!", font, PdfBrushes.Black, new PointF(0, 0));

I get an error in the line
PdfFont font = new PdfTrueTypeFont(new Font("Arial", 14));
with the message : Connot resolve constructor

How can this be fixed?

Hartmut


JT Jeyalakshmi Thangamarippandian Syncfusion Team May 16, 2024 11:20 AM UTC

Hi Hartmut,

PdfPen is used to draw lines, borders, and outlines in a PDF document.

TextBrush is used to define the color of text when drawing text in a PDF document.

We can utilize the PdfTrueTypeFont instance using the FileStream. For more details, you can refer to the API documentation:

Class PdfTrueTypeFont - API Reference (syncfusion.com)


Regards,

Jeyalakshmi T


Loader.
Up arrow icon