Assign Fonts (.NET)

Fonts define the shapes of the text characters that make up each character set. A single font can be used by more than one style. Use the FileName property to set the font file for the text style. You can assign TrueType or AutoCAD-compiled SHX fonts to a text style.

Set text fonts

The following example gets the current font values using the Font property for the active text style and then changes the typeface for the font to “PlayBill.” To see the effects of changing the typeface, add some multiline or single-line text to your current drawing before running the example. Note that, if you don't have the PlayBill font on your system, you need to substitute a font you do have in order for this example to work.

C# Example

using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
 
[CommandMethod("UpdateTextFont")]
public static void UpdateTextFont()
{
    // Get the current document and database
    Document acDoc = Application.DocumentManager.MdiActiveDocument;
    Database acCurDb = acDoc.Database;
 
    // Start a transaction
    using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
    {
        // Open the current text style for write
        TextStyleTableRecord acTextStyleTblRec;
        acTextStyleTblRec = acTrans.GetObject(acCurDb.Textstyle,
                                              OpenMode.ForWrite) as TextStyleTableRecord;
 
        // Get the current font settings
        Autodesk.AutoCAD.GraphicsInterface.FontDescriptor acFont;
        acFont = acTextStyleTblRec.Font;
 
        // Update the text style's typeface with "PlayBill"
        Autodesk.AutoCAD.GraphicsInterface.FontDescriptor acNewFont;
        acNewFont = new
          Autodesk.AutoCAD.GraphicsInterface.FontDescriptor("PlayBill",
                                                            acFont.Bold,
                                                            acFont.Italic,
                                                            acFont.CharacterSet,
                                                            acFont.PitchAndFamily);
 
        acTextStyleTblRec.Font = acNewFont;
 
        acDoc.Editor.Regen();
 
        // Save the changes and dispose of the transaction
        acTrans.Commit();
    }
}