Dne 19.9.2013 10:19, Sebastian Haag napsal(a):
> I managed to embed a file (or file content/string) into a 
> PdfMemDocument. Now I want to extract it again and donĀ“t find a way...
     Hi,
it's tricky, or slightly complicated. I do it through traversing the 
actual object hierarchy (see below, it asks for data of an attachment at 
index 'index').
     Bye, zyx

PdfNamesTree *names = document->GetNamesTree(false);
         if (names) {
             PdfObject *pObj = 
names->GetObject()->GetIndirectKey("EmbeddedFiles");
             if (pObj) {
                 const PdfObject *kidsKey = pObj->GetIndirectKey("Kids");
                 if (kidsKey) {
                     const PdfArray &kids = kidsKey->GetArray();
                     PdfArray::const_iterator kit, kend = kids.end();

                     for (kit = kids.begin(); kit != kend; kit++) {
                         const PdfObject *obj = &(*kit);
                         if (obj->IsReference()) {
                             obj = 
document->GetObjects()->GetObject(obj->GetReference());
                             if (!obj) {
                                 continue;
                             }
                         }

                         const PdfObject *namesKey = 
obj->GetIndirectKey("Names");
                         if (namesKey) {
                             const PdfArray &names = namesKey->GetArray();
                             // pair of PdfString/PdfObject, thus divide 
by two
                             if (names.GetSize() / 2 > index) {
                                 PdfArray::const_iterator nit, nend = 
names.end();
                                 for (nit = names.begin(); nit != nend;) {
                                     if (index == 0) {
                                         nit++;

                                         pdf_int64 streamSize = 0;
                                         const PdfStream *stream = NULL;

                                         obj = &(*nit);
                                         if (obj->IsReference()) {
                                             obj = 
document->GetObjects()->GetObject(obj->GetReference());
                                         }

                                         if (obj) {
                                             const PdfDictionary &dict = 
obj->GetDictionary();
                                             if (dict.HasKey("Type") &&
  dict.GetKey("Type")->GetName() == PdfName("Filespec") &&
                                                  dict.HasKey("EF") &&
  dict.GetKey("EF")->GetDictionary().HasKey("F")) {
                                                 obj = 
dict.GetKey("EF")->GetDictionary().GetKey("F");
                                                 if (obj && 
obj->IsReference()) {
                                                     obj = 
document->GetObjects()->GetObject(obj->GetReference());
                                                 }
                                             } else {
                                                 obj = NULL;
                                             }
                                         }

                                         if (obj) {
                                             const PdfDictionary &dict = 
obj->GetDictionary();
                                             if (dict.HasKey("Type") &&
  dict.GetKey("Type")->GetName() == PdfName("EmbeddedFile") &&
  dict.HasKey("Params") &&
  dict.GetKey("Params")->GetDictionary().HasKey("Size") &&
  dict.GetKey("Params")->GetDictionary().GetKey("Size")->IsNumber() &&
  obj->HasStream()) {
                                                 // get the stream
                                                 streamSize = 
dict.GetKey("Params")->GetDictionary().GetKey("Size")->GetNumber();
                                                 stream = obj->GetStream();
                                             }
                                         }

                                         if (!obj) {
                                             // Failed to get embedded 
file object
                                             return FALSE;
                                         }

                                         if (!stream) {
                                             // Failed to get embedded 
file stream
                                             return FALSE;
                                         }

                                         char *buff = NULL;
                                         pdf_long buffLength = 0;

stream->GetFilteredCopy(&buff, &buffLength);

                                         // the buffLength is supposed 
to match streamSize
                                         // do something with the 
buff/buffLength

                                         return TRUE;
                                     }
                                     index--;
                                     nit++;
                                     nit++;
                                 }
                                 break;
                             } else {
                                 index -= names.GetSize() / 2;
                             }
                         }
                     }
                 }
             }
         }


------------------------------------------------------------------------------
LIMITED TIME SALE - Full Year of Microsoft Training For Just $49.99!
1,500+ hours of tutorials including VisualStudio 2012, Windows 8, SharePoint
2013, SQL 2012, MVC 4, more. BEST VALUE: New Multi-Library Power Pack includes
Mobile, Cloud, Java, and UX Design. Lowest price ever! Ends 9/22/13. 
http://pubads.g.doubleclick.net/gampad/clk?id=64545871&iu=/4140/ostg.clktrk
_______________________________________________
Podofo-users mailing list
Podofo-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/podofo-users

Reply via email to