docinfo (docinfo.cpp) – this example demonstrates how to read and write information about the document. In consist of two parts. The first part writes a PDF document which has set information about an author and such to a file, then it reads the file from a disk and checks that the written values were the same as those read. Some of the values are Unicode strings.

    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 void addPage(litePDF::TLitePDF &litePDF,
   22                     unsigned int pageWidth,
   23                     unsigned int pageHeight,
   24                     const char *msg)
   25 {
   26    // add a new page to it, with large-enough pixel scale
   27    HDC hDC = litePDF.AddPage(litePDF.MMToUnit(pageWidth), litePDF.MMToUnit(pageHeight),
   28                              pageWidth * 10, pageHeight * 10,
   29                              LitePDFDrawFlag_SubstituteFonts);
   30 
   31    // draw the text
   32    LOGFONTA lf = {0, };
   33    lf.lfHeight = -50; // ~5mm
   34    strcpy(lf.lfFaceName, "Helvetica");
   35 
   36    HFONT fnt;
   37    HGDIOBJ oldFnt;
   38 
   39    fnt = CreateFontIndirect(&lf);
   40    oldFnt = SelectObject(hDC, fnt);
   41 
   42    SetTextColor(hDC, RGB(0, 0, 0));
   43    TextOut(hDC, 100, 100, msg, strlen(msg));
   44 
   45    SelectObject(hDC, oldFnt);
   46    DeleteObject(fnt);
   47 
   48    // finish drawing
   49    litePDF.FinishPage(hDC);
   50 }
   51 
   52 int main(void)
   53 {
   54    int res = 0, ii;
   55    struct _docinfo {
   56       const char *key;
   57       const char *value;
   58    } docinfo[] = {
   59       { LitePDFDocumentInfo_Author, "Document's Author ěščřžýáíéúůöĚŠČŘŽÝÁÍÉÚŮÖ §" },
   60       { LitePDFDocumentInfo_Creator, "Document's Creator" },
   61       { LitePDFDocumentInfo_Keywords, "Keyword1;Keyword2" },
   62       { LitePDFDocumentInfo_Subject, "Document's subject ěščřžýáíéúůöĚŠČŘŽÝÁÍÉÚŮÖ §" },
   63       { LitePDFDocumentInfo_Title, "Document's Title ěščřžýáíéúůöĚŠČŘŽÝÁÍÉÚŮÖ §" },
   64       { "CustomProperty", "CustomPropertyValue" },
   65       { LitePDFDocumentInfo_Producer, NULL }, // cannot be written
   66       { LitePDFDocumentInfo_Trapped, NULL },
   67       { LitePDFDocumentInfo_CreationDate, NULL }, // this is set automatically on save
   68       { LitePDFDocumentInfo_ModificationDate, NULL }, // this is set automatically on save
   69       { NULL, NULL }
   70    };
   71 
   72    using namespace litePDF;
   73 
   74    try {
   75       TLitePDF litePDF;
   76 
   77       // create a document
   78       litePDF.CreateMemDocument();
   79 
   80       // create a page
   81       addPage(litePDF, 210, 297, "Document with set information about an author and such");
   82 
   83       for (ii = 0; docinfo[ii].key; ii++) {
   84          if (docinfo[ii].value) {
   85             wchar_t wbuff[256];
   86             int wrote;
   87 
   88             wrote = MultiByteToWideChar(CP_ACP, 0,
   89                     docinfo[ii].value, strlen(docinfo[ii].value),
   90                     wbuff, sizeof(wbuff));
   91             wbuff[wrote] = 0;
   92 
   93             litePDF.SetDocumentInfo(docinfo[ii].key, wbuff);
   94             printf ("writing '%s' = '%s'\n", docinfo[ii].key, docinfo[ii].value);
   95          }
   96       }
   97 
   98       // save to file
   99       litePDF.SaveToFile("docinfo-1.pdf");
  100 
  101       // close the document
  102       litePDF.Close();
  103 
  104       //-----------------------------------------------------------------
  105 
  106       // load from file
  107       litePDF.LoadFromFile("docinfo-1.pdf", NULL, false);
  108 
  109       for (ii = 0; docinfo[ii].key; ii++) {
  110          if (litePDF.GetDocumentInfoExists(docinfo[ii].key)) {
  111             std::wstring wstr;
  112 
  113             wstr = litePDF.GetDocumentInfo(docinfo[ii].key);
  114 
  115             char buff[256];
  116             int wrote;
  117 
  118             wrote = WideCharToMultiByte(CP_ACP, 0,
  119                     wstr.c_str(), wstr.length(),
  120                     buff, sizeof(buff), "?", FALSE);
  121             buff[wrote] = 0;
  122 
  123             if (docinfo[ii].value) {
  124                printf (" + key '%s' has value '%s'; expected:%s\n",
  125                        docinfo[ii].key, buff,
  126                        strcmp(buff, docinfo[ii].value) == 0 ? "yes" : "no");
  127             } else {
  128                printf (" + key '%s' has value '%s'\n", docinfo[ii].key, buff);
  129             }
  130          } else {
  131             printf (" - key '%s' not found\n", docinfo[ii].key);
  132          }
  133       }
  134 
  135       // close the document
  136       litePDF.Close();
  137    } catch (TLitePDFException &ex) {
  138       fprintf(stderr, "litePDF Exception: %x: %s\n", ex.getCode(), ex.getMessage());
  139       res = 1;
  140    }
  141 
  142    return res;
  143 }
TOPlist