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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
|
// Copyright 2014 PDFium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
#include "../../public/fpdf_edit.h"
#include "../../public/fpdf_formfill.h"
#include "../../public/fpdf_save.h"
#include "../include/fsdk_define.h"
#include "../include/fpdfxfa/fpdfxfa_doc.h"
#include "../include/fpdfxfa/fpdfxfa_app.h"
#include "../include/fpdfxfa/fpdfxfa_util.h"
#if _FX_OS_ == _FX_ANDROID_
#include "time.h"
#else
#include <ctime>
#endif
class CFX_IFileWrite FX_FINAL : public IFX_StreamWrite
{
public:
CFX_IFileWrite();
FX_BOOL Init( FPDF_FILEWRITE * pFileWriteStruct );
virtual FX_BOOL WriteBlock(const void* pData, size_t size) FX_OVERRIDE;
virtual void Release() FX_OVERRIDE {}
protected:
FPDF_FILEWRITE* m_pFileWriteStruct;
};
CFX_IFileWrite::CFX_IFileWrite()
{
m_pFileWriteStruct = NULL;
}
FX_BOOL CFX_IFileWrite::Init( FPDF_FILEWRITE * pFileWriteStruct )
{
if (!pFileWriteStruct)
return FALSE;
else
{
m_pFileWriteStruct = pFileWriteStruct;
}
return TRUE;
}
FX_BOOL CFX_IFileWrite::WriteBlock(const void* pData, size_t size)
{
if (m_pFileWriteStruct)
{
m_pFileWriteStruct->WriteBlock( m_pFileWriteStruct, pData, size );
return TRUE;
}
else
return FALSE;
}
#define XFA_DATASETS 0
#define XFA_FORMS 1
FX_BOOL _SaveXFADocumentData(CPDFXFA_Document* pDocument, CFX_PtrArray& fileList)
{
if (!pDocument)
return FALSE;
if (pDocument->GetDocType() != DOCTYPE_DYNIMIC_XFA && pDocument->GetDocType() != DOCTYPE_STATIC_XFA)
return TRUE;
if (!FPDFXFA_GetApp()->GetXFAApp())
return TRUE;
IXFA_DocView* pXFADocView = pDocument->GetXFADocView();
if (NULL == pXFADocView)
return TRUE;
IXFA_DocHandler *pXFADocHandler = FPDFXFA_GetApp()->GetXFAApp()->GetDocHandler();
CPDF_Document * pPDFDocument = pDocument->GetPDFDoc();
if (pDocument == NULL)
return FALSE;
CPDF_Dictionary* pRoot = pPDFDocument->GetRoot();
if (pRoot == NULL)
return FALSE;
CPDF_Dictionary* pAcroForm = pRoot->GetDict("AcroForm");
if (NULL == pAcroForm)
return FALSE;
CPDF_Object* pXFA = pAcroForm->GetElement("XFA");
if (pXFA == NULL)
return TRUE;
if(pXFA->GetType() != PDFOBJ_ARRAY)
return FALSE;
CPDF_Array* pArray = pXFA->GetArray();
if (NULL == pArray)
return FALSE;
int size = pArray->GetCount();
int iFormIndex = -1;
int iDataSetsIndex = -1;
int iTemplate = -1;
int iLast = size - 2;
for (int i = 0; i < size - 1; i++)
{
CPDF_Object* pPDFObj = pArray->GetElement(i);
if (pPDFObj->GetType() != PDFOBJ_STRING)
continue;
if (pPDFObj->GetString() == "form")
iFormIndex = i+1;
else if (pPDFObj->GetString() == "datasets")
iDataSetsIndex = i+1;
else if (pPDFObj->GetString() == FX_BSTRC("template"))
iTemplate = i + 1;
}
IXFA_ChecksumContext* pContext = NULL;
#define XFA_USECKSUM
#ifdef XFA_USECKSUM
//Checksum
pContext = XFA_Checksum_Create();
FXSYS_assert(pContext);
pContext->StartChecksum();
//template
if (iTemplate > -1)
{
CPDF_Stream *pTemplateStream = pArray->GetStream(iTemplate);
CPDF_StreamAcc streamAcc;
streamAcc.LoadAllData(pTemplateStream);
FX_LPBYTE pData = (FX_LPBYTE)streamAcc.GetData();
FX_DWORD dwSize2 = streamAcc.GetSize();
IFX_FileStream *pTemplate = FX_CreateMemoryStream(pData, dwSize2);
pContext->UpdateChecksum((IFX_FileRead*)pTemplate);
pTemplate->Release();
}
#endif
CPDF_Stream* pFormStream = NULL;
CPDF_Stream* pDataSetsStream = NULL;
if (iFormIndex != -1)
{
//Get form CPDF_Stream
CPDF_Object* pFormPDFObj = pArray->GetElement(iFormIndex);
if (pFormPDFObj->GetType() == PDFOBJ_REFERENCE)
{
CPDF_Reference* pFormRefObj = (CPDF_Reference*)pFormPDFObj;
CPDF_Object* pFormDircetObj = pFormPDFObj->GetDirect();
if (NULL != pFormDircetObj && pFormDircetObj->GetType() == PDFOBJ_STREAM)
{
pFormStream = (CPDF_Stream*)pFormDircetObj;
}
}
else if (pFormPDFObj->GetType() == PDFOBJ_STREAM)
{
pFormStream = (CPDF_Stream*)pFormPDFObj;
}
}
if (iDataSetsIndex != -1)
{
//Get datasets CPDF_Stream
CPDF_Object* pDataSetsPDFObj = pArray->GetElement(iDataSetsIndex);
if (pDataSetsPDFObj->GetType() == PDFOBJ_REFERENCE)
{
CPDF_Reference* pDataSetsRefObj = (CPDF_Reference*)pDataSetsPDFObj;
CPDF_Object* pDataSetsDircetObj = pDataSetsRefObj->GetDirect();
if (NULL != pDataSetsDircetObj && pDataSetsDircetObj->GetType() == PDFOBJ_STREAM)
{
pDataSetsStream = (CPDF_Stream*)pDataSetsDircetObj;
}
}
else if (pDataSetsPDFObj->GetType() == PDFOBJ_STREAM)
{
pDataSetsStream = (CPDF_Stream*)pDataSetsPDFObj;
}
}
//end
//L"datasets"
{
IFX_FileStream* pDsfileWrite = FX_CreateMemoryStream();
if ( NULL == pDsfileWrite )
{
pContext->Release();
pDsfileWrite->Release();
return FALSE;
}
if (pXFADocHandler->SavePackage(pXFADocView->GetDoc(), CFX_WideStringC(L"datasets"), pDsfileWrite) && pDsfileWrite->GetSize()>0)
{
#ifdef XFA_USECKSUM
//Datasets
pContext->UpdateChecksum((IFX_FileRead*)pDsfileWrite);
pContext->FinishChecksum();
#endif
CPDF_Dictionary* pDataDict = FX_NEW CPDF_Dictionary;
if (iDataSetsIndex != -1)
{
if (pDataSetsStream)
pDataSetsStream->InitStream(pDsfileWrite, pDataDict);
}
else
{
CPDF_Stream* pData = FX_NEW CPDF_Stream(NULL, 0, NULL);
pData->InitStream(pDsfileWrite, pDataDict);
FX_DWORD AppStreamobjnum = pPDFDocument->AddIndirectObject(pData);
CPDF_Reference* pRef = (CPDF_Reference*)pPDFDocument->GetIndirectObject(AppStreamobjnum);
{
iLast = pArray->GetCount() -2;
pArray->InsertAt(iLast,CPDF_String::Create("datasets"));
pArray->InsertAt(iLast+1, pData, pPDFDocument);
}
}
fileList.Add(pDsfileWrite);
}
}
//L"form"
{
IFX_FileStream* pfileWrite = FX_CreateMemoryStream();
if (NULL == pfileWrite)
{
pContext->Release();
return FALSE;
}
if(pXFADocHandler->SavePackage(pXFADocView->GetDoc(), CFX_WideStringC(L"form"), pfileWrite, pContext) && pfileWrite > 0)
{
CPDF_Dictionary* pDataDict = FX_NEW CPDF_Dictionary;
if (iFormIndex != -1)
{
if (pFormStream)
pFormStream->InitStream(pfileWrite, pDataDict);
}
else
{
CPDF_Stream* pData = FX_NEW CPDF_Stream(NULL, 0, NULL);
pData->InitStream(pfileWrite, pDataDict);
FX_DWORD AppStreamobjnum = pPDFDocument->AddIndirectObject(pData);
CPDF_Reference* pRef = (CPDF_Reference*)pPDFDocument->GetIndirectObject(AppStreamobjnum);
{
iLast = pArray->GetCount() -2;
pArray->InsertAt(iLast, CPDF_String::Create("form"));
pArray->InsertAt(iLast+1, pData, pPDFDocument);
}
}
fileList.Add(pfileWrite);
}
}
pContext->Release();
return TRUE;
}
FX_BOOL _SendPostSaveToXFADoc(CPDFXFA_Document* pDocument)
{
if (!pDocument)
return FALSE;
if (pDocument->GetDocType() != DOCTYPE_DYNIMIC_XFA && pDocument->GetDocType() != DOCTYPE_STATIC_XFA)
return TRUE;
IXFA_DocView* pXFADocView = pDocument->GetXFADocView();
if (NULL == pXFADocView)
return FALSE;
IXFA_WidgetHandler* pWidgetHander = pXFADocView->GetWidgetHandler();
CXFA_WidgetAcc* pWidgetAcc = NULL;
IXFA_WidgetAccIterator* pWidgetAccIterator = pXFADocView->CreateWidgetAccIterator();
pWidgetAcc = pWidgetAccIterator->MoveToNext();
while(pWidgetAcc)
{
CXFA_EventParam preParam;
preParam.m_eType = XFA_EVENT_PostSave;
pWidgetHander->ProcessEvent(pWidgetAcc,&preParam);
pWidgetAcc = pWidgetAccIterator->MoveToNext();
}
pWidgetAccIterator->Release();
pXFADocView->UpdateDocView();
pDocument->_ClearChangeMark();
return TRUE;
}
FX_BOOL _SendPreSaveToXFADoc(CPDFXFA_Document* pDocument, CFX_PtrArray& fileList)
{
if (pDocument->GetDocType() != DOCTYPE_DYNIMIC_XFA && pDocument->GetDocType() != DOCTYPE_STATIC_XFA)
return TRUE;
IXFA_DocView* pXFADocView = pDocument->GetXFADocView();
if (NULL == pXFADocView)
return TRUE;
IXFA_WidgetHandler* pWidgetHander = pXFADocView->GetWidgetHandler();
CXFA_WidgetAcc* pWidgetAcc = NULL;
IXFA_WidgetAccIterator* pWidgetAccIterator = pXFADocView->CreateWidgetAccIterator();
pWidgetAcc = pWidgetAccIterator->MoveToNext();
while(pWidgetAcc)
{
CXFA_EventParam preParam;
preParam.m_eType = XFA_EVENT_PreSave;
pWidgetHander->ProcessEvent(pWidgetAcc, &preParam);
pWidgetAcc = pWidgetAccIterator->MoveToNext();
}
pWidgetAccIterator->Release();
pXFADocView->UpdateDocView();
return _SaveXFADocumentData(pDocument, fileList);
}
FPDF_BOOL _FPDF_Doc_Save(FPDF_DOCUMENT document, FPDF_FILEWRITE * pFileWrite,FPDF_DWORD flags, FPDF_BOOL bSetVersion,
int fileVerion)
{
CPDFXFA_Document* pDoc = (CPDFXFA_Document*)document;
CFX_PtrArray fileList;
_SendPreSaveToXFADoc(pDoc, fileList);
CPDF_Document* pPDFDoc = pDoc->GetPDFDoc();
if (!pPDFDoc)
return 0;
if ( flags < FPDF_INCREMENTAL || flags > FPDF_REMOVE_SECURITY )
{
flags = 0;
}
CPDF_Creator FileMaker(pPDFDoc);
if (bSetVersion)
FileMaker.SetFileVersion(fileVerion);
if (flags == FPDF_REMOVE_SECURITY)
{
flags = 0;
FileMaker.RemoveSecurity();
}
CFX_IFileWrite* pStreamWrite = NULL;
FX_BOOL bRet;
pStreamWrite = new CFX_IFileWrite;
pStreamWrite->Init( pFileWrite );
bRet = FileMaker.Create(pStreamWrite, flags);
_SendPostSaveToXFADoc(pDoc);
//pDoc->_ClearChangeMark();
for (int i = 0; i < fileList.GetSize(); i++)
{
IFX_FileStream* pFile = (IFX_FileStream*)fileList.GetAt(i);
pFile->Release();
}
fileList.RemoveAll();
delete pStreamWrite;
return bRet;
}
DLLEXPORT FPDF_BOOL STDCALL FPDF_SaveAsCopy( FPDF_DOCUMENT document,FPDF_FILEWRITE * pFileWrite,
FPDF_DWORD flags )
{
return _FPDF_Doc_Save(document, pFileWrite, flags, FALSE , 0);
}
DLLEXPORT FPDF_BOOL STDCALL FPDF_SaveWithVersion( FPDF_DOCUMENT document,FPDF_FILEWRITE * pFileWrite,
FPDF_DWORD flags, int fileVersion)
{
return _FPDF_Doc_Save(document, pFileWrite, flags, TRUE , fileVersion);
}
|