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