Both PdfDefinition and RasterImageDef class has Load() and Unload() methods (their signature are slightly different, though).
Take PdfDefinition as example. To reload unloaded Pdf underlay, you can loop through all Pdfdefinitions in a drawing, and test each PdfDefinition.Loaded property. If Loaded=False, then you can call PdfDefinition.Load() method to reload it. Of course, before call Load() to reload, you may want to check PdfDefinition.SourceFileName/ActiveFileName to make sure the source file still exists. If not, you may want to prompt user and/or remove the PdfDefinition and/or locate the source file and reattach it to the drawing as underlay.
To get all PdfDefinition in a drawing, I used code like this:
private ObjectIdCollection GetPDFUnderlayDefs(Transaction tran)
{
ObjectIdCollection ids = new ObjectIdCollection();
DBDictionary namedDic = (DBDictionary)tran.GetObject(_db.NamedObjectsDictionaryId, OpenMode.ForRead);
string pdfDicKey = PdfDefinition.GetDictionaryKey(typeof(PdfDefinition));
if (pdfDicKey != null)
{
if (namedDic.Contains(pdfDicKey))
{
DBDictionary pdfDic = (DBDictionary)tran.GetObject(namedDic.GetAt(pdfDicKey), OpenMode.ForRead);
foreach (DBDictionaryEntry entry in pdfDic)
{
ObjectId id = (ObjectId)entry.Value;
ids.Add(id);
}
}
}
return ids;
} Then, I use this code to get unloaded PdfDefinitions (ObjectIdCollection):
public ObjectIdCollection GetUnloadedPdfDefs()
{
ObjectIdCollection ids = new ObjectIdCollection();
using (_dwg.LockDocument())
{
using (Transaction tran = _db.TransactionManager.StartTransaction())
{
ObjectIdCollection defs = GetPDFUnderlayDefs(tran);
foreach (ObjectId id in defs)
{
PdfDefinition def =(PdfDefinition) tran.GetObject(id, OpenMode.ForRead);
if (!def.Loaded) ids.Add(id);
}
tran.Commit();
}
}
return ids;
} Then I can do reload by passing the ObjectIdCollection obtained from GetUnloadedPdfDefs():
public void ReloadPdf(ObjectIdCollection pdfdefIds)
{
using (_dwg.LockDocument())
{
using (Transaction tran = _db.TransactionManager.StartTransaction())
{
foreach (ObjectId id in pdfdefIds)
{
PdfDefinition def = (PdfDefinition)tran.GetObject(id, OpenMode.ForWrite);
if (!def.Loaded) def.Load(null);
}
tran.Commit();
}
}
} After calling ReloadPdf(), you would need to call Editor.Regen() to make the reloaded Pdf underlay to show up on screen.