ENERGY MONITOR 0.2
Loading...
Searching...
No Matches
pdfWrapper.h
Go to the documentation of this file.
1#pragma once
2
3#pragma comment
4
5#include <string>
6#include <PDFWriter.h>
7#include <PDFPage.h>
8#include <PageContentContext.h>
9#include <PDFFormXObject.h>
10#include <ResourcesDictionary.h>
11#include <PDFUsedFont.h>
12
13
15
16
17
26#define A4_Width 595
27#define A4_Height 842
28
29#define LEFT(n)(n)
30#define TOP(n)(A4_Height - n)
31#define standardFont "resources/OpenSans-Italic.ttf"
32
34 PDFWriter pdfWriter;
35 PDFPage* page;
36 PageContentContext* pageContentContext;
37 PDFUsedFont* font;
38
39 struct {
40 int r = 0;
41 int g = 0;
42 int b = 0;
43 int a = 1;
44 }fontStyle;
45
46 unsigned int pos_x = 0;
47 unsigned int pos_y = 0;
48
49
50public:
51 int left_padding = 30;
52 int top_padding = 30;
53 float font_size = 5;
55
56
57 void init(std::string fileLocation){
58 pdfWriter.StartPDF(fileLocation,ePDFVersion13);
59 font = pdfWriter.GetFontForFile(standardFont);
60 }
61
62 void end(){
63 pdfWriter.EndPDF();
64 }
65
66 void newPage(){
67 page = new PDFPage();
68 page->SetMediaBox(PDFRectangle(0,0,A4_Width,A4_Height));
69 pageContentContext = pdfWriter.StartPageContentContext(page);
70 pos_x = 0;
71 pos_y = 0;
72 }
73
74 void endPage(){
75 pdfWriter.EndPageContentContext(pageContentContext);
76 pdfWriter.WritePageAndRelease(page);
77 }
78
80 pageContentContext->BT();
81 pageContentContext->Tf(font,font_size);
82 pageContentContext->k(fontStyle.r,fontStyle.g,fontStyle.b,fontStyle.a);
83 }
84
85 void addText(std::string text){
86 pageContentContext->Tj(text);
87 PDFUsedFont::TextMeasures textDimensions = font->CalculateTextDimensions(text,font_size);
88 pos_x += textDimensions.xMax;
89 }
90
92 pageContentContext->ET();
93 }
94
95 void newLine(){
96 pos_x = LEFT(left_padding);
97 pos_y -= interline_gap;
98 pageContentContext->Tm(1,0,0,1,LEFT(left_padding), pos_y);
99 }
100
101 void setFontStyle(float size, int r, int g, int b,int a){
102 pageContentContext->Tf(font,size);
103 font_size = size;
104 pageContentContext->k(r,g,b,a);
105 fontStyle.r = r;
106 fontStyle.g = g;
107 fontStyle.b = b;
108 fontStyle.a = a;
109 pageContentContext->TL(font_size);
110 }
111
112 void setCursor(int x, int y){
113 pageContentContext->Tm(1, 0, 0, 1, x, y);
114 pos_x = x;
115 pos_y = y;
116 }
117
118
119 int get_posX(){
120 return pos_x;
121 }
122
123 int get_posY(){
124 return pos_y;
125 }
126
127 void center_text(std::string word){
128 PDFUsedFont::TextMeasures textDimensions = font->CalculateTextDimensions(word,font_size);
129 pageContentContext->Td(A4_Width/2 - textDimensions.width/2 - left_padding, 0);
130 addText(word);
131 pageContentContext->Td(-(A4_Width/2 - textDimensions.width/2 - left_padding), 0);
132 }
133};
134
void init(std::string fileLocation)
Definition pdfWrapper.h:57
int get_posY()
Definition pdfWrapper.h:123
void newPage()
Definition pdfWrapper.h:66
void center_text(std::string word)
Definition pdfWrapper.h:127
void endPage()
Definition pdfWrapper.h:74
void setCursor(int x, int y)
Definition pdfWrapper.h:112
void textPiece_end()
Definition pdfWrapper.h:91
void addText(std::string text)
Definition pdfWrapper.h:85
int top_padding
Definition pdfWrapper.h:52
void end()
Definition pdfWrapper.h:62
int interline_gap
Definition pdfWrapper.h:54
void newLine()
Definition pdfWrapper.h:95
int get_posX()
Definition pdfWrapper.h:119
float font_size
Definition pdfWrapper.h:53
void setFontStyle(float size, int r, int g, int b, int a)
Definition pdfWrapper.h:101
void textPiece_start()
Definition pdfWrapper.h:79
int left_padding
Definition pdfWrapper.h:51
#define standardFont
Definition pdfWrapper.h:31
#define A4_Height
Definition pdfWrapper.h:27
#define A4_Width
Definition pdfWrapper.h:26
#define LEFT(n)
Definition pdfWrapper.h:29