C++ API Interface (litePDF.cpp)

    1 /*
    2  * (c) 2013-2016 http://www.litePDF.cz
    3  * (c) 2017 zyx [@:] zyx gmx [dot] us
    4  *
    5  * This software is provided 'as-is', without any express or implied
    6  * warranty.  In no event will the authors be held liable for any damages
    7  * arising from the use of this software.
    8  *
    9  * Permission is granted to anyone to use this software for any purpose,
   10  * including commercial applications, and to alter it and redistribute it
   11  * freely, subject to the following restrictions:
   12  *
   13  * 1. The origin of this software must not be misrepresented; you must not
   14  *    claim that you wrote the original software. If you use this software
   15  *    in a product, an acknowledgment in the product documentation would be
   16  *    appreciated but is not required.
   17  * 2. Altered source versions must be plainly marked as such, and must not be
   18  *    misrepresented as being the original software.
   19  * 3. This notice may not be removed or altered from any source distribution.
   20  */
   21 
   22 //---------------------------------------------------------------------------
   23 #include <windows.h>
   24 #include <stdio.h>
   25 #include <string>
   26 
   27 #ifdef LITEPDF_USE_VCL_EXCEPTION
   28 #include <vcl.h>
   29 
   30 #pragma hdrstop
   31 #endif
   32 
   33 #include "litePDF.h"
   34 
   35 // link to version.lib, due to checkAPIVersion()
   36 #ifdef _MSC_VER
   37 #pragma comment (lib, "version.lib")
   38 #else // _MSC_VER
   39 #ifdef _BORLANDC
   40 #pragma link "version.lib"
   41 #endif // _BORLANDC
   42 #endif // _MSC_VER
   43 
   44 namespace litePDF {
   45 
   46 #define ThrowIfFail(_expr) do {                                                \
   47       if (!(_expr)) {                                                          \
   48          std::string ttmsg;                                                    \
   49          ttmsg = std::string(_func) + ": Assertion '" + (#_expr) + "' failed"; \
   50          throw TLitePDFException(ERROR_INVALID_PARAMETER, ttmsg.c_str());      \
   51       }                                                                        \
   52    } while (false)
   53 
   54 #define ThrowMessageIfFail(_expr, _msg) do {                                   \
   55       if (!(_expr)) {                                                          \
   56          std::string ttmsg = std::string(_func) + ": " + (_msg);               \
   57          throw TLitePDFException(ERROR_INVALID_PARAMETER, ttmsg.c_str());      \
   58       }                                                                        \
   59    } while (false)
   60 
   61 #define ThrowLastErrorIfFail(_expr) do {                                       \
   62       if (!(_expr)) {                                                          \
   63          std::string ttmsg = std::string("Failed to call '") + (_func) + "'";  \
   64          const char *msg = getLastErrorMessage();                              \
   65          if (!msg) {                                                           \
   66             msg = ttmsg.c_str();                                               \
   67          }                                                                     \
   68          throw TLitePDFException(getLastErrorCode(), msg);                     \
   69       }                                                                        \
   70    } while (false)
   71 
   72 #define InitFunc(_ret,_name,_params)                  \
   73    freeLastError();                                   \
   74    typedef _ret (__stdcall FAR *LP ## _name) _params; \
   75    LP ## _name func;                                  \
   76                                                       \
   77    func = (LP ## _name) GetProc(# _name);
   78 
   79 #define FreePtr(_ptr) do { if ((_ptr)) { free((_ptr)); (_ptr) = NULL; } } while (0)
   80 
   81 //---------------------------------------------------------------------------
   82 
   83 #ifdef LITEPDF_USE_VCL_EXCEPTION
   84 __fastcall TLitePDFException::TLitePDFException(DWORD pCode, const char *pMsg) : Sysutils::Exception(pMsg)
   85 #else
   86 TLitePDFException::TLitePDFException(DWORD pCode, const char *pMsg)
   87 #endif
   88 {
   89    code = pCode;
   90    #ifndef LITEPDF_USE_VCL_EXCEPTION
   91    if (pMsg) {
   92       msg = strdup(pMsg);
   93    } else {
   94       msg = NULL;
   95    }
   96    #endif
   97 }
   98 //---------------------------------------------------------------------------
   99 
  100 #ifdef LITEPDF_USE_VCL_EXCEPTION
  101 __fastcall TLitePDFException::TLitePDFException(const TLitePDFException &src) : Sysutils::Exception(src)
  102 #else
  103 TLitePDFException::TLitePDFException(const TLitePDFException &src)
  104 #endif
  105 {
  106    code = src.getCode();
  107    #ifdef LITEPDF_USE_VCL_EXCEPTION
  108    Message = src.Message;
  109    #else
  110    if (src.getMessage()) {
  111       msg = strdup(src.getMessage());
  112    } else {
  113       msg = NULL;
  114    }
  115    #endif
  116 }
  117 //---------------------------------------------------------------------------
  118 
  119 #ifdef LITEPDF_USE_VCL_EXCEPTION
  120 __fastcall TLitePDFException::~TLitePDFException()
  121 #else
  122 TLitePDFException::~TLitePDFException()
  123 #endif
  124 {
  125    #ifndef LITEPDF_USE_VCL_EXCEPTION
  126    FreePtr(msg);
  127    #endif
  128 }
  129 //---------------------------------------------------------------------------
  130 
  131 DWORD TLitePDFException::getCode(void) const
  132 {
  133    return code;
  134 }
  135 //---------------------------------------------------------------------------
  136 
  137 #ifndef LITEPDF_USE_VCL_EXCEPTION
  138 const char *TLitePDFException::getMessage(void) const
  139 {
  140    return msg;
  141 }
  142 #endif // !LITEPDF_USE_VCL_EXCEPTION
  143 //---------------------------------------------------------------------------
  144 
  145 TLitePDF::TLitePDF()
  146 {
  147    lib = NULL;
  148    context = NULL;
  149    lastErrorCode = 0;
  150    lastErrorMessage = NULL;
  151    onError = NULL;
  152    onErrorUserData = NULL;
  153    onEvalFontFlag = NULL;
  154    onEvalFontFlagUserData = NULL;
  155 }
  156 //---------------------------------------------------------------------------
  157 
  158 TLitePDF::~TLitePDF()
  159 {
  160    unloadLibrary();
  161 
  162    FreePtr(lastErrorMessage);
  163 }
  164 //---------------------------------------------------------------------------
  165 
  166 FARPROC TLitePDF::GetProc(const char *pProcIdent)
  167 {
  168    const char *_func = "TLitePDF::GetProc";
  169 
  170    ensureLibraryLoaded(_func);
  171 
  172    ThrowIfFail(pProcIdent != NULL);
  173    ThrowIfFail(lib != NULL);
  174 
  175    FARPROC res = NULL;
  176    res = GetProcAddress(lib, pProcIdent);
  177 
  178    char err[1024];
  179    sprintf(err, "Proc '%s' not found", pProcIdent);
  180 
  181    ThrowMessageIfFail(res != NULL, err);
  182 
  183    return res;
  184 }
  185 //---------------------------------------------------------------------------
  186 
  187 void TLitePDF::setOnError(MLitePDFErrorEvent pOnError,
  188                           void *pOnErrorUserData)
  189 {
  190    onError = pOnError;
  191    onErrorUserData = pOnErrorUserData;
  192 }
  193 //---------------------------------------------------------------------------
  194 
  195 DWORD TLitePDF::getLastErrorCode(void) const
  196 {
  197    return lastErrorCode;
  198 }
  199 //---------------------------------------------------------------------------
  200 
  201 const char *TLitePDF::getLastErrorMessage(void) const
  202 {
  203    return lastErrorMessage;
  204 }
  205 //---------------------------------------------------------------------------
  206 
  207 void TLitePDF::freeLastError(void)
  208 {
  209    FreePtr(lastErrorMessage);
  210    lastErrorCode = 0;
  211 }
  212 //---------------------------------------------------------------------------
  213 
  214 void TLitePDF::setLastError(DWORD code,
  215                             const char *msg)
  216 {
  217    freeLastError();
  218 
  219    lastErrorCode = code;
  220    if (msg) {
  221       lastErrorMessage = strdup (msg);
  222    }
  223 }
  224 //---------------------------------------------------------------------------
  225 
  226 bool TLitePDF::checkAPIVersion(unsigned int major,
  227                                unsigned int minor)
  228 {
  229    if (!lib) {
  230       return false;
  231    }
  232 
  233    char fileName[2048 + 1];
  234    DWORD fileNameLen = GetModuleFileNameA(lib, fileName, 2048);
  235    if (!fileNameLen) {
  236       return false;
  237    }
  238    fileName[fileNameLen] = 0;
  239 
  240    bool apiIsOK = false;
  241    DWORD dwVerHnd;
  242    DWORD dwVerInfoSize;
  243 
  244    dwVerInfoSize = GetFileVersionInfoSizeA(fileName, &dwVerHnd);
  245   
  246    if (dwVerInfoSize) {
  247       HANDLE  hMem;
  248       LPVOID  lpvMem;
  249 
  250       hMem = GlobalAlloc(GMEM_MOVEABLE, dwVerInfoSize);
  251       lpvMem = GlobalLock(hMem);
  252 
  253       VS_FIXEDFILEINFO *VersionInfo = NULL;
  254       if (GetFileVersionInfoA(fileName, dwVerHnd, dwVerInfoSize, lpvMem)) {
  255          UINT cchVer;
  256          BOOL fRet = VerQueryValueA(lpvMem, "\\", (void **)&VersionInfo, &cchVer);
  257 
  258          if (fRet && cchVer) {
  259             apiIsOK = ((VersionInfo->dwFileVersionMS >> 16) & 0xFFFF) == major &&
  260                        (VersionInfo->dwFileVersionMS & 0xFFFF) == minor;
  261          }
  262  
  263          GlobalUnlock(hMem);
  264          GlobalFree(hMem);
  265       }
  266    }
  267 
  268    return apiIsOK;
  269 }
  270 //---------------------------------------------------------------------------
  271 
  272 void TLitePDF::ensureLibraryLoaded(const char *_func)
  273 {
  274    if (lib) {
  275       return;
  276    }
  277 
  278    ThrowIfFail(lib == NULL);
  279    ThrowIfFail(context == NULL);
  280 
  281    #ifdef UNICODE
  282    lib = LoadLibraryW(L"litePDF.dll");
  283    #else
  284    lib = LoadLibraryA("litePDF.dll");
  285    #endif // UNICODE
  286    ThrowMessageIfFail (lib != NULL, "Failed to open litePDF.dll");
  287 
  288    if (!checkAPIVersion(LitePDF_API_Major, LitePDF_API_Minor)) {
  289       FreeLibrary (lib);
  290       lib = NULL;
  291 
  292       std::string ttmsg = std::string(_func) + ": " + "This LitePDF class is not designed for API version of litePDF.dll";
  293       throw TLitePDFException(ERROR_INVALID_DLL, ttmsg.c_str());
  294    }
  295 
  296    {
  297       typedef void (__stdcall * litePDFErrorCB)(unsigned int code, const char *msg, void *user_data);
  298 
  299       InitFunc(void *, litePDF_CreateContext, (litePDFErrorCB on_error, void *on_error_user_data));
  300 
  301       context = func (litePDFError, this);
  302    }
  303 
  304    if (!context) {
  305       FreeLibrary (lib);
  306       lib = NULL;
  307       ThrowMessageIfFail (context != NULL, "Failed to create context");
  308    } else {
  309       typedef unsigned int (__stdcall * litePDFEvalFontFlagCB)(char *inout_faceName,
  310                                                                unsigned int faceNameBufferSize,
  311                                                                void *user_data);
  312 
  313       InitFunc(BOOL, litePDF_SetEvalFontFlagCallback, (void *pctx, litePDFEvalFontFlagCB callback, void *callback_user_data));
  314 
  315       ThrowLastErrorIfFail(func (context, litePDFEvalFontFlag, this));
  316    }
  317 }
  318 //---------------------------------------------------------------------------
  319 
  320 void TLitePDF::unloadLibrary(void)
  321 {
  322    if (lib && context) {
  323       try {
  324          InitFunc(void, litePDF_FreeContext, (void *context));
  325          func(context);
  326       } catch(...) {
  327       }
  328       FreeLibrary(lib);
  329    }
  330 
  331    context = NULL;
  332    lib = NULL;
  333 }
  334 //---------------------------------------------------------------------------
  335 
  336 void TLitePDF::SetUnit(TLitePDFUnit unitValue)
  337 {
  338    const char *_func = "TLitePDF::SetUnit";
  339 
  340    ensureLibraryLoaded(_func);
  341 
  342    ThrowIfFail(lib != NULL);
  343 
  344    InitFunc(BOOL, litePDF_SetUnit, (void *pctx, unsigned int unitValue));
  345 
  346    ThrowLastErrorIfFail(func(context, (unsigned int) unitValue));
  347 }
  348 //---------------------------------------------------------------------------
  349 
  350 TLitePDFUnit TLitePDF::GetUnit(void)
  351 {
  352    const char *_func = "TLitePDF::GetUnit";
  353 
  354    ensureLibraryLoaded(_func);
  355 
  356    ThrowIfFail(lib != NULL);
  357 
  358    InitFunc(unsigned int, litePDF_GetUnit, (void *pctx));
  359 
  360    TLitePDFUnit currentUnit = (TLitePDFUnit) func(context);
  361 
  362    ThrowLastErrorIfFail(currentUnit > LitePDFUnit_Unknown && currentUnit <= LitePDFUnit_1000th_inch);
  363 
  364    return currentUnit;
  365 }
  366 //---------------------------------------------------------------------------
  367 
  368 double TLitePDF::MMToUnitEx(TLitePDFUnit useUnit,
  369                             double mmValue) const
  370 {
  371    double ratio = 1.0;
  372 
  373    switch(useUnit) {
  374    case LitePDFUnit_mm:
  375       ratio = 1.0;
  376       break;
  377    case LitePDFUnit_10th_mm:
  378       ratio = 10.0;
  379       break;
  380    case LitePDFUnit_100th_mm:
  381       ratio = 100.0;
  382       break;
  383    case LitePDFUnit_1000th_mm:
  384       ratio = 1000.0;
  385       break;
  386    case LitePDFUnit_inch:
  387       ratio = 1.0 / 25.4;
  388       break;
  389    case LitePDFUnit_10th_inch:
  390       ratio = 10.0 / 25.4;
  391       break;
  392    case LitePDFUnit_100th_inch:
  393       ratio = 100.0 / 25.4;
  394       break;
  395    case LitePDFUnit_1000th_inch:
  396       ratio = 1000.0 / 25.4;
  397       break;
  398    case LitePDFUnit_Unknown:
  399       break;
  400    }
  401 
  402    return mmValue * ratio;
  403 }
  404 //---------------------------------------------------------------------------
  405 
  406 double TLitePDF::UnitToMMEx(TLitePDFUnit useUnit,
  407                             double unitValue) const
  408 {
  409    double ratio = 1.0;
  410 
  411    switch(useUnit) {
  412    case LitePDFUnit_mm:
  413       ratio = 1.0;
  414       break;
  415    case LitePDFUnit_10th_mm:
  416       ratio = 1.0 / 10.0;
  417       break;
  418    case LitePDFUnit_100th_mm:
  419       ratio = 1.0 / 100.0;
  420       break;
  421    case LitePDFUnit_1000th_mm:
  422       ratio = 1.0 / 1000.0;
  423       break;
  424    case LitePDFUnit_inch:
  425       ratio = 25.4;
  426       break;
  427    case LitePDFUnit_10th_inch:
  428       ratio = 25.4 / 10.0;
  429       break;
  430    case LitePDFUnit_100th_inch:
  431       ratio = 25.4 / 100.0;
  432       break;
  433    case LitePDFUnit_1000th_inch:
  434       ratio = 25.4 / 1000.0;
  435       break;
  436    case LitePDFUnit_Unknown:
  437       break;
  438    }
  439 
  440    return unitValue * ratio;
  441 }
  442 //---------------------------------------------------------------------------
  443 
  444 double TLitePDF::InchToUnitEx(TLitePDFUnit useUnit,
  445                               double inchValue) const
  446 {
  447    double ratio = 1.0;
  448 
  449    switch(useUnit) {
  450    case LitePDFUnit_mm:
  451       ratio = 25.4;
  452       break;
  453    case LitePDFUnit_10th_mm:
  454       ratio = 10.0 * 25.4;
  455       break;
  456    case LitePDFUnit_100th_mm:
  457       ratio = 100.0 * 25.4;
  458       break;
  459    case LitePDFUnit_1000th_mm:
  460       ratio = 1000.0 * 25.4;
  461       break;
  462    case LitePDFUnit_inch:
  463       ratio = 1.0;
  464       break;
  465    case LitePDFUnit_10th_inch:
  466       ratio = 10.0;
  467       break;
  468    case LitePDFUnit_100th_inch:
  469       ratio = 100.0;
  470       break;
  471    case LitePDFUnit_1000th_inch:
  472       ratio = 1000.0;
  473       break;
  474    case LitePDFUnit_Unknown:
  475       break;
  476    }
  477 
  478    return inchValue * ratio;
  479 }
  480 //---------------------------------------------------------------------------
  481 
  482 double TLitePDF::UnitToInchEx(TLitePDFUnit useUnit,
  483                               double unitValue) const
  484 {
  485    double ratio = 1.0;
  486 
  487    switch(useUnit) {
  488    case LitePDFUnit_mm:
  489       ratio = 1.0 / 25.4;
  490       break;
  491    case LitePDFUnit_10th_mm:
  492       ratio = 1.0 / (25.4 * 10.0);
  493       break;
  494    case LitePDFUnit_100th_mm:
  495       ratio = 1.0 / (25.4 * 100.0);
  496       break;
  497    case LitePDFUnit_1000th_mm:
  498       ratio = 1.0 / (25.4 * 1000.0);
  499       break;
  500    case LitePDFUnit_inch:
  501       ratio = 1.0;
  502       break;
  503    case LitePDFUnit_10th_inch:
  504       ratio = 1.0 / 10.0;
  505       break;
  506    case LitePDFUnit_100th_inch:
  507       ratio = 1.0 / 100.0;
  508       break;
  509    case LitePDFUnit_1000th_inch:
  510       ratio = 1.0 / 1000.0;
  511       break;
  512    case LitePDFUnit_Unknown:
  513       break;
  514    }
  515 
  516    return unitValue * ratio;
  517 }
  518 //---------------------------------------------------------------------------
  519 
  520 double TLitePDF::MMToUnit(double mmValue)
  521 {
  522    return MMToUnitEx(GetUnit(), mmValue);
  523 }
  524 //---------------------------------------------------------------------------
  525 
  526 double TLitePDF::UnitToMM(double unitValue)
  527 {
  528    return UnitToMMEx(GetUnit(), unitValue);
  529 }
  530 //---------------------------------------------------------------------------
  531 
  532 double TLitePDF::InchToUnit(double inchValue)
  533 {
  534    return InchToUnitEx(GetUnit(), inchValue);
  535 }
  536 //---------------------------------------------------------------------------
  537 
  538 double TLitePDF::UnitToInch(double unitValue)
  539 {
  540    return UnitToInchEx(GetUnit(), unitValue);
  541 }
  542 //---------------------------------------------------------------------------
  543 
  544 void __stdcall TLitePDF::litePDFError(unsigned int code,
  545                                       const char *msg,
  546                                       void *user_data)
  547 {
  548    const char *_func = "TLitePDF::litePDFError";
  549    TLitePDF *lpdf;
  550 
  551    ThrowIfFail(user_data != NULL);
  552 
  553    lpdf = (TLitePDF *) user_data;
  554    lpdf->setLastError(code, msg);
  555    if (lpdf->onError) {
  556       lpdf->onError(code, msg, lpdf->onErrorUserData);
  557    }
  558 }
  559 //---------------------------------------------------------------------------
  560 
  561 unsigned int __stdcall TLitePDF::litePDFEvalFontFlag(char *inout_faceName,
  562                                                      unsigned int faceNameBufferSize,
  563                                                      void *user_data)
  564 {
  565    const char *_func = "TLitePDF::litePDFEvalFontFlag";
  566    TLitePDF *lpdf;
  567 
  568    ThrowIfFail(user_data != NULL);
  569 
  570    lpdf = (TLitePDF *) user_data;
  571    if (lpdf->onEvalFontFlag) {
  572       return lpdf->onEvalFontFlag(inout_faceName, faceNameBufferSize, lpdf->onEvalFontFlagUserData);
  573    }
  574 
  575    return LitePDFFontFlag_Default;
  576 }
  577 //---------------------------------------------------------------------------
  578 
  579 void TLitePDF::SetEvalFontFlagCallback(TLitePDFEvalFontFlagCB callback,
  580                                        void *userData)
  581 {
  582    onEvalFontFlag = callback;
  583    onEvalFontFlagUserData = userData;
  584 }
  585 //---------------------------------------------------------------------------
  586 
  587 void TLitePDF::PrepareEncryption(const char *userPassword,
  588                                  const char *ownerPassword,
  589                                  unsigned int permissions,
  590                                  unsigned int algorithm)
  591 {
  592    const char *_func = "TLitePDF::PrepareEncryption";
  593 
  594    ensureLibraryLoaded(_func);
  595 
  596    ThrowIfFail(lib != NULL);
  597 
  598    InitFunc(BOOL, litePDF_PrepareEncryption, (void *pctx, const char *userPassword, const char *ownerPassword, unsigned int permissions, unsigned int algorithm));
  599 
  600    ThrowLastErrorIfFail(func(context, userPassword, ownerPassword, permissions, algorithm));
  601 }
  602 //---------------------------------------------------------------------------
  603 
  604 void TLitePDF::CreateFileDocument(const char *fileName)
  605 {
  606    const char *_func = "TLitePDF::CreateFileDocument";
  607 
  608    ensureLibraryLoaded(_func);
  609 
  610    ThrowIfFail(lib != NULL);
  611 
  612    InitFunc(BOOL, litePDF_CreateFileDocument, (void *pctx, const char *fileName));
  613 
  614    ThrowLastErrorIfFail(func(context, fileName));
  615 }
  616 //---------------------------------------------------------------------------
  617 
  618 void TLitePDF::CreateFileDocumentW(const wchar_t *fileName)
  619 {
  620    const char *_func = "TLitePDF::CreateFileDocumentW";
  621 
  622    ensureLibraryLoaded(_func);
  623 
  624    ThrowIfFail(lib != NULL);
  625 
  626    InitFunc(BOOL, litePDF_CreateFileDocumentW, (void *pctx, const wchar_t *fileName));
  627 
  628    ThrowLastErrorIfFail(func(context, fileName));
  629 }
  630 //---------------------------------------------------------------------------
  631 
  632 void TLitePDF::CreateMemDocument(void)
  633 {
  634    const char *_func = "TLitePDF::CreateMemDocument";
  635 
  636    ensureLibraryLoaded(_func);
  637 
  638    ThrowIfFail(lib != NULL);
  639 
  640    InitFunc(BOOL, litePDF_CreateMemDocument, (void *pctx));
  641 
  642    ThrowLastErrorIfFail(func(context));
  643 }
  644 //---------------------------------------------------------------------------
  645 
  646 void TLitePDF::LoadFromFile(const char *fileName,
  647                             const char *password,
  648                             bool loadCompletely,
  649                             bool forUpdate)
  650 {
  651    const char *_func = "TLitePDF::LoadFromFile";
  652 
  653    ensureLibraryLoaded(_func);
  654 
  655    ThrowIfFail(lib != NULL);
  656    ThrowIfFail(fileName != NULL);
  657 
  658    InitFunc(BOOL, litePDF_LoadFromFile, (void *pctx, const char *fileName, const char *password, BOOL loadCompletely, BOOL forUpdate));
  659 
  660    ThrowLastErrorIfFail(func(context, fileName, password, loadCompletely ? TRUE : FALSE, forUpdate ? TRUE : FALSE));
  661 }
  662 //---------------------------------------------------------------------------
  663 
  664 void TLitePDF::LoadFromFileW(const wchar_t *fileName,
  665                              const char *password,
  666                              bool loadCompletely,
  667                              bool forUpdate)
  668 {
  669    const char *_func = "TLitePDF::LoadFromFileW";
  670 
  671    ensureLibraryLoaded(_func);
  672 
  673    ThrowIfFail(lib != NULL);
  674    ThrowIfFail(fileName != NULL);
  675 
  676    InitFunc(BOOL, litePDF_LoadFromFileW, (void *pctx, const wchar_t *fileName, const char *password, BOOL loadCompletely, BOOL forUpdate));
  677 
  678    ThrowLastErrorIfFail(func(context, fileName, password, loadCompletely ? TRUE : FALSE, forUpdate ? TRUE : FALSE));
  679 }
  680 //---------------------------------------------------------------------------
  681 
  682 void TLitePDF::LoadFromData(const BYTE *data,
  683                             unsigned int dataLength,
  684                             const char *password,
  685                             bool forUpdate)
  686 {
  687    const char *_func = "TLitePDF::LoadFromData";
  688 
  689    ensureLibraryLoaded(_func);
  690 
  691    ThrowIfFail(lib != NULL);
  692    ThrowIfFail(data != NULL);
  693 
  694    InitFunc(BOOL, litePDF_LoadFromData, (void *pctx, const BYTE *data, unsigned int dataLength, const char *password, BOOL forUpdate));
  695 
  696    ThrowLastErrorIfFail(func(context, data, dataLength, password, forUpdate ? TRUE : FALSE));
  697 }
  698 //---------------------------------------------------------------------------
  699 
  700 void TLitePDF::SaveToFile(const char *fileName)
  701 {
  702    const char *_func = "TLitePDF::SaveToFile";
  703 
  704    ensureLibraryLoaded(_func);
  705 
  706    ThrowIfFail(lib != NULL);
  707    ThrowIfFail(fileName != NULL);
  708 
  709    InitFunc(BOOL, litePDF_SaveToFile, (void *pctx, const char *fileName));
  710 
  711    ThrowLastErrorIfFail(func(context, fileName));
  712 }
  713 //---------------------------------------------------------------------------
  714 
  715 void TLitePDF::SaveToFileW(const wchar_t *fileName)
  716 {
  717    const char *_func = "TLitePDF::SaveToFileW";
  718 
  719    ensureLibraryLoaded(_func);
  720 
  721    ThrowIfFail(lib != NULL);
  722    ThrowIfFail(fileName != NULL);
  723 
  724    InitFunc(BOOL, litePDF_SaveToFileW, (void *pctx, const wchar_t *fileName));
  725 
  726    ThrowLastErrorIfFail(func(context, fileName));
  727 }
  728 //---------------------------------------------------------------------------
  729 
  730 bool TLitePDF::SaveToData(BYTE *data,
  731                           unsigned int *dataLength)
  732 {
  733    const char *_func = "TLitePDF::SaveToData";
  734 
  735    ensureLibraryLoaded(_func);
  736 
  737    ThrowIfFail(lib != NULL);
  738    ThrowIfFail(dataLength != NULL);
  739 
  740    InitFunc(BOOL, litePDF_SaveToData, (void *pctx, BYTE *data, unsigned int *dataLength));
  741 
  742    BOOL succeeded = func(context, data, dataLength);
  743 
  744    return succeeded ? true : false;
  745 }
  746 //---------------------------------------------------------------------------
  747 
  748 void TLitePDF::Close(void)
  749 {
  750    const char *_func = "TLitePDF::Close";
  751 
  752    ensureLibraryLoaded(_func);
  753 
  754    ThrowIfFail(lib != NULL);
  755 
  756    InitFunc(void, litePDF_Close, (void *pctx));
  757 
  758    func(context);
  759 }
  760 //---------------------------------------------------------------------------
  761 
  762 unsigned int TLitePDF::GetPageCount(void)
  763 {
  764    const char *_func = "TLitePDF::GetPageCount";
  765 
  766    ensureLibraryLoaded(_func);
  767 
  768    ThrowIfFail(lib != NULL);
  769 
  770    InitFunc(BOOL, litePDF_GetPageCount, (void *pctx, unsigned int *pageCount));
  771 
  772    unsigned int pageCount = 0;
  773    ThrowLastErrorIfFail(func(context, &pageCount));
  774 
  775    return pageCount;
  776 }
  777 //---------------------------------------------------------------------------
  778 
  779 void TLitePDF::GetPageSize(unsigned int pageIndex,
  780                            unsigned int *width_u,
  781                            unsigned int *height_u)
  782 {
  783    const char *_func = "TLitePDF::GetPageSize";
  784 
  785    ensureLibraryLoaded(_func);
  786 
  787    ThrowIfFail(lib != NULL);
  788    ThrowIfFail(width_u != NULL);
  789    ThrowIfFail(height_u != NULL);
  790 
  791    InitFunc(BOOL, litePDF_GetPageSize, (void *pctx, unsigned int pageIndex, unsigned int *width_u, unsigned int *height_u));
  792 
  793    ThrowLastErrorIfFail(func(context, pageIndex, width_u, height_u));
  794 }
  795 //---------------------------------------------------------------------------
  796 
  797 int TLitePDF::GetPageRotation(unsigned int pageIndex)
  798 {
  799    const char *_func = "TLitePDF::GetPageRotation";
  800 
  801    ensureLibraryLoaded(_func);
  802 
  803    ThrowIfFail(lib != NULL);
  804 
  805    int degrees = 0;
  806 
  807    InitFunc(BOOL, litePDF_GetPageRotation, (void *pctx, unsigned int pageIndex, int *out_degrees));
  808 
  809    ThrowLastErrorIfFail(func(context, pageIndex, &degrees));
  810 
  811    return degrees;
  812 }
  813 //---------------------------------------------------------------------------
  814 
  815 void TLitePDF::SetPageRotation(unsigned int pageIndex,
  816                                int degrees)
  817 {
  818    const char *_func = "TLitePDF::SetPageRotation";
  819 
  820    ensureLibraryLoaded(_func);
  821 
  822    ThrowIfFail(lib != NULL);
  823 
  824    InitFunc(BOOL, litePDF_SetPageRotation, (void *pctx, unsigned int pageIndex, int degrees));
  825 
  826    ThrowLastErrorIfFail(func(context, pageIndex, degrees));
  827 }
  828 //---------------------------------------------------------------------------
  829 
  830 
  831 HDC TLitePDF::AddPage(unsigned int width_u,
  832                       unsigned int height_u,
  833                       unsigned int width_px,
  834                       unsigned int height_px,
  835                       unsigned int drawFlags)
  836 {
  837    const char *_func = "TLitePDF::AddPage";
  838 
  839    ensureLibraryLoaded(_func);
  840 
  841    ThrowIfFail(lib != NULL);
  842 
  843    InitFunc(HDC, litePDF_AddPage, (void *pctx, unsigned int width_u, unsigned int height_u, unsigned int width_px, unsigned int height_px, unsigned int drawFlags));
  844 
  845    HDC res = func (context, width_u, height_u, width_px, height_px, drawFlags);
  846    ThrowLastErrorIfFail(res != NULL);
  847 
  848    return res;
  849 }
  850 //---------------------------------------------------------------------------
  851 
  852 HDC TLitePDF::InsertPage(unsigned int pageIndex,
  853                          unsigned int width_u,
  854                          unsigned int height_u,
  855                          unsigned int width_px,
  856                          unsigned int height_px,
  857                          unsigned int drawFlags)
  858 {
  859    const char *_func = "TLitePDF::InsertPage";
  860 
  861    ensureLibraryLoaded(_func);
  862 
  863    ThrowIfFail(lib != NULL);
  864 
  865    InitFunc(HDC, litePDF_InsertPage, (void *pctx, unsigned int pageIndex, unsigned int width_u, unsigned int height_u, unsigned int width_px, unsigned int height_px, unsigned int drawFlags));
  866 
  867    HDC res = func(context, pageIndex, width_u, height_u, width_px, height_px, drawFlags);
  868    ThrowLastErrorIfFail(res != NULL);
  869 
  870    return res;
  871 }
  872 //---------------------------------------------------------------------------
  873 
  874 HDC TLitePDF::UpdatePage(unsigned int pageIndex,
  875                          unsigned int width_px,
  876                          unsigned int height_px,
  877                          unsigned int drawFlags)
  878 {
  879    const char *_func = "TLitePDF::UpdatePage";
  880 
  881    ensureLibraryLoaded(_func);
  882 
  883    ThrowIfFail(lib != NULL);
  884 
  885    InitFunc(HDC, litePDF_UpdatePage, (void *pctx, unsigned int pageIndex, unsigned int width_px, unsigned int height_px, unsigned int drawFlags));
  886 
  887    HDC res = func(context, pageIndex, width_px, height_px, drawFlags);
  888    ThrowLastErrorIfFail(res != NULL);
  889 
  890    return res;
  891 }
  892 //---------------------------------------------------------------------------
  893 
  894 void TLitePDF::FinishPage(HDC hDC)
  895 {
  896    const char *_func = "TLitePDF::FinishPage";
  897 
  898    ensureLibraryLoaded(_func);
  899 
  900    ThrowIfFail(lib != NULL);
  901    ThrowIfFail(hDC != NULL);
  902 
  903    InitFunc(BOOL, litePDF_FinishPage, (void *pctx, HDC hDC));
  904 
  905    ThrowLastErrorIfFail(func(context, hDC));
  906 }
  907 //---------------------------------------------------------------------------
  908 
  909 HDC TLitePDF::AddResource(unsigned int width_u,
  910                           unsigned int height_u,
  911                           unsigned int width_px,
  912                           unsigned int height_px,
  913                           unsigned int drawFlags)
  914 {
  915    const char *_func = "TLitePDF::AddResource";
  916 
  917    ensureLibraryLoaded(_func);
  918 
  919    ThrowIfFail(lib != NULL);
  920 
  921    InitFunc(HDC, litePDF_AddResource, (void *pctx, unsigned int width_u, unsigned int height_u, unsigned int width_px, unsigned int height_px, unsigned int drawFlags));
  922 
  923    HDC res = func(context, width_u, height_u, width_px, height_px, drawFlags);
  924    ThrowLastErrorIfFail(res != NULL);
  925 
  926    return res;
  927 }
  928 //---------------------------------------------------------------------------
  929 
  930 unsigned int TLitePDF::FinishResource(HDC hDC)
  931 {
  932    const char *_func = "TLitePDF::FinishResource";
  933 
  934    ensureLibraryLoaded(_func);
  935 
  936    ThrowIfFail(lib != NULL);
  937    ThrowIfFail(hDC != NULL);
  938 
  939    InitFunc(unsigned int, litePDF_FinishResource, (void *pctx, HDC hDC));
  940 
  941    unsigned int resourceID = func(context, hDC);
  942    ThrowLastErrorIfFail(resourceID != 0);
  943 
  944    return resourceID;
  945 }
  946 //---------------------------------------------------------------------------
  947 
  948 void TLitePDF::DeletePage(unsigned int pageIndex)
  949 {
  950    const char *_func = "TLitePDF::DeletePage";
  951 
  952    ensureLibraryLoaded(_func);
  953 
  954    ThrowIfFail(lib != NULL);
  955 
  956    InitFunc(BOOL, litePDF_DeletePage, (void *pctx, unsigned int pageIndex));
  957 
  958    ThrowLastErrorIfFail(func(context, pageIndex));
  959 }
  960 //---------------------------------------------------------------------------
  961 
  962 void TLitePDF::AddPagesFrom(litePDF::TLitePDF *from,
  963                             unsigned int pageIndex,
  964                             unsigned int pageCount)
  965 {
  966    const char *_func = "TLitePDF::AddPagesFrom";
  967 
  968    ensureLibraryLoaded(_func);
  969 
  970    ThrowIfFail(lib != NULL);
  971    ThrowIfFail(from != NULL);
  972    ThrowIfFail(from != this);
  973    ThrowIfFail(from->context != NULL);
  974 
  975    InitFunc(BOOL, litePDF_AddPagesFrom, (void *pctx, void *pctx_from, unsigned int pageIndex, unsigned int pageCount));
  976 
  977    ThrowLastErrorIfFail(func(context, from->context, pageIndex, pageCount));
  978 }
  979 //---------------------------------------------------------------------------
  980 
  981 void TLitePDF::InsertPageFrom(unsigned int pageIndexTo,
  982                               litePDF::TLitePDF *from,
  983                               unsigned int pageIndexFrom)
  984 {
  985    const char *_func = "TLitePDF::InsertPageFrom";
  986 
  987    ensureLibraryLoaded(_func);
  988 
  989    ThrowIfFail(lib != NULL);
  990    ThrowIfFail(from != NULL);
  991    ThrowIfFail(from != this);
  992    ThrowIfFail(from->context != NULL);
  993 
  994    InitFunc(BOOL, litePDF_InsertPageFrom, (void *pctx, unsigned int pageIndexTo, void *pctx_from, unsigned int pageIndexFrom));
  995 
  996    ThrowLastErrorIfFail(func(context, pageIndexTo, from->context, pageIndexFrom));
  997 }
  998 //---------------------------------------------------------------------------
  999 
 1000 unsigned int TLitePDF::AddPageFromAsResource(litePDF::TLitePDF *from,
 1001                                              unsigned int pageIndex,
 1002                                              bool useTrimBox)
 1003 {
 1004    const char *_func = "TLitePDF::AddPageFromAsResource";
 1005 
 1006    ensureLibraryLoaded(_func);
 1007 
 1008    ThrowIfFail(lib != NULL);
 1009    ThrowIfFail(from != NULL);
 1010    ThrowIfFail(from != this);
 1011    ThrowIfFail(from->context != NULL);
 1012 
 1013    InitFunc(unsigned int, litePDF_AddPageFromAsResource, (void *pctx, void *pctx_from, unsigned int pageIndex, BOOL useTrimBox));
 1014 
 1015    unsigned int resourceID = func(context, from->context, pageIndex, useTrimBox ? TRUE : FALSE);
 1016    ThrowLastErrorIfFail(resourceID != 0);
 1017 
 1018    return resourceID;
 1019 }
 1020 //---------------------------------------------------------------------------
 1021 
 1022 unsigned int TLitePDF::PageToResource(unsigned int pageIndex)
 1023 {
 1024    const char *_func = "TLitePDF::PageToResource";
 1025 
 1026    ensureLibraryLoaded(_func);
 1027 
 1028    ThrowIfFail(lib != NULL);
 1029 
 1030    InitFunc(unsigned int, litePDF_PageToResource, (void *pctx, unsigned int pageIndex));
 1031 
 1032    unsigned int resourceID = func(context, pageIndex);
 1033    ThrowLastErrorIfFail(resourceID != 0);
 1034 
 1035    return resourceID;
 1036 }
 1037 //---------------------------------------------------------------------------
 1038 
 1039 void TLitePDF::GetResourceSize(unsigned int resourceID,
 1040                                unsigned int *width_u,
 1041                                unsigned int *height_u)
 1042 {
 1043    const char *_func = "TLitePDF::GetResourceSize";
 1044 
 1045    ensureLibraryLoaded(_func);
 1046 
 1047    ThrowIfFail(lib != NULL);
 1048    ThrowIfFail(width_u != NULL);
 1049    ThrowIfFail(height_u != NULL);
 1050 
 1051    InitFunc(BOOL, litePDF_GetResourceSize, (void *pctx, unsigned int resourceID, unsigned int *width_u, unsigned int *height_u));
 1052 
 1053    ThrowLastErrorIfFail(func(context, resourceID, width_u, height_u));
 1054 }
 1055 //---------------------------------------------------------------------------
 1056 
 1057 void TLitePDF::DrawResource(unsigned int resourceID,
 1058                             unsigned int pageIndex,
 1059                             TLitePDFUnit unitValue,
 1060                             int x,
 1061                             int y,
 1062                             int scaleX,
 1063                             int scaleY)
 1064 {
 1065    const char *_func = "TLitePDF::DrawResource";
 1066 
 1067    ensureLibraryLoaded(_func);
 1068 
 1069    ThrowIfFail(lib != NULL);
 1070 
 1071    InitFunc(BOOL, litePDF_DrawResource, (void *pctx, unsigned int resourceID, unsigned int unitValue, unsigned int pageIndex, int x, int y, int scaleX, int scaleY));
 1072 
 1073    ThrowLastErrorIfFail(func(context, resourceID, pageIndex, (unsigned int) unitValue, x, y, scaleX, scaleY));
 1074 }
 1075 //---------------------------------------------------------------------------
 1076 
 1077 void TLitePDF::DrawResourceWithMatrix(unsigned int resourceID,
 1078                                       unsigned int pageIndex,
 1079                                       double a,
 1080                                       double b,
 1081                                       double c,
 1082                                       double d,
 1083                                       double e,
 1084                                       double f)
 1085 {
 1086    const char *_func = "TLitePDF::DrawResourceWithMatrix";
 1087 
 1088    ensureLibraryLoaded(_func);
 1089 
 1090    ThrowIfFail(lib != NULL);
 1091 
 1092    InitFunc(BOOL, litePDF_DrawResourceWithMatrix, (void *pctx, unsigned int resourceID, unsigned int pageIndex, int a, int b, int c, int d, int e, int f));
 1093 
 1094    int i_a = a * 1000.0, i_b = b * 1000.0, i_c = c * 1000.0, i_d = d * 1000.0, i_e = e * 1000.0, i_f = f * 1000.0;
 1095 
 1096    ThrowLastErrorIfFail(func(context, resourceID, pageIndex, i_a, i_b, i_c, i_d, i_e, i_f));
 1097 }
 1098 //---------------------------------------------------------------------------
 1099 
 1100 void TLitePDF::SetDocumentInfo(const char *name,
 1101                                const wchar_t *value)
 1102 {
 1103    const char *_func = "TLitePDF::SetDocumentInfo";
 1104 
 1105    ensureLibraryLoaded(_func);
 1106 
 1107    ThrowIfFail(lib != NULL);
 1108    ThrowIfFail(name != NULL);
 1109    ThrowIfFail(value != NULL);
 1110 
 1111    InitFunc(BOOL, litePDF_SetDocumentInfo, (void *pctx, const char *name, const wchar_t *value));
 1112 
 1113    ThrowLastErrorIfFail(func(context, name, value));
 1114 }
 1115 //---------------------------------------------------------------------------
 1116 
 1117 bool TLitePDF::GetDocumentInfoExists(const char *name)
 1118 {
 1119    const char *_func = "TLitePDF::GetDocumentInfoExists";
 1120 
 1121    ensureLibraryLoaded(_func);
 1122 
 1123    ThrowIfFail(lib != NULL);
 1124    ThrowIfFail(name != NULL);
 1125 
 1126    InitFunc(BOOL, litePDF_GetDocumentInfoExists, (void *pctx, const char *name, BOOL *pExists));
 1127 
 1128    BOOL exists = FALSE;
 1129 
 1130    ThrowLastErrorIfFail(func(context, name, &exists));
 1131 
 1132    return exists ? true : false;
 1133 }
 1134 //---------------------------------------------------------------------------
 1135 
 1136 std::wstring TLitePDF::GetDocumentInfo(const char *name)
 1137 {
 1138    const char *_func = "TLitePDF::GetDocumentInfo";
 1139 
 1140    ensureLibraryLoaded(_func);
 1141 
 1142    ThrowIfFail(lib != NULL);
 1143    ThrowIfFail(name != NULL);
 1144 
 1145    InitFunc(BOOL, litePDF_GetDocumentInfo, (void *pctx, const char *name, wchar_t *value, unsigned int *valueLength));
 1146 
 1147    unsigned int valueLength = 0;
 1148    ThrowLastErrorIfFail(func(context, name, NULL, &valueLength));
 1149 
 1150    wchar_t *buff = (wchar_t *) malloc(sizeof(wchar_t) * (valueLength + 1));
 1151    ThrowMessageIfFail(buff != NULL, "Out of memory!");
 1152 
 1153    std::wstring value;
 1154 
 1155    if (func(context, name, buff, &valueLength)) {
 1156       buff[valueLength] = 0;
 1157       value = buff;
 1158       free (buff);
 1159    } else {
 1160       free (buff);
 1161 
 1162       // always false
 1163       ThrowLastErrorIfFail(buff == NULL);
 1164    }
 1165 
 1166    return value;
 1167 }
 1168 //---------------------------------------------------------------------------
 1169 
 1170 bool TLitePDF::GetDocumentIsSigned(void)
 1171 {
 1172    const char *_func = "TLitePDF::GetDocumentIsSigned";
 1173 
 1174    ensureLibraryLoaded(_func);
 1175 
 1176    ThrowIfFail(lib != NULL);
 1177 
 1178    InitFunc(BOOL, litePDF_GetDocumentIsSigned, (void *pctx, BOOL *pIsSigned));
 1179 
 1180    BOOL isSigned = FALSE;
 1181 
 1182    ThrowLastErrorIfFail(func(context, &isSigned));
 1183 
 1184    return isSigned ? true : false;
 1185 }
 1186 //---------------------------------------------------------------------------
 1187 
 1188 unsigned int TLitePDF::GetSignatureCount(void)
 1189 {
 1190    const char *_func = "TLitePDF::GetSignatureCount";
 1191 
 1192    ensureLibraryLoaded(_func);
 1193 
 1194    ThrowIfFail(lib != NULL);
 1195 
 1196    InitFunc(BOOL, litePDF_GetSignatureCount, (void *pctx, unsigned int *pCount));
 1197 
 1198    unsigned int count = 0;
 1199 
 1200    ThrowLastErrorIfFail(func(context, &count));
 1201 
 1202    return count;
 1203 }
 1204 //---------------------------------------------------------------------------
 1205 
 1206 std::string TLitePDF::GetSignatureName(unsigned int index)
 1207 {
 1208    const char *_func = "TLitePDF::GetSignatureName";
 1209 
 1210    ensureLibraryLoaded(_func);
 1211 
 1212    ThrowIfFail(lib != NULL);
 1213 
 1214    InitFunc(BOOL, litePDF_GetSignatureName, (void *pctx, unsigned int index, char *name, unsigned int *nameLength));
 1215 
 1216    unsigned int nameLength = 0;
 1217    ThrowLastErrorIfFail(func(context, index, NULL, &nameLength));
 1218 
 1219    nameLength++;
 1220 
 1221    char *buff = (char *) malloc(sizeof(char) * (nameLength));
 1222    ThrowMessageIfFail(buff != NULL, "Out of memory!");
 1223 
 1224    std::string name;
 1225 
 1226    if (func(context, index, buff, &nameLength)) {
 1227       buff[nameLength] = 0;
 1228       name = buff;
 1229       free (buff);
 1230    } else {
 1231       free (buff);
 1232 
 1233       // always false
 1234       ThrowLastErrorIfFail(buff == NULL);
 1235    }
 1236 
 1237    return name;
 1238 }
 1239 //---------------------------------------------------------------------------
 1240 
 1241 unsigned int TLitePDF::CreateSignature(const char *name,
 1242                                        unsigned int annotationPageIndex,
 1243                                        int annotationX_u,
 1244                                        int annotationY_u,
 1245                                        int annotationWidth_u,
 1246                                        int annotationHeight_u,
 1247                                        unsigned int annotationFlags)
 1248 {
 1249    const char *_func = "TLitePDF::CreateSignature";
 1250 
 1251    ensureLibraryLoaded(_func);
 1252 
 1253    ThrowIfFail(lib != NULL);
 1254    ThrowIfFail(name != NULL);
 1255 
 1256    InitFunc(BOOL, litePDF_CreateSignature, (void *pctx,
 1257                                             const char *name,
 1258                                             unsigned int annotationPageIndex,
 1259                                             int annotationX_u,
 1260                                             int annotationY_u,
 1261                                             int annotationWidth_u,
 1262                                             int annotationHeight_u,
 1263                                             unsigned int annotationFlags,
 1264                                             unsigned int *pAddedIndex));
 1265 
 1266    unsigned int addedIndex = -1;
 1267 
 1268    ThrowLastErrorIfFail(func(context,
 1269       name,
 1270       annotationPageIndex,
 1271       annotationX_u,
 1272       annotationY_u,
 1273       annotationWidth_u,
 1274       annotationHeight_u,
 1275       annotationFlags,
 1276       &addedIndex));
 1277 
 1278    return addedIndex;
 1279 }
 1280 //---------------------------------------------------------------------------
 1281 
 1282 bool TLitePDF::GetSignatureHasData(unsigned int index)
 1283 {
 1284    const char *_func = "TLitePDF::GetSignatureHasData";
 1285 
 1286    ensureLibraryLoaded(_func);
 1287 
 1288    ThrowIfFail(lib != NULL);
 1289 
 1290    InitFunc(BOOL, litePDF_GetSignatureHasData, (void *pctx, unsigned int index, BOOL *pHasData));
 1291 
 1292    BOOL hasData = FALSE;
 1293 
 1294    ThrowLastErrorIfFail(func(context, index, &hasData));
 1295 
 1296    return hasData ? true : false;
 1297 }
 1298 //---------------------------------------------------------------------------
 1299 
 1300 bool TLitePDF::GetSignatureData(unsigned int index,
 1301                                 BYTE *data,
 1302                                 unsigned int *dataLength)
 1303 {
 1304    const char *_func = "TLitePDF::GetSignatureData";
 1305 
 1306    ensureLibraryLoaded(_func);
 1307 
 1308    ThrowIfFail(lib != NULL);
 1309    ThrowIfFail(dataLength != NULL);
 1310 
 1311    InitFunc(BOOL, litePDF_GetSignatureData, (void *pctx, unsigned int index, BYTE *data, unsigned int *dataLength));
 1312 
 1313    BOOL succeeded = func(context, index, data, dataLength);
 1314 
 1315    return succeeded ? true : false;
 1316 }
 1317 //---------------------------------------------------------------------------
 1318 
 1319 bool TLitePDF::GetSignatureRanges(unsigned int index,
 1320                                   unsigned __int64 *pRangesArray,
 1321                                   unsigned int *pRangesArrayLength)
 1322 {
 1323    const char *_func = "TLitePDF::GetSignatureRanges";
 1324 
 1325    ensureLibraryLoaded(_func);
 1326 
 1327    ThrowIfFail(lib != NULL);
 1328    ThrowIfFail(pRangesArrayLength != NULL);
 1329 
 1330    InitFunc(BOOL, litePDF_GetSignatureRanges, (void *pctx, unsigned int index, unsigned __int64 *pRangesArray, unsigned int *pRangesArrayLength));
 1331 
 1332    BOOL succeeded = func(context, index, pRangesArray, pRangesArrayLength);
 1333 
 1334    return succeeded ? true : false;
 1335 }
 1336 //---------------------------------------------------------------------------
 1337 
 1338 void TLitePDF::SetSignatureDate(unsigned int index,
 1339                                 __int64 dateOfSign)
 1340 {
 1341    const char *_func = "TLitePDF::SetSignatureDate";
 1342 
 1343    ensureLibraryLoaded(_func);
 1344 
 1345    ThrowIfFail(lib != NULL);
 1346 
 1347    InitFunc(BOOL, litePDF_SetSignatureDate, (void *pctx, unsigned int index, __int64 dateOfSign));
 1348 
 1349    ThrowLastErrorIfFail(func(context, index, dateOfSign));
 1350 }
 1351 //---------------------------------------------------------------------------
 1352 
 1353 __int64 TLitePDF::GetSignatureDate(unsigned int index)
 1354 {
 1355    const char *_func = "TLitePDF::GetSignatureDate";
 1356 
 1357    ensureLibraryLoaded(_func);
 1358 
 1359    ThrowIfFail(lib != NULL);
 1360 
 1361    InitFunc(BOOL, litePDF_GetSignatureDate, (void *pctx, unsigned int index, __int64 *pDateOfSign));
 1362 
 1363    __int64 dateOfSign = 0;
 1364 
 1365    ThrowLastErrorIfFail(func(context, index, &dateOfSign));
 1366 
 1367    return dateOfSign;
 1368 }
 1369 //---------------------------------------------------------------------------
 1370 
 1371 void TLitePDF::SetSignatureReason(unsigned int index,
 1372                                   const wchar_t *reason)
 1373 {
 1374    const char *_func = "TLitePDF::SetSignatureReason";
 1375 
 1376    ensureLibraryLoaded(_func);
 1377 
 1378    ThrowIfFail(lib != NULL);
 1379 
 1380    InitFunc(BOOL, litePDF_SetSignatureReason, (void *pctx, unsigned int index, const wchar_t *reason));
 1381 
 1382    ThrowLastErrorIfFail(func(context, index, reason));
 1383 }
 1384 //---------------------------------------------------------------------------
 1385 
 1386 std::wstring TLitePDF::GetSignatureReason(unsigned int index)
 1387 {
 1388    const char *_func = "TLitePDF::GetSignatureReason";
 1389 
 1390    ensureLibraryLoaded(_func);
 1391 
 1392    ThrowIfFail(lib != NULL);
 1393 
 1394    InitFunc(BOOL, litePDF_GetSignatureReason, (void *pctx, unsigned int index, wchar_t *value, unsigned int *valueLength));
 1395 
 1396    unsigned int valueLength = 0;
 1397    ThrowLastErrorIfFail(func(context, index, NULL, &valueLength));
 1398 
 1399    valueLength++;
 1400 
 1401    wchar_t *buff = (wchar_t *) malloc(sizeof(wchar_t) * (valueLength));
 1402    ThrowMessageIfFail(buff != NULL, "Out of memory!");
 1403 
 1404    std::wstring value;
 1405 
 1406    if (func(context, index, buff, &valueLength)) {
 1407       buff[valueLength] = 0;
 1408       value = buff;
 1409       free (buff);
 1410    } else {
 1411       free (buff);
 1412 
 1413       // always false
 1414       ThrowLastErrorIfFail(buff == NULL);
 1415    }
 1416 
 1417    return value;
 1418 }
 1419 //---------------------------------------------------------------------------
 1420 
 1421 void TLitePDF::SetSignatureLocation(unsigned int index,
 1422                                     const wchar_t *location)
 1423 {
 1424    const char *_func = "TLitePDF::SetSignatureLocation";
 1425 
 1426    ensureLibraryLoaded(_func);
 1427 
 1428    ThrowIfFail(lib != NULL);
 1429 
 1430    InitFunc(BOOL, litePDF_SetSignatureLocation, (void *pctx, unsigned int index, const wchar_t *location));
 1431 
 1432    ThrowLastErrorIfFail(func(context, index, location));
 1433 }
 1434 //---------------------------------------------------------------------------
 1435 
 1436 std::wstring TLitePDF::GetSignatureLocation(unsigned int index)
 1437 {
 1438    const char *_func = "TLitePDF::GetSignatureLocation";
 1439 
 1440    ensureLibraryLoaded(_func);
 1441 
 1442    ThrowIfFail(lib != NULL);
 1443 
 1444    InitFunc(BOOL, litePDF_GetSignatureLocation, (void *pctx, unsigned int index, wchar_t *value, unsigned int *valueLength));
 1445 
 1446    unsigned int valueLength = 0;
 1447    ThrowLastErrorIfFail(func(context, index, NULL, &valueLength));
 1448 
 1449    valueLength++;
 1450 
 1451    wchar_t *buff = (wchar_t *) malloc(sizeof(wchar_t) * (valueLength));
 1452    ThrowMessageIfFail(buff != NULL, "Out of memory!");
 1453 
 1454    std::wstring value;
 1455 
 1456    if (func(context, index, buff, &valueLength)) {
 1457       buff[valueLength] = 0;
 1458       value = buff;
 1459       free (buff);
 1460    } else {
 1461       free (buff);
 1462 
 1463       // always false
 1464       ThrowLastErrorIfFail(buff == NULL);
 1465    }
 1466 
 1467    return value;
 1468 }
 1469 //---------------------------------------------------------------------------
 1470 
 1471 void TLitePDF::SetSignatureCreator(unsigned int index,
 1472                                    const char *creator)
 1473 {
 1474    const char *_func = "TLitePDF::SetSignatureCreator";
 1475 
 1476    ensureLibraryLoaded(_func);
 1477 
 1478    ThrowIfFail(lib != NULL);
 1479 
 1480    InitFunc(BOOL, litePDF_SetSignatureCreator, (void *pctx, unsigned int index, const char *creator));
 1481 
 1482    ThrowLastErrorIfFail(func(context, index, creator));
 1483 }
 1484 //---------------------------------------------------------------------------
 1485 
 1486 std::string TLitePDF::GetSignatureCreator(unsigned int index)
 1487 {
 1488    const char *_func = "TLitePDF::GetSignatureCreator";
 1489 
 1490    ensureLibraryLoaded(_func);
 1491 
 1492    ThrowIfFail(lib != NULL);
 1493 
 1494    InitFunc(BOOL, litePDF_GetSignatureCreator, (void *pctx, unsigned int index, char *value, unsigned int *valueLength));
 1495 
 1496    unsigned int valueLength = 0;
 1497    ThrowLastErrorIfFail(func(context, index, NULL, &valueLength));
 1498 
 1499    valueLength++;
 1500 
 1501    char *buff = (char *) malloc(sizeof(char) * (valueLength));
 1502    ThrowMessageIfFail(buff != NULL, "Out of memory!");
 1503 
 1504    std::string value;
 1505 
 1506    if (func(context, index, buff, &valueLength)) {
 1507       buff[valueLength] = 0;
 1508       value = buff;
 1509       free (buff);
 1510    } else {
 1511       free (buff);
 1512 
 1513       // always false
 1514       ThrowLastErrorIfFail(buff == NULL);
 1515    }
 1516 
 1517    return value;
 1518 }
 1519 //---------------------------------------------------------------------------
 1520 
 1521 void TLitePDF::SetSignatureAppearance(unsigned int index,
 1522                                       TLitePDFAppearance appearanceType,
 1523                                       unsigned int resourceID,
 1524                                       int offsetX_u,
 1525                                       int offsetY_u)
 1526 {
 1527    const char *_func = "TLitePDF::SetSignatureAppearance";
 1528 
 1529    ensureLibraryLoaded(_func);
 1530 
 1531    ThrowIfFail(lib != NULL);
 1532 
 1533    InitFunc(BOOL, litePDF_SetSignatureAppearance, (void *pctx,
 1534                                                    unsigned int index,
 1535                                                    unsigned int appearanceType,
 1536                                                    unsigned int resourceID,
 1537                                                    int offsetX_u,
 1538                                                    int offsetY_u));
 1539 
 1540    unsigned int apType;
 1541 
 1542    if (appearanceType == LitePDFAppearance_Rollover) {
 1543       apType = 1;
 1544    } else if (appearanceType == LitePDFAppearance_Down) {
 1545       apType = 2;
 1546    } else { // LitePDFAppearance_Normal
 1547       apType = 0;
 1548    }
 1549 
 1550    ThrowLastErrorIfFail(func(context, index, apType, resourceID, offsetX_u, offsetY_u));
 1551 }
 1552 //---------------------------------------------------------------------------
 1553 
 1554 void TLitePDF::SetSignatureCertification(unsigned int index,
 1555                                          TLitePDFCertificationPermission permission)
 1556 {
 1557    const char *_func = "TLitePDF::SetSignatureCertification";
 1558 
 1559    ensureLibraryLoaded(_func);
 1560 
 1561    ThrowIfFail(lib != NULL);
 1562 
 1563    InitFunc(BOOL, litePDF_SetSignatureCertification, (void *pctx,
 1564                                                       unsigned int index,
 1565                                                       unsigned int permission));
 1566 
 1567    unsigned int perm;
 1568 
 1569    if (permission == LitePDFCertificationPermission_NoPerms) {
 1570       perm = 1;
 1571    } else if (permission == LitePDFCertificationPermission_FormFill) {
 1572       perm = 2;
 1573    } else { // LitePDFCertificationPermission_Annotations
 1574       perm = 3;
 1575    }
 1576 
 1577    ThrowLastErrorIfFail(func(context, index, perm));
 1578 }
 1579 //---------------------------------------------------------------------------
 1580 
 1581 void TLitePDF::SetSignatureSize(unsigned int requestBytes)
 1582 {
 1583    const char *_func = "TLitePDF::SetSignatureSize";
 1584 
 1585    ensureLibraryLoaded(_func);
 1586 
 1587    ThrowIfFail(lib != NULL);
 1588 
 1589    InitFunc(BOOL, litePDF_SetSignatureSize, (void *pctx, unsigned int requestBytes));
 1590 
 1591    ThrowLastErrorIfFail(func(context, requestBytes));
 1592 }
 1593 //---------------------------------------------------------------------------
 1594 
 1595 void TLitePDF::SetSignatureHash(TLitePDFSignatureHash signatureHash)
 1596 {
 1597    const char *_func = "TLitePDF::SetSignatureHash";
 1598 
 1599    ensureLibraryLoaded(_func);
 1600 
 1601    ThrowIfFail(lib != NULL);
 1602 
 1603    InitFunc(BOOL, litePDF_SetSignatureHash, (void *pctx, unsigned int signatureHash));
 1604 
 1605    unsigned int hashAlgo;
 1606 
 1607    if (signatureHash == LitePDFSignatureHash_SHA1)
 1608       hashAlgo = 1;
 1609    else if (signatureHash == LitePDFSignatureHash_SHA256)
 1610       hashAlgo = 2;
 1611    else if (signatureHash == LitePDFSignatureHash_SHA384)
 1612       hashAlgo = 3;
 1613    else // LitePDFSignatureHash_SHA512
 1614       hashAlgo = 4;
 1615 
 1616 
 1617    ThrowLastErrorIfFail(func(context, hashAlgo));
 1618 }
 1619 
 1620 //---------------------------------------------------------------------------
 1621 
 1622 void TLitePDF::AddSignerPFX(const BYTE *pfxData,
 1623                             unsigned int pfxDataLength,
 1624                             const char *pfxPassword)
 1625 {
 1626    const char *_func = "TLitePDF::AddSignerPFX";
 1627 
 1628    ensureLibraryLoaded(_func);
 1629 
 1630    ThrowIfFail(lib != NULL);
 1631 
 1632    InitFunc(BOOL, litePDF_AddSignerPFX, (void *pctx,
 1633                                          const BYTE *pfxData,
 1634                                          unsigned int pfxDataLength,
 1635                                          const char *pfxPassword));
 1636 
 1637    ThrowLastErrorIfFail(func(context, pfxData, pfxDataLength, pfxPassword));
 1638 }
 1639 //---------------------------------------------------------------------------
 1640 
 1641 void TLitePDF::AddSignerPEM(const BYTE *pemData,
 1642                             unsigned int pemDataLength,
 1643                             const BYTE *pkeyData,
 1644                             unsigned int pkeyDataLength,
 1645                             const char *pkeyPassword)
 1646 {
 1647    const char *_func = "TLitePDF::AddSignerPEM";
 1648 
 1649    ensureLibraryLoaded(_func);
 1650 
 1651    ThrowIfFail(lib != NULL);
 1652 
 1653    InitFunc(BOOL, litePDF_AddSignerPEM, (void *pctx,
 1654                                          const BYTE *pemData,
 1655                                          unsigned int pemDataLength,
 1656                                          const BYTE *pkeyData,
 1657                                          unsigned int pkeyDataLength,
 1658                                          const char *pkeyPassword));
 1659 
 1660    ThrowLastErrorIfFail(func(context, pemData, pemDataLength, pkeyData, pkeyDataLength, pkeyPassword));
 1661 }
 1662 //---------------------------------------------------------------------------
 1663 
 1664 void TLitePDF::SaveToFileWithSign(const char *fileName,
 1665                                   unsigned int signatureIndex)
 1666 {
 1667    const char *_func = "TLitePDF::SaveToFileWithSign";
 1668 
 1669    ensureLibraryLoaded(_func);
 1670 
 1671    ThrowIfFail(lib != NULL);
 1672    ThrowIfFail(fileName != NULL);
 1673 
 1674    InitFunc(BOOL, litePDF_SaveToFileWithSign, (void *pctx,
 1675                                                const char *fileName,
 1676                                                unsigned int signatureIndex));
 1677 
 1678    ThrowLastErrorIfFail(func(context, fileName, signatureIndex));
 1679 }
 1680 //---------------------------------------------------------------------------
 1681 
 1682 void TLitePDF::SaveToFileWithSignW(const wchar_t *fileName,
 1683                                    unsigned int signatureIndex)
 1684 {
 1685    const char *_func = "TLitePDF::SaveToFileWithSignW";
 1686 
 1687    ensureLibraryLoaded(_func);
 1688 
 1689    ThrowIfFail(lib != NULL);
 1690    ThrowIfFail(fileName != NULL);
 1691 
 1692    InitFunc(BOOL, litePDF_SaveToFileWithSignW, (void *pctx,
 1693                                                 const wchar_t *fileName,
 1694                                                 unsigned int signatureIndex));
 1695 
 1696    ThrowLastErrorIfFail(func(context, fileName, signatureIndex));
 1697 }
 1698 //---------------------------------------------------------------------------
 1699 
 1700 bool TLitePDF::SaveToDataWithSign(unsigned int signatureIndex,
 1701                                   BYTE *data,
 1702                                   unsigned int *dataLength)
 1703 {
 1704    const char *_func = "TLitePDF::SaveToDataWithSign";
 1705 
 1706    ensureLibraryLoaded(_func);
 1707 
 1708    ThrowIfFail(lib != NULL);
 1709    ThrowIfFail(dataLength != NULL);
 1710 
 1711    InitFunc(BOOL, litePDF_SaveToDataWithSign, (void *pctx,
 1712                                                unsigned int signatureIndex,
 1713                                                BYTE *data,
 1714                                                unsigned int *dataLength));
 1715 
 1716    BOOL succeeded = func(context, signatureIndex, data, dataLength);
 1717 
 1718    return succeeded ? true : false;
 1719 }
 1720 //---------------------------------------------------------------------------
 1721 
 1722 void TLitePDF::SaveToFileWithSignManual(const char *fileName,
 1723                                         unsigned int signatureIndex,
 1724                                         TLitePDFAppendSignatureDataFunc appendSignatureData,
 1725                                         void *append_user_data,
 1726                                         TLitePDFFinishSignatureFunc finishSignature,
 1727                                         void *finish_user_data)
 1728 {
 1729    const char *_func = "TLitePDF::SaveToFileWithSignManual";
 1730 
 1731    ensureLibraryLoaded(_func);
 1732 
 1733    ThrowIfFail(lib != NULL);
 1734    ThrowIfFail(fileName != NULL);
 1735    ThrowIfFail(appendSignatureData != NULL);
 1736    ThrowIfFail(finishSignature != NULL);
 1737 
 1738    InitFunc(BOOL, litePDF_SaveToFileWithSignManual, (void *pctx,
 1739                                                      const char *fileName,
 1740                                                      unsigned int signatureIndex,
 1741                                                      TLitePDFAppendSignatureDataFunc appendSignatureData,
 1742                                                      void *append_user_data,
 1743                                                      TLitePDFFinishSignatureFunc finishSignature,
 1744                                                      void *finish_user_data));
 1745 
 1746    ThrowLastErrorIfFail(func(context,
 1747       fileName,
 1748       signatureIndex,
 1749       appendSignatureData,
 1750       append_user_data,
 1751       finishSignature,
 1752       finish_user_data));
 1753 }
 1754 //---------------------------------------------------------------------------
 1755 
 1756 void TLitePDF::SaveToFileWithSignManualW(const wchar_t *fileName,
 1757                                          unsigned int signatureIndex,
 1758                                          TLitePDFAppendSignatureDataFunc appendSignatureData,
 1759                                          void *append_user_data,
 1760                                          TLitePDFFinishSignatureFunc finishSignature,
 1761                                          void *finish_user_data)
 1762 {
 1763    const char *_func = "TLitePDF::SaveToFileWithSignManualW";
 1764 
 1765    ensureLibraryLoaded(_func);
 1766 
 1767    ThrowIfFail(lib != NULL);
 1768    ThrowIfFail(fileName != NULL);
 1769    ThrowIfFail(appendSignatureData != NULL);
 1770    ThrowIfFail(finishSignature != NULL);
 1771 
 1772    InitFunc(BOOL, litePDF_SaveToFileWithSignManualW, (void *pctx,
 1773                                                       const wchar_t *fileName,
 1774                                                       unsigned int signatureIndex,
 1775                                                       TLitePDFAppendSignatureDataFunc appendSignatureData,
 1776                                                       void *append_user_data,
 1777                                                       TLitePDFFinishSignatureFunc finishSignature,
 1778                                                       void *finish_user_data));
 1779 
 1780    ThrowLastErrorIfFail(func(context,
 1781       fileName,
 1782       signatureIndex,
 1783       appendSignatureData,
 1784       append_user_data,
 1785       finishSignature,
 1786       finish_user_data));
 1787 }
 1788 //---------------------------------------------------------------------------
 1789 
 1790 bool TLitePDF::SaveToDataWithSignManual(unsigned int signatureIndex,
 1791                                         TLitePDFAppendSignatureDataFunc appendSignatureData,
 1792                                         void *append_user_data,
 1793                                         TLitePDFFinishSignatureFunc finishSignature,
 1794                                         void *finish_user_data,
 1795                                         BYTE *data,
 1796                                         unsigned int *dataLength)
 1797 {
 1798    const char *_func = "TLitePDF::SaveToDataWithSignManual";
 1799 
 1800    ensureLibraryLoaded(_func);
 1801 
 1802    ThrowIfFail(lib != NULL);
 1803    ThrowIfFail(appendSignatureData != NULL);
 1804    ThrowIfFail(finishSignature != NULL);
 1805    ThrowIfFail(dataLength != NULL);
 1806 
 1807    InitFunc(BOOL, litePDF_SaveToDataWithSignManual, (void *pctx,
 1808                                                      unsigned int signatureIndex,
 1809                                                      TLitePDFAppendSignatureDataFunc appendSignatureData,
 1810                                                      void *append_user_data,
 1811                                                      TLitePDFFinishSignatureFunc finishSignature,
 1812                                                      void *finish_user_data,
 1813                                                      BYTE *data,
 1814                                                      unsigned int *dataLength));
 1815 
 1816    BOOL succeeded = func(context,
 1817       signatureIndex,
 1818       appendSignatureData,
 1819       append_user_data,
 1820       finishSignature,
 1821       finish_user_data,
 1822       data,
 1823       dataLength);
 1824 
 1825    return succeeded ? true : false;
 1826 }
 1827 //---------------------------------------------------------------------------
 1828 
 1829 void TLitePDF::EmbedFile(const char *fileName)
 1830 {
 1831    const char *_func = "TLitePDF::EmbedFile";
 1832 
 1833    ensureLibraryLoaded(_func);
 1834 
 1835    ThrowIfFail(lib != NULL);
 1836    ThrowIfFail(fileName != NULL);
 1837 
 1838    InitFunc(BOOL, litePDF_EmbedFile, (void *pctx, const char *fileName));
 1839 
 1840    ThrowLastErrorIfFail(func(context, fileName));
 1841 }
 1842 //---------------------------------------------------------------------------
 1843 
 1844 void TLitePDF::EmbedFileW(const wchar_t *fileName)
 1845 {
 1846    const char *_func = "TLitePDF::EmbedFileW";
 1847 
 1848    ensureLibraryLoaded(_func);
 1849 
 1850    ThrowIfFail(lib != NULL);
 1851    ThrowIfFail(fileName != NULL);
 1852 
 1853    InitFunc(BOOL, litePDF_EmbedFileW, (void *pctx, const wchar_t *fileName));
 1854 
 1855    ThrowLastErrorIfFail(func(context, fileName));
 1856 }
 1857 //---------------------------------------------------------------------------
 1858 
 1859 void TLitePDF::EmbedData(const char *fileName,
 1860                          const BYTE *data,
 1861                          unsigned int dataLength)
 1862 {
 1863    const char *_func = "TLitePDF::EmbedData";
 1864 
 1865    ensureLibraryLoaded(_func);
 1866 
 1867    ThrowIfFail(lib != NULL);
 1868    ThrowIfFail(data != NULL);
 1869 
 1870    InitFunc(BOOL, litePDF_EmbedData, (void *pctx, const char *fileName, const BYTE *data, unsigned int dataLength));
 1871 
 1872    ThrowLastErrorIfFail(func(context, fileName, data, dataLength));
 1873 }
 1874 //---------------------------------------------------------------------------
 1875 
 1876 void TLitePDF::EmbedDataW(const wchar_t *fileName,
 1877                           const BYTE *data,
 1878                           unsigned int dataLength)
 1879 {
 1880    const char *_func = "TLitePDF::EmbedDataW";
 1881 
 1882    ensureLibraryLoaded(_func);
 1883 
 1884    ThrowIfFail(lib != NULL);
 1885    ThrowIfFail(data != NULL);
 1886 
 1887    InitFunc(BOOL, litePDF_EmbedDataW, (void *pctx, const wchar_t *fileName, const BYTE *data, unsigned int dataLength));
 1888 
 1889    ThrowLastErrorIfFail(func(context, fileName, data, dataLength));
 1890 }
 1891 //---------------------------------------------------------------------------
 1892 
 1893 int TLitePDF::GetEmbeddedFileCount(void)
 1894 {
 1895    const char *_func = "TLitePDF::GetEmbeddedFileCount";
 1896 
 1897    ensureLibraryLoaded(_func);
 1898 
 1899    ThrowIfFail(lib != NULL);
 1900 
 1901    InitFunc(INT, litePDF_GetEmbeddedFileCount, (void *pctx));
 1902 
 1903    return func(context);
 1904 }
 1905 //---------------------------------------------------------------------------
 1906 
 1907 std::string TLitePDF::GetEmbeddedFileName(unsigned int index)
 1908 {
 1909    const char *_func = "TLitePDF::GetEmbeddedFileName";
 1910 
 1911    ensureLibraryLoaded(_func);
 1912 
 1913    ThrowIfFail(lib != NULL);
 1914 
 1915    InitFunc(BOOL, litePDF_GetEmbeddedFileName, (void *pctx, unsigned int index, char *fileName, unsigned int *fileNameLength));
 1916 
 1917    unsigned int fileNameLength = 0;
 1918    ThrowLastErrorIfFail(func(context, index, NULL, &fileNameLength));
 1919 
 1920    fileNameLength++;
 1921 
 1922    char *buff = (char *) malloc(sizeof(char) * (fileNameLength));
 1923    ThrowMessageIfFail(buff != NULL, "Out of memory!");
 1924 
 1925    std::string fileName;
 1926 
 1927    if (func(context, index, buff, &fileNameLength)) {
 1928       buff[fileNameLength] = 0;
 1929       fileName = buff;
 1930       free (buff);
 1931    } else {
 1932       free (buff);
 1933 
 1934       // always false
 1935       ThrowLastErrorIfFail(buff == NULL);
 1936    }
 1937 
 1938    return fileName;
 1939 }
 1940 //---------------------------------------------------------------------------
 1941 
 1942 std::wstring TLitePDF::GetEmbeddedFileNameW(unsigned int index)
 1943 {
 1944    const char *_func = "TLitePDF::GetEmbeddedFileNameW";
 1945 
 1946    ensureLibraryLoaded(_func);
 1947 
 1948    ThrowIfFail(lib != NULL);
 1949 
 1950    InitFunc(BOOL, litePDF_GetEmbeddedFileNameW, (void *pctx, unsigned int index, wchar_t *fileName, unsigned int *fileNameLength));
 1951 
 1952    unsigned int fileNameLength = 0;
 1953    ThrowLastErrorIfFail(func(context, index, NULL, &fileNameLength));
 1954 
 1955    fileNameLength++;
 1956 
 1957    wchar_t *buff = (wchar_t *) malloc(sizeof(wchar_t) * (fileNameLength));
 1958    ThrowMessageIfFail(buff != NULL, "Out of memory!");
 1959 
 1960    std::wstring fileName;
 1961 
 1962    if (func(context, index, buff, &fileNameLength)) {
 1963       buff[fileNameLength] = 0;
 1964       fileName = buff;
 1965       free (buff);
 1966    } else {
 1967       free (buff);
 1968 
 1969       // always false
 1970       ThrowLastErrorIfFail(buff == NULL);
 1971    }
 1972 
 1973    return fileName;
 1974 }
 1975 //---------------------------------------------------------------------------
 1976 
 1977 bool TLitePDF::GetEmbeddedFileData(unsigned int index,
 1978                                    BYTE *data,
 1979                                    unsigned int *dataLength)
 1980 {
 1981    const char *_func = "TLitePDF::GetEmbeddedFileData";
 1982 
 1983    ensureLibraryLoaded(_func);
 1984 
 1985    ThrowIfFail(lib != NULL);
 1986    ThrowIfFail(dataLength != NULL);
 1987 
 1988    InitFunc(BOOL, litePDF_GetEmbeddedFileData, (void *pctx, unsigned int index, BYTE *data, unsigned int *dataLength));
 1989 
 1990    BOOL succeeded = func(context, index, data, dataLength);
 1991 
 1992    return succeeded ? true : false;
 1993 }
 1994 //---------------------------------------------------------------------------
 1995 
 1996 void *TLitePDF::GetPoDoFoDocument(void)
 1997 {
 1998    const char *_func = "TLitePDF::GetPoDoFoDocument";
 1999 
 2000    ensureLibraryLoaded(_func);
 2001 
 2002    ThrowIfFail(lib != NULL);
 2003 
 2004    InitFunc(void *, litePDF_GetPoDoFoDocument, (void *pctx));
 2005 
 2006    void *podofoDocument = func(context);
 2007 
 2008    ThrowLastErrorIfFail(podofoDocument != NULL);
 2009 
 2010    return podofoDocument;
 2011 }
 2012 //---------------------------------------------------------------------------
 2013 
 2014 void TLitePDF::DrawDebugPage(const char *filename)
 2015 {
 2016    const char *_func = "TLitePDF::DrawDebugPage";
 2017 
 2018    ensureLibraryLoaded(_func);
 2019 
 2020    ThrowIfFail(lib != NULL);
 2021    ThrowIfFail(filename != NULL);
 2022 
 2023    InitFunc(BOOL, litePDF_DrawDebugPage, (void *pctx, const char *filename));
 2024 
 2025    ThrowLastErrorIfFail(func(context, filename));
 2026 }
 2027 //---------------------------------------------------------------------------
 2028 
 2029 void TLitePDF::CreateLinkAnnotation(unsigned int annotationPageIndex,
 2030                                     int annotationX_u,
 2031                                     int annotationY_u,
 2032                                     int annotationWidth_u,
 2033                                     int annotationHeight_u,
 2034                                     unsigned int annotationFlags,
 2035                                     unsigned int annotationResourceID,
 2036                                     unsigned int destinationPageIndex,
 2037                                     unsigned int destinationX_u,
 2038                                     unsigned int destinationY_u,
 2039                                     const wchar_t *destinationDescription)
 2040 {
 2041    const char *_func = "TLitePDF::CreateLinkAnnotation";
 2042 
 2043    ensureLibraryLoaded(_func);
 2044 
 2045    ThrowIfFail(lib != NULL);
 2046 
 2047    InitFunc(BOOL, litePDF_CreateLinkAnnotation, (void *pctx,
 2048                                                  unsigned int annotationPageIndex,
 2049                                                  int annotationX_u,
 2050                                                  int annotationY_u,
 2051                                                  int annotationWidth_u,
 2052                                                  int annotationHeight_u,
 2053                                                  unsigned int annotationFlags,
 2054                                                  unsigned int annotationResourceID,
 2055                                                  unsigned int destinationPageIndex,
 2056                                                  unsigned int destinationX_u,
 2057                                                  unsigned int destinationY_u,
 2058                                                  const wchar_t *destinationDescription));
 2059 
 2060    ThrowLastErrorIfFail(func(context,
 2061                              annotationPageIndex,
 2062                              annotationX_u,
 2063                              annotationY_u,
 2064                              annotationWidth_u,
 2065                              annotationHeight_u,
 2066                              annotationFlags,
 2067                              annotationResourceID,
 2068                              destinationPageIndex,
 2069                              destinationX_u,
 2070                              destinationY_u,
 2071                              destinationDescription));
 2072 }
 2073 //---------------------------------------------------------------------------
 2074 
 2075 void TLitePDF::CreateURIAnnotation(unsigned int annotationPageIndex,
 2076                                    int annotationX_u,
 2077                                    int annotationY_u,
 2078                                    int annotationWidth_u,
 2079                                    int annotationHeight_u,
 2080                                    unsigned int annotationFlags,
 2081                                    unsigned int annotationResourceID,
 2082                                    const char *destinationURI,
 2083                                    const wchar_t *destinationDescription)
 2084 {
 2085    const char *_func = "TLitePDF::CreateURIAnnotation";
 2086 
 2087    ensureLibraryLoaded(_func);
 2088 
 2089    ThrowIfFail(lib != NULL);
 2090 
 2091    InitFunc(BOOL, litePDF_CreateURIAnnotation, (void *pctx,
 2092                                                 unsigned int annotationPageIndex,
 2093                                                 int annotationX_u,
 2094                                                 int annotationY_u,
 2095                                                 int annotationWidth_u,
 2096                                                 int annotationHeight_u,
 2097                                                 unsigned int annotationFlags,
 2098                                                 unsigned int annotationResourceID,
 2099                                                 const char *destinationURI,
 2100                                                 const wchar_t *destinationDescription));
 2101 
 2102    ThrowLastErrorIfFail(func(context,
 2103                              annotationPageIndex,
 2104                              annotationX_u,
 2105                              annotationY_u,
 2106                              annotationWidth_u,
 2107                              annotationHeight_u,
 2108                              annotationFlags,
 2109                              annotationResourceID,
 2110                              destinationURI,
 2111                              destinationDescription));
 2112 }
 2113 //---------------------------------------------------------------------------
 2114 
 2115 unsigned int TLitePDF::CreateBookmarkRoot(const wchar_t *title,
 2116                                           unsigned int flags,
 2117                                           COLORREF titleColor,
 2118                                           unsigned int destinationPageIndex,
 2119                                           unsigned int destinationX_u,
 2120                                           unsigned int destinationY_u)
 2121 {
 2122    const char *_func = "TLitePDF::CreateBookmarkRoot";
 2123 
 2124    ensureLibraryLoaded(_func);
 2125 
 2126    ThrowIfFail(lib != NULL);
 2127    ThrowIfFail(title != NULL);
 2128 
 2129    InitFunc(unsigned int, litePDF_CreateBookmarkRoot, (void *pctx,
 2130                                                        const wchar_t *title,
 2131                                                        unsigned int flags,
 2132                                                        unsigned char titleColor_red,
 2133                                                        unsigned char titleColor_green,
 2134                                                        unsigned char titleColor_blue,
 2135                                                        unsigned int destinationPageIndex,
 2136                                                        unsigned int destinationX_u,
 2137                                                        unsigned int destinationY_u));
 2138 
 2139    unsigned int resourceID = func(context,
 2140                                   title,
 2141                                   flags,
 2142                                   GetRValue(titleColor),
 2143                                   GetGValue(titleColor),
 2144                                   GetBValue(titleColor),
 2145                                   destinationPageIndex,
 2146                                   destinationX_u,
 2147                                   destinationY_u);
 2148    ThrowLastErrorIfFail(resourceID != 0);
 2149 
 2150    return resourceID;
 2151 }
 2152 //---------------------------------------------------------------------------
 2153 
 2154 unsigned int TLitePDF::CreateBookmarkChild(unsigned int parentBookmarkID,
 2155                                            const wchar_t *title,
 2156                                            unsigned int flags,
 2157                                            COLORREF titleColor,
 2158                                            unsigned int destinationPageIndex,
 2159                                            unsigned int destinationX_u,
 2160                                            unsigned int destinationY_u)
 2161 {
 2162    const char *_func = "TLitePDF::CreateBookmarkChild";
 2163 
 2164    ensureLibraryLoaded(_func);
 2165 
 2166    ThrowIfFail(lib != NULL);
 2167    ThrowIfFail(title != NULL);
 2168 
 2169    InitFunc(unsigned int, litePDF_CreateBookmarkChild, (void *pctx,
 2170                                                         unsigned int parentBookmarkID,
 2171                                                         const wchar_t *title,
 2172                                                         unsigned int flags,
 2173                                                         unsigned char titleColor_red,
 2174                                                         unsigned char titleColor_green,
 2175                                                         unsigned char titleColor_blue,
 2176                                                         unsigned int destinationPageIndex,
 2177                                                         unsigned int destinationX_u,
 2178                                                         unsigned int destinationY_u));
 2179 
 2180    unsigned int resourceID = func(context,
 2181                                   parentBookmarkID,
 2182                                   title,
 2183                                   flags,
 2184                                   GetRValue(titleColor),
 2185                                   GetGValue(titleColor),
 2186                                   GetBValue(titleColor),
 2187                                   destinationPageIndex,
 2188                                   destinationX_u,
 2189                                   destinationY_u);
 2190    ThrowLastErrorIfFail(resourceID != 0);
 2191 
 2192    return resourceID;
 2193 }
 2194 //---------------------------------------------------------------------------
 2195 
 2196 unsigned int TLitePDF::CreateBookmarkSibling(unsigned int previousBookmarkID,
 2197                                              const wchar_t *title,
 2198                                              unsigned int flags,
 2199                                              COLORREF titleColor,
 2200                                              unsigned int destinationPageIndex,
 2201                                              unsigned int destinationX_u,
 2202                                              unsigned int destinationY_u)
 2203 {
 2204    const char *_func = "TLitePDF::CreateBookmarkSibling";
 2205 
 2206    ensureLibraryLoaded(_func);
 2207 
 2208    ThrowIfFail(lib != NULL);
 2209    ThrowIfFail(title != NULL);
 2210 
 2211    InitFunc(unsigned int, litePDF_CreateBookmarkSibling, (void *pctx,
 2212                                                           unsigned int previousBookmarkID,
 2213                                                           const wchar_t *title,
 2214                                                           unsigned int flags,
 2215                                                           unsigned char titleColor_red,
 2216                                                           unsigned char titleColor_green,
 2217                                                           unsigned char titleColor_blue,
 2218                                                           unsigned int destinationPageIndex,
 2219                                                           unsigned int destinationX_u,
 2220                                                           unsigned int destinationY_u));
 2221 
 2222    unsigned int resourceID = func(context,
 2223                                   previousBookmarkID,
 2224                                   title,
 2225                                   flags,
 2226                                   GetRValue(titleColor),
 2227                                   GetGValue(titleColor),
 2228                                   GetBValue(titleColor),
 2229                                   destinationPageIndex,
 2230                                   destinationX_u,
 2231                                   destinationY_u);
 2232    ThrowLastErrorIfFail(resourceID != 0);
 2233 
 2234    return resourceID;
 2235 }
 2236 //---------------------------------------------------------------------------
 2237 
 2238 #undef ThrowIfFail
 2239 #undef ThrowMessageIfFail
 2240 #undef ThrowLastErrorIfFail
 2241 
 2242 }; //namespace litePDF