Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
15
Use PrivateFontCollection in UltraPrintDocument and UltraPrintPreviewDialog
posted
I need a text obtained by a method to be drawn with an added font with PrivateFontCollection.

This is done in the PrintPage event of the UltraPrintDocument, using e.Graphics.DrawString, but when the print preview is generated, the text appears with its original source, not with the font created from the font loaded with PrivateFontCollection.

Once you check the source and it works correctly, try assigning the font to an UltraLabel and it works correctly, but the drawing does not work.

I would appreciate it if you could please help me with my problem and if in that case it could not be done in that way, what would you please give me a suggestion on how to do it.

the code:

--To get the font
        private KeyValuePair<string, Font> GetCodigoBarrasRecaudo(UltraLabel label, Graphics graphics)
        {
                //Other Code

            if (valuesOfParams[0] == "1")
            {
                privateFontCollection = new PrivateFontCollection();
                privateFontCollection.AddFontFile(Application.StartupPath + @"\Updates\Fonts\Code128bWinLarge.ttf");

                var fontFamily = privateFontCollection.Families.Where(p => p.Name == "Code128bWinLarge").FirstOrDefault();

                if (fontFamily == null)
                {
                    errorMessage = $"Error durante proceso de codificación:\nNo se encontro la fuente de Codigo de Barras";
                    return GetErrorMessageBarCodeRecaudo(label, errorMessage, fontToText, graphics);
                }

                if (fontFamily.IsStyleAvailable(FontStyle.Regular))
                    fontToText = new Font(fontFamily, label.Font.SizeInPoints, FontStyle.Regular, GraphicsUnit.Pixel);


                if ((int)label.Appearance.TextHAlign == 0)
                    label.Appearance.TextHAlign = HAlign.Left;
            }

            ModifySizeOfControl(label, fontToText, encodeData, graphics);
            return new KeyValuePair<string, Font>(encodeData, fontToText);
        }
--To set the font
 private string GetValorCalculado(UltraLabel lblCalculado, Graphics graphics, ref Font fontToControl)
        {
                //other code.....
      
      
                var resultEncode = GetCodigoBarrasRecaudo(lblCalculado, graphics);
                valorCalculado = resultEncode.Key;
                fontToControl = resultEncode.Value;

            return valorCalculado;
        }

--To draw the text
        private void ultraPrintDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
        {
        //other code.......
        
        
                 string valueOfLabel = string.Empty;
                Font fontToLabel = lbl.Font;
                 valueOfLabel = GetValorCalculado(lbl, e.Graphics, ref fontToLabel);
                
                e.Graphics.DrawString(valueOfLabel, fontToLabel, new SolidBrush(lbl.Appearance.ForeColor), rt, this.alineacionTexto(lbl));
        }
  • 25665
    Offline posted
    Hello Willy,
    Thank you for contacting Infragistics!
    I have done some looking into this matter and this is something that is outside the field of our controls. The e.Graphics returns an object that is not a part of the Infragistics control.
  • 469350
    Offline posted

    UltraPrintDocument derives from PrintDocument. So the event you are handling is an event on PrintDocument, and the Graphics object in the event args is simply the graphics of the printer page. 

    In any case, I'm not sure if the graphics object is really relevant here. If you assigned a font from a PrivateFontCollection as the Font property of the label, and then you are getting the lbl.Font property, I would expect it to return the Font you set it to. But once again, the Font property is a property on Control, so if that's happening, it seems to me that it's an issue with the DotNet framework and isn't anything specific to the Infragistics controls. 

    Is that what's happening? Are you explicitly setting the Font property of an UltraLabel and then later retrieving the Font property of that same UltraLabel control and getting the wrong Font? I don't see anywhere in the code you posted here where you are setting a Font on an UltraLabel control - or any control. I see where you are getting it and using it in the PrintPage event. If it's not the correct Font, then I recommend trying this with some other control, like an inbox Label control. If the Label control works and the UltraLabel does not, than maybe there's a bug there. Also, just to be clear, the only way this would work is if you set the Font Property on the control. Setting the UltraLabel.Appearance.Font properties would not work and would not affect the Font being returned from the property.