Hello mowag! I'm currently having the same issue. This line of code works in 90% of his requests. But eventually it will throw an error.
The solution i created is to create a new textstyle on the DrawingDocument.
GeneralNotes generalNotes = drawingDocument.ActiveSheet.DrawingNotes.GeneralNotes;
TransientGeometry oTG = ((Inventor.Application)drawingDocument.Parent).TransientGeometry;
TextStyle oStyle;
try
{
oStyle = drawingDocument.StylesManager.TextStyles["Note Localizador"];
}
catch
{
oStyle = drawingDocument.StylesManager.TextStyles[1].Copy("Note Localizador") as TextStyle;
}
oStyle.Font = "Arial";
oStyle.FontSize = 0.75f;
oStyle.Rotation = 0;
oStyle.Bold = false;
oStyle.Italic = false;
oStyle.Underline = false;
oStyle.LineSpacing = (double)LineSpacingEnum.kSingleLineSpacing;
oStyle.HorizontalJustification = HorizontalTextAlignmentEnum.kAlignTextLeft;
oStyle.VerticalJustification = VerticalTextAlignmentEnum.kAlignTextUpper;
oStyle.Comments = "Estilo criado para formatar a TextBox Localizador";
TextStyle oStyle2;
try
{
oStyle2 = drawingDocument.StylesManager.TextStyles["Note Pedido"];
}
catch
{
oStyle2 = oStyle.Copy("Note Pedido") as TextStyle;
}
oStyle2.FontSize = 0.5f;
oStyle.Comments = "Estilo criado para formatar a TextBox Pedido";
if (formPrint.Localizador != string.Empty)
{
//string TextBoxLocalizador = "<StyleOverride Font='Arial' FontSize='" + Convert.ToString("0,75", CultureInfo.CurrentCulture) + "'>" + formPrint.Localizador + "</StyleOverride>";
_ = generalNotes.AddFitted(oTG.CreatePoint2d(drawingDocument.ActiveSheet.Width - 9.25, 6.2), formPrint.Localizador, oStyle);
}
if (formPrint.Pedido != string.Empty)
{
//string TextBoxPedido = "<StyleOverride Font='Arial' FontSize='" + Convert.ToString("0,5", CultureInfo.CurrentCulture) + "'> PED" + formPrint.Pedido + "</StyleOverride>";
_ = generalNotes.AddFitted(oTG.CreatePoint2d(drawingDocument.ActiveSheet.Width - 4.25, 6.05), "PED" + formPrint.Pedido, oStyle2);
}
In my case, i won't save the document when the code is running, it's only to print a custom textbox on the activesheet. But i put a try and catch statement to create the textstyle if not exists.
If not exists, copy the first one (can be any) and make sure to format all the styles, to not bring together wrong styles from the one you copy.