-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathpdfcore.cpp
174 lines (139 loc) · 4.2 KB
/
pdfcore.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#include "pdfcore.h"
#include <sstream>
#include <cmath>
#include <iostream>
#include <PDFWriter.h>
#include <PDFPage.h>
#include <PDFWriter.h>
#include <PDFPageInput.h>
PDFCore::PDFCore(){
errorMsg="None";
}
int PDFCore::merge(std::vector<std::string> &files, std::string dest){
PDFWriter pdfWriter;
EStatusCode status;
status=pdfWriter.StartPDF(dest, ePDFVersion13);
if (status!=PDFHummus::eSuccess){
errorMsg="failed to start pdf "+dest;
return status;
}
for (unsigned int i=0;i<files.size();i++){
status=pdfWriter.AppendPDFPagesFromPDF(files[i], PDFPageRange()).first;
if (status!=PDFHummus::eSuccess){
errorMsg="failed to merge "+files[i]+"\n";
break;
}
}
status=pdfWriter.EndPDF();
errorMsg="";
if (status!=PDFHummus::eSuccess)
errorMsg+="failed to end pdf "+dest;
return status;
}
int PDFCore::split(std::string srcPath,std::string destDir,std::string baseFileName,bool split){
//https://github.com/galkahana/PDF-Writer/wiki/PDF-Parsing
EStatusCode status;
InputFile pdfFile;
PDFParser parser;
status=pdfFile.OpenFile(srcPath);
if (status!=PDFHummus::eSuccess){
errorMsg="failed to open pdf";
return status;
}
status=parser.StartPDFParsing(pdfFile.GetInputStream());
if (status!=PDFHummus::eSuccess){
errorMsg="failed to parse pdf";
return status;
}
unsigned int long pageNum=parser.GetPagesCount();
if (!pageNum){
errorMsg="pdf is empty or failed to parse content";
return PDFHummus::eFailure;
}
errorMsg="";
for (unsigned int long i=0;i<pageNum;i++){
std::string
fullDest=destDir+'/'+baseFileName+"_"+intZeroPadding(i+1,pageNum);
PDFPageInput pageInput(&parser,parser.ParsePage(i));
PDFRectangle mediaBox=pageInput.GetMediaBox();
if (split){
PDFPageRange singlePageRange;
singlePageRange.mType = PDFPageRange::eRangeTypeSpecific;
singlePageRange.mSpecificRanges.push_back(ULongAndULong(i,i));
PDFWriter pdfWriter1;
PDFPage* p1=new PDFPage();
status=pdfWriter1.StartPDF(fullDest+"_A.pdf", ePDFVersion13);
if (status!=PDFHummus::eSuccess){
goto error;
}
PDFRectangle leftRect=mediaBox;
leftRect.UpperRightX=leftRect.UpperRightX/2;
p1->SetMediaBox(leftRect);
status=pdfWriter1.MergePDFPagesToPage(p1,srcPath,singlePageRange);
if (status!=PDFHummus::eSuccess)
goto error;
status=pdfWriter1.WritePageAndRelease(p1);
if (status!=PDFHummus::eSuccess)
goto error;
status=pdfWriter1.EndPDF();
if (status!=PDFHummus::eSuccess)
goto error;
PDFWriter pdfWriter2;
PDFPage* p2=new PDFPage();
status=pdfWriter2.StartPDF(fullDest+"_B.pdf", ePDFVersion13);
if (status!=PDFHummus::eSuccess)
goto error;
PDFRectangle rightRect=mediaBox;
rightRect.LowerLeftX=rightRect.UpperRightX/2;
p2->SetMediaBox(rightRect);
status=pdfWriter2.MergePDFPagesToPage(p2,srcPath,singlePageRange);
if (status!=PDFHummus::eSuccess)
goto error;
status=pdfWriter2.WritePageAndRelease(p2);
if (status!=PDFHummus::eSuccess)
goto error;
status=pdfWriter2.EndPDF();
if (status!=PDFHummus::eSuccess)
goto error;
}
else{
PDFWriter pdfWriter;
PDFPage* page=new PDFPage();
status=pdfWriter.StartPDF(fullDest+".pdf", ePDFVersion13);
page->SetMediaBox(mediaBox);
PDFPageRange singlePageRange;
singlePageRange.mType = PDFPageRange::eRangeTypeSpecific;
singlePageRange.mSpecificRanges.push_back(ULongAndULong(i,i));
status=pdfWriter.MergePDFPagesToPage(page,srcPath,singlePageRange);
if (status!=PDFHummus::eSuccess)
goto error;
status=pdfWriter.WritePageAndRelease(page);
if (status!=PDFHummus::eSuccess)
goto error;
status=pdfWriter.EndPDF();
if (status!=PDFHummus::eSuccess)
goto error;
}
continue;
error:
errorMsg+="failed to split page "+intZeroPadding(i+1,pageNum)+"\n";
}
return status;
}
//int to string then add zeroes to match the length of c
//this helps viewing files sorted by filename (so _020 is placed after _019 instead of _1)
std::string PDFCore::intZeroPadding(int i,int c){
std::stringstream ss;
ss<<i;
std::string baseNum= ss.str();
int iDigit=(int)log10(i)+1;
int cDigit=(int)(log10(c))+1;
while(iDigit!=cDigit){
baseNum='0'+baseNum;
iDigit++;
}
return baseNum;
}
std::string PDFCore::getErrorMsg(){
return errorMsg;
}