Attach PDF or RasterImage Reload

Attach PDF or RasterImage Reload

Anonymous
Not applicable
1,856 Views
3 Replies
Message 1 of 4

Attach PDF or RasterImage Reload

Anonymous
Not applicable

Hi

  Is there any Functionality available for Reloading the PDF or Image like we have for DWGs (Database.ReloadXrefs).

Thanks

0 Likes
Accepted solutions (1)
1,857 Views
3 Replies
Replies (3)
Message 2 of 4

norman.yuan
Mentor
Mentor
Accepted solution

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.

 

Norman Yuan

Drive CAD With Code

EESignature

Message 3 of 4

Anonymous
Not applicable

Thanks norman.yuan for your help. i was calling the load function but missing the regen of drwing. that solved the problem.

Thanks a lot once again.

 

 

0 Likes
Message 4 of 4

Anonymous
Not applicable

Thank you norman.yuan, I have been looking for this as well.

Would you by any chance also know how to set the page of a pdf reference? I haven't been able to find a related variable in the PdfDefinition and PdfReference objects.

0 Likes