Back to Blog

    Developer Blog

    How to Add Attachments to your PDFs with Foxit PDF SDK (C++)

    Foxit PDF SDK for Windows

    With Foxit PDF SDK, attachments refer to documents rather than file attachment annotations. This allows whole files to be embedded in a document, much like email attachments. PDF SDK provides application APIs to access functions such as loading attachments, getting attachments, inserting/removing attachments, and accessing properties of attachments.

    Example:

    How to export the embedded attachment file from a PDF and save it as a single file

    #include "include/common/fs_common.h"
    #include "include/pdf/fs_pdfdoc.h"
    #include "include/pdf/fs_pdfattachments.h"
    using namespace foxit;
    using namespace common;
    using namespace pdf;
    using namespace foxit::common;
    // Assuming PDFDoc doc has been loaded.
    // Get information of attachments.
    Attachments attachments(doc);
    int count = attachments.GetCount();
    for (int i = 0; i < count; i++) {
     WString key = attachments.GetKey(i);
     FileSpec file_spec = attachments.GetEmbeddedFile(key);
     if (!file_spec.IsEmpty()) {
     WString name = file_spec.GetFileName();
     if (file_spec.IsEmbedded()) {
     WString exFilePath = "output_directory";
     file_spec.ExportToFile(exFilePath);
     }
     }
    }
    ...
    

    How to remove all the attachments of a PDF

    #include "include/common/fs_common.h"
    #include "include/pdf/fs_pdfdoc.h"
    #include "include/pdf/fs_pdfattachments.h"
    ...
    using namespace foxit;
    using namespace common;
    using namespace pdf;
    using namespace foxit::common;
    // Assuming PDFDoc doc has been loaded.
    // Get information of attachments.
    Attachments attachments(doc);
    int count = attachments.GetCount();
    for (int i = 0; i < count; i++) {
     WString key = attachments.GetKey(i);
     attachment.RemoveEmbeddedFile(&key);
    }
    ...