podofodocument (
podofodocument.cpp) –
this example shows how to access the PoDoFo document. It creates
a simple document and then checks how many internal objects contain
a stream, which is printed at the end of the example. This is
the only example which requires linking against litePDF.lib,
due to usage of the PoDoFo classes.
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
26 #include "share/litePDF.h"
27 #include "podofo/podofo.h"
28
29 // fills PDF content with a text from a helloworld example
30 void fillContent(litePDF::TLitePDF &litePDF)
31 {
32 // add a new page to it, with large-enough pixel scale
33 HDC hDC = litePDF.AddPage(litePDF.MMToUnit(210), litePDF.MMToUnit(297), 2100, 2970, LitePDFDrawFlag_None);
34
35 // draw the text
36 LOGFONTA lf = {0, };
37 lf.lfHeight = -300; // ~1/10 of the page height
38 strcpy(lf.lfFaceName, "Arial");
39
40 HFONT fnt;
41 HGDIOBJ oldFnt;
42
43 fnt = CreateFontIndirect(&lf);
44 oldFnt = SelectObject(hDC, fnt);
45
46 SetTextColor(hDC, RGB(128, 0, 0));
47 TextOut(hDC, 100, 100, "Hello World!", 12);
48
49 SelectObject(hDC, oldFnt);
50 DeleteObject(fnt);
51
52 // finish drawing
53 litePDF.FinishPage(hDC);
54 }
55
56 int main(void)
57 {
58 int res = 0;
59
60 using namespace litePDF;
61 using namespace PoDoFo;
62
63 try {
64 TLitePDF litePDF;
65
66 // create an in-memory PDF doument
67 litePDF.CreateMemDocument();
68
69 // fill some content
70 fillContent(litePDF);
71
72 // obtain current PoDoFo document pointer
73 PdfDocument *document = (PdfDocument *) litePDF.GetPoDoFoDocument();
74 if (!document) {
75 throw TLitePDFException(ERROR_INVALID_HANDLE,
76 "Failed to obtain PoDoFo document");
77 }
78
79 // traverse all document objects and count those with streams
80 int streams = 0, total = 0;
81 PdfVecObjects *objs = document->GetObjects();
82 if (!objs) {
83 throw TLitePDFException(ERROR_INVALID_HANDLE,
84 "Failed to get PdfVecObjects");
85 }
86
87 std::vector<PdfObject *>::const_iterator it, end = objs->end();
88 for (it = objs->begin(); it != end; it++, total++) {
89 PdfObject *obj = *it;
90 if (obj && obj->HasStream()) {
91 const PdfStream *strm = obj->GetStream();
92 if (strm) {
93 streams++;
94 }
95 }
96 }
97
98 // print the result
99 printf ("Found %d out of %d objects with stream\n", streams, total);
100
101 // close the document, without saving it
102 litePDF.Close();
103 } catch (TLitePDFException &ex) {
104 fprintf (stderr, "litePDF Exception: %x: %s\n", ex.getCode(), ex.getMessage());
105 res = 1;
106 }
107
108 return res;
109 }