attachments (attachments.cpp) – this example shows how to attach files, or custom data, into a PDF document, and how to extract these files from the document. The example also tests whether read data are the same as those written.

    1 /*
    2  * (c) 2013-2017 http://www.litePDF.cz
    3  *
    4  * The example code is supplied "AS IS". It disclaims all warranties, expressed
    5  * or implied, including, without limitation, the warranties of merchantability
    6  * and of fitness for any purpose. It assumes no liability for direct, indirect,
    7  * incidental, special, exemplary, or consequential damages, which may result
    8  * from the use of the code, even if advised of the possibility of such damage.
    9  *
   10  * Permission is hereby granted to use, copy, modify, and distribute this
   11  * source code, or portions hereof, for any purpose, without fee.
   12  */ 
   13 
   14 #include <windows.h>
   15 #include <stdio.h>
   16 #include <string.h>
   17 #include <string>
   18 
   19 #include "share/litePDF.h"
   20 
   21 static std::string to_string(unsigned int num)
   22 {
   23    char buff[128];
   24 
   25    sprintf(buff, "%u", num);
   26 
   27    return std::string(buff);
   28 }
   29 
   30 static void addPage(litePDF::TLitePDF &litePDF,
   31                     unsigned int pageWidth,
   32                     unsigned int pageHeight,
   33                     const char *msg)
   34 {
   35    // add a new page to it, with large-enough pixel scale
   36    HDC hDC = litePDF.AddPage(litePDF.MMToUnit(pageWidth), litePDF.MMToUnit(pageHeight),
   37                              pageWidth * 10, pageHeight * 10,
   38                              LitePDFDrawFlag_SubstituteFonts);
   39 
   40    // draw the text
   41    LOGFONTA lf = {0, };
   42    lf.lfHeight = -50; // ~5mm
   43    strcpy(lf.lfFaceName, "Helvetica");
   44 
   45    HFONT fnt;
   46    HGDIOBJ oldFnt;
   47 
   48    fnt = CreateFontIndirect(&lf);
   49    oldFnt = SelectObject(hDC, fnt);
   50 
   51    SetTextColor(hDC, RGB(0, 0, 0));
   52    TextOut(hDC, 100, 100, msg, strlen(msg));
   53 
   54    SelectObject(hDC, oldFnt);
   55    DeleteObject(fnt);
   56 
   57    // finish drawing
   58    litePDF.FinishPage(hDC);
   59 }
   60 
   61 int main(void)
   62 {
   63    int res = 0;
   64 
   65    using namespace litePDF;
   66 
   67    try {
   68       TLitePDF litePDF;
   69 
   70       // create a document
   71       litePDF.CreateMemDocument();
   72 
   73       // check expected counts
   74       if (litePDF.GetEmbeddedFileCount() != 0) {
   75          std::string msg = "Newly created document reports non-zero (" +
   76                            to_string(litePDF.GetEmbeddedFileCount()) +
   77                            ") embedded files";
   78          throw TLitePDFException(ERROR_CANNOT_MAKE, msg.c_str());
   79       }
   80 
   81       // create a page
   82       addPage(litePDF, 210, 297, "Document with two attachments");
   83 
   84       const char *customData = "This is\na multiline\ncustom data.";
   85       litePDF.EmbedData("custom data.txt",
   86                         (const BYTE *) customData, strlen(customData));
   87       litePDF.EmbedFile("..\\attachments\\attachments.cpp");
   88 
   89       // check expected counts
   90       if (litePDF.GetEmbeddedFileCount() != 2) {
   91          std::string msg = "Newly created document reports other than two (" +
   92                            to_string(litePDF.GetEmbeddedFileCount()) +
   93                            ") embedded files";
   94          throw TLitePDFException(ERROR_CANNOT_MAKE, msg.c_str());
   95       }
   96 
   97       // save to file
   98       litePDF.SaveToFile("attachments-1.pdf");
   99 
  100       // close the document
  101       litePDF.Close();
  102 
  103       //-----------------------------------------------------------------
  104 
  105       // load from file
  106       litePDF.LoadFromFile("attachments-1.pdf", NULL, true);
  107 
  108       // check expected counts
  109       if (litePDF.GetEmbeddedFileCount() != 2) {
  110          std::string msg = "Loaded document reports other than two (" +
  111                            to_string(litePDF.GetEmbeddedFileCount()) +
  112                            ") embedded files";
  113          throw TLitePDFException(ERROR_CANNOT_MAKE, msg.c_str());
  114       }
  115 
  116       unsigned int ii = 0, sz = litePDF.GetEmbeddedFileCount();
  117       for (ii = 0; ii < sz; ii++) {
  118          std::string fileName;
  119 
  120          fileName = litePDF.GetEmbeddedFileName(ii);
  121          printf("embedded file [%d]: fileName: '%s' ", ii, fileName.c_str());
  122 
  123          unsigned int dataLength = 0;
  124          if (!litePDF.GetEmbeddedFileData(ii, NULL, &dataLength)) {
  125             throw TLitePDFException(ERROR_CANNOT_MAKE,
  126                                     "Failed to get attachment data length");
  127          }
  128 
  129          BYTE *data = (BYTE *) malloc(sizeof(BYTE) * dataLength);
  130          if (!data) {
  131             std::string msg = "Failed to allocate " + to_string(dataLength) + " bytes";
  132             throw TLitePDFException(ERROR_OUTOFMEMORY, msg.c_str());
  133          }
  134 
  135          if (!litePDF.GetEmbeddedFileData(ii, data, &dataLength)) {
  136             free(data);
  137             throw TLitePDFException(ERROR_CANNOT_MAKE, "Failed to get attachment data");
  138          }
  139 
  140          printf("dataLength: %u\n", dataLength);
  141          printf("data:\n%.*s\n******************************************************\n",
  142                  dataLength, data);
  143 
  144          free(data);
  145       }
  146 
  147       // close the document
  148       litePDF.Close();
  149    } catch (TLitePDFException &ex) {
  150       fprintf(stderr, "litePDF Exception: %x: %s\n", ex.getCode(), ex.getMessage());
  151       res = 1;
  152    }
  153 
  154    return res;
  155 }
TOPlist