Here is the code that is working, we need to make sure the preview will be saved...
[CommandMethod("TXT", CommandFlags.Session)]
public void txt()
{
StampDrawing("C:\\test.dwg", "TESTING", true);
}
private void StampDrawing(string fullDwgPath, string stampString, bool FullSize)
{
Document ThisDrawing = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.Open(fullDwgPath, false);
ThisDrawing.LockDocument();
Database thisDB = ThisDrawing.Database;
Autodesk.AutoCAD.DatabaseServices.TransactionManager tm = ThisDrawing.Database.TransactionManager;
using (Transaction trans = tm.StartTransaction())
{
BlockTable bt = (BlockTable)trans.GetObject(thisDB.BlockTableId, OpenMode.ForWrite);
BlockTableRecord modelsp = (BlockTableRecord)trans.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);
DBText stampText = new DBText();
stampText.TextString = stampString;
stampText.Height = 12;
stampText.HorizontalMode = TextHorizontalMode.TextRight;
stampText.Rotation = 0;
Point3d pIns = new Point3d(1070, 15, 0);
stampText.Position = pIns;
modelsp.AppendEntity(stampText);
trans.TransactionManager.AddNewlyCreatedDBObject(stampText, true);
trans.Commit();
}
tm.Dispose();
ThisDrawing.CloseAndSave(fullDwgPath);
thisDB.Dispose();
}