John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 1 | // Copyright 2014 PDFium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
Lei Zhang | a6d9f0e | 2015-06-13 00:48:38 -0700 | [diff] [blame] | 4 | |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 5 | // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com |
| 6 | |
Lei Zhang | b4e7f30 | 2015-11-06 15:52:32 -0800 | [diff] [blame] | 7 | #include "public/fpdf_save.h" |
| 8 | |
Dan Sinclair | 3ebd121 | 2016-03-09 09:59:23 -0500 | [diff] [blame] | 9 | #include <vector> |
| 10 | |
Dan Sinclair | aa403d3 | 2016-03-15 14:57:22 -0400 | [diff] [blame] | 11 | #include "core/fpdfapi/fpdf_edit/include/cpdf_creator.h" |
| 12 | #include "core/fpdfapi/fpdf_parser/include/cpdf_array.h" |
| 13 | #include "core/fpdfapi/fpdf_parser/include/cpdf_document.h" |
| 14 | #include "core/fpdfapi/fpdf_parser/include/cpdf_reference.h" |
Dan Sinclair | 584b1e6 | 2016-03-21 09:15:45 -0400 | [diff] [blame] | 15 | #include "core/fpdfapi/fpdf_parser/include/cpdf_stream_acc.h" |
Dan Sinclair | aa403d3 | 2016-03-15 14:57:22 -0400 | [diff] [blame] | 16 | #include "core/fpdfapi/fpdf_parser/include/cpdf_string.h" |
Dan Sinclair | a8a28e0 | 2016-03-23 15:41:39 -0400 | [diff] [blame] | 17 | #include "core/fxcrt/include/fx_ext.h" |
Tom Sepez | 40e9ff3 | 2015-11-30 12:39:54 -0800 | [diff] [blame] | 18 | #include "fpdfsdk/include/fsdk_define.h" |
| 19 | #include "public/fpdf_edit.h" |
| 20 | |
Tom Sepez | 51da093 | 2015-11-25 16:05:49 -0800 | [diff] [blame] | 21 | #ifdef PDF_ENABLE_XFA |
dsinclair | 89bdd08 | 2016-04-06 10:47:54 -0700 | [diff] [blame] | 22 | #include "fpdfsdk/fpdfxfa/include/fpdfxfa_app.h" |
| 23 | #include "fpdfsdk/fpdfxfa/include/fpdfxfa_doc.h" |
| 24 | #include "fpdfsdk/fpdfxfa/include/fpdfxfa_util.h" |
Lei Zhang | b4e7f30 | 2015-11-06 15:52:32 -0800 | [diff] [blame] | 25 | #include "public/fpdf_formfill.h" |
weili | 625ad66 | 2016-06-15 11:21:33 -0700 | [diff] [blame] | 26 | #include "xfa/fxfa/include/cxfa_eventparam.h" |
dsinclair | 7222ea6 | 2016-04-06 14:33:07 -0700 | [diff] [blame] | 27 | #include "xfa/fxfa/include/xfa_checksum.h" |
| 28 | #include "xfa/fxfa/include/xfa_ffapp.h" |
| 29 | #include "xfa/fxfa/include/xfa_ffdocview.h" |
| 30 | #include "xfa/fxfa/include/xfa_ffwidgethandler.h" |
Tom Sepez | 51da093 | 2015-11-25 16:05:49 -0800 | [diff] [blame] | 31 | #endif |
Tom Sepez | 1ed8a21 | 2015-05-11 15:25:39 -0700 | [diff] [blame] | 32 | |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 33 | #if _FX_OS_ == _FX_ANDROID_ |
| 34 | #include "time.h" |
| 35 | #else |
| 36 | #include <ctime> |
| 37 | #endif |
| 38 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 39 | class CFX_IFileWrite final : public IFX_StreamWrite { |
| 40 | public: |
| 41 | CFX_IFileWrite(); |
| 42 | FX_BOOL Init(FPDF_FILEWRITE* pFileWriteStruct); |
Lei Zhang | 2b1a2d5 | 2015-08-14 22:16:22 -0700 | [diff] [blame] | 43 | FX_BOOL WriteBlock(const void* pData, size_t size) override; |
| 44 | void Release() override; |
Lei Zhang | a6d9f0e | 2015-06-13 00:48:38 -0700 | [diff] [blame] | 45 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 46 | protected: |
Lei Zhang | 2b1a2d5 | 2015-08-14 22:16:22 -0700 | [diff] [blame] | 47 | ~CFX_IFileWrite() override {} |
| 48 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 49 | FPDF_FILEWRITE* m_pFileWriteStruct; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 50 | }; |
| 51 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 52 | CFX_IFileWrite::CFX_IFileWrite() { |
thestig | 1cd352e | 2016-06-07 17:53:06 -0700 | [diff] [blame] | 53 | m_pFileWriteStruct = nullptr; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 54 | } |
| 55 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 56 | FX_BOOL CFX_IFileWrite::Init(FPDF_FILEWRITE* pFileWriteStruct) { |
| 57 | if (!pFileWriteStruct) |
| 58 | return FALSE; |
Tom Sepez | 2f2ffec | 2015-07-23 14:42:09 -0700 | [diff] [blame] | 59 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 60 | m_pFileWriteStruct = pFileWriteStruct; |
| 61 | return TRUE; |
| 62 | } |
| 63 | |
| 64 | FX_BOOL CFX_IFileWrite::WriteBlock(const void* pData, size_t size) { |
| 65 | if (!m_pFileWriteStruct) |
| 66 | return FALSE; |
| 67 | |
| 68 | m_pFileWriteStruct->WriteBlock(m_pFileWriteStruct, pData, size); |
| 69 | return TRUE; |
| 70 | } |
| 71 | |
Lei Zhang | 2b1a2d5 | 2015-08-14 22:16:22 -0700 | [diff] [blame] | 72 | void CFX_IFileWrite::Release() { |
| 73 | delete this; |
| 74 | } |
| 75 | |
Tom Sepez | 7a73eff | 2016-02-08 13:39:53 -0800 | [diff] [blame] | 76 | namespace { |
| 77 | |
Tom Sepez | 51da093 | 2015-11-25 16:05:49 -0800 | [diff] [blame] | 78 | #ifdef PDF_ENABLE_XFA |
Tom Sepez | 7a73eff | 2016-02-08 13:39:53 -0800 | [diff] [blame] | 79 | bool SaveXFADocumentData(CPDFXFA_Document* pDocument, |
| 80 | std::vector<ScopedFileStream>* fileList) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 81 | if (!pDocument) |
Tom Sepez | 7a73eff | 2016-02-08 13:39:53 -0800 | [diff] [blame] | 82 | return false; |
| 83 | |
Tom Sepez | d3116dc | 2015-11-24 15:58:06 -0800 | [diff] [blame] | 84 | if (pDocument->GetDocType() != DOCTYPE_DYNAMIC_XFA && |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 85 | pDocument->GetDocType() != DOCTYPE_STATIC_XFA) |
Tom Sepez | 7a73eff | 2016-02-08 13:39:53 -0800 | [diff] [blame] | 86 | return true; |
| 87 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 88 | if (!CPDFXFA_App::GetInstance()->GetXFAApp()) |
Tom Sepez | 7a73eff | 2016-02-08 13:39:53 -0800 | [diff] [blame] | 89 | return true; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 90 | |
dsinclair | df4bc59 | 2016-03-31 20:34:43 -0700 | [diff] [blame] | 91 | CXFA_FFDocView* pXFADocView = pDocument->GetXFADocView(); |
Tom Sepez | 7a73eff | 2016-02-08 13:39:53 -0800 | [diff] [blame] | 92 | if (!pXFADocView) |
| 93 | return true; |
Bo Xu | fdc00a7 | 2014-10-28 23:03:33 -0700 | [diff] [blame] | 94 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 95 | CPDF_Document* pPDFDocument = pDocument->GetPDFDoc(); |
Tom Sepez | 7a73eff | 2016-02-08 13:39:53 -0800 | [diff] [blame] | 96 | if (!pDocument) |
| 97 | return false; |
Bo Xu | fdc00a7 | 2014-10-28 23:03:33 -0700 | [diff] [blame] | 98 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 99 | CPDF_Dictionary* pRoot = pPDFDocument->GetRoot(); |
Tom Sepez | 7a73eff | 2016-02-08 13:39:53 -0800 | [diff] [blame] | 100 | if (!pRoot) |
| 101 | return false; |
| 102 | |
Wei Li | 9b76113 | 2016-01-29 15:44:20 -0800 | [diff] [blame] | 103 | CPDF_Dictionary* pAcroForm = pRoot->GetDictBy("AcroForm"); |
Tom Sepez | 7a73eff | 2016-02-08 13:39:53 -0800 | [diff] [blame] | 104 | if (!pAcroForm) |
| 105 | return false; |
| 106 | |
tsepez | bd56755 | 2016-03-29 14:51:50 -0700 | [diff] [blame] | 107 | CPDF_Object* pXFA = pAcroForm->GetObjectBy("XFA"); |
Tom Sepez | 7a73eff | 2016-02-08 13:39:53 -0800 | [diff] [blame] | 108 | if (!pXFA) |
| 109 | return true; |
| 110 | |
thestig | b8bf55f | 2016-05-21 21:08:05 -0700 | [diff] [blame] | 111 | CPDF_Array* pArray = pXFA->AsArray(); |
Tom Sepez | 7a73eff | 2016-02-08 13:39:53 -0800 | [diff] [blame] | 112 | if (!pArray) |
| 113 | return false; |
| 114 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 115 | int size = pArray->GetCount(); |
| 116 | int iFormIndex = -1; |
| 117 | int iDataSetsIndex = -1; |
| 118 | int iTemplate = -1; |
| 119 | int iLast = size - 2; |
| 120 | for (int i = 0; i < size - 1; i++) { |
tsepez | bd56755 | 2016-03-29 14:51:50 -0700 | [diff] [blame] | 121 | CPDF_Object* pPDFObj = pArray->GetObjectAt(i); |
Tom Sepez | 8e5cd19 | 2016-01-26 13:20:26 -0800 | [diff] [blame] | 122 | if (!pPDFObj->IsString()) |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 123 | continue; |
| 124 | if (pPDFObj->GetString() == "form") |
| 125 | iFormIndex = i + 1; |
| 126 | else if (pPDFObj->GetString() == "datasets") |
| 127 | iDataSetsIndex = i + 1; |
Lei Zhang | d983b09 | 2015-12-14 16:58:33 -0800 | [diff] [blame] | 128 | else if (pPDFObj->GetString() == "template") |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 129 | iTemplate = i + 1; |
| 130 | } |
tsepez | 501e8cd | 2016-05-18 11:39:15 -0700 | [diff] [blame] | 131 | std::unique_ptr<CXFA_ChecksumContext> pContext(new CXFA_ChecksumContext); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 132 | pContext->StartChecksum(); |
Lei Zhang | a6d9f0e | 2015-06-13 00:48:38 -0700 | [diff] [blame] | 133 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 134 | // template |
| 135 | if (iTemplate > -1) { |
Wei Li | 9b76113 | 2016-01-29 15:44:20 -0800 | [diff] [blame] | 136 | CPDF_Stream* pTemplateStream = pArray->GetStreamAt(iTemplate); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 137 | CPDF_StreamAcc streamAcc; |
| 138 | streamAcc.LoadAllData(pTemplateStream); |
| 139 | uint8_t* pData = (uint8_t*)streamAcc.GetData(); |
tsepez | c3255f5 | 2016-03-25 14:52:27 -0700 | [diff] [blame] | 140 | uint32_t dwSize2 = streamAcc.GetSize(); |
Tom Sepez | 7a73eff | 2016-02-08 13:39:53 -0800 | [diff] [blame] | 141 | ScopedFileStream pTemplate(FX_CreateMemoryStream(pData, dwSize2)); |
| 142 | pContext->UpdateChecksum(pTemplate.get()); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 143 | } |
thestig | 1cd352e | 2016-06-07 17:53:06 -0700 | [diff] [blame] | 144 | CPDF_Stream* pFormStream = nullptr; |
| 145 | CPDF_Stream* pDataSetsStream = nullptr; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 146 | if (iFormIndex != -1) { |
| 147 | // Get form CPDF_Stream |
tsepez | bd56755 | 2016-03-29 14:51:50 -0700 | [diff] [blame] | 148 | CPDF_Object* pFormPDFObj = pArray->GetObjectAt(iFormIndex); |
Tom Sepez | 8e5cd19 | 2016-01-26 13:20:26 -0800 | [diff] [blame] | 149 | if (pFormPDFObj->IsReference()) { |
| 150 | CPDF_Object* pFormDirectObj = pFormPDFObj->GetDirect(); |
| 151 | if (pFormDirectObj && pFormDirectObj->IsStream()) { |
| 152 | pFormStream = (CPDF_Stream*)pFormDirectObj; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 153 | } |
Tom Sepez | 8e5cd19 | 2016-01-26 13:20:26 -0800 | [diff] [blame] | 154 | } else if (pFormPDFObj->IsStream()) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 155 | pFormStream = (CPDF_Stream*)pFormPDFObj; |
Tom Sepez | 2f2ffec | 2015-07-23 14:42:09 -0700 | [diff] [blame] | 156 | } |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 157 | } |
Lei Zhang | a6d9f0e | 2015-06-13 00:48:38 -0700 | [diff] [blame] | 158 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 159 | if (iDataSetsIndex != -1) { |
| 160 | // Get datasets CPDF_Stream |
tsepez | bd56755 | 2016-03-29 14:51:50 -0700 | [diff] [blame] | 161 | CPDF_Object* pDataSetsPDFObj = pArray->GetObjectAt(iDataSetsIndex); |
Tom Sepez | 8e5cd19 | 2016-01-26 13:20:26 -0800 | [diff] [blame] | 162 | if (pDataSetsPDFObj->IsReference()) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 163 | CPDF_Reference* pDataSetsRefObj = (CPDF_Reference*)pDataSetsPDFObj; |
Tom Sepez | 8e5cd19 | 2016-01-26 13:20:26 -0800 | [diff] [blame] | 164 | CPDF_Object* pDataSetsDirectObj = pDataSetsRefObj->GetDirect(); |
| 165 | if (pDataSetsDirectObj && pDataSetsDirectObj->IsStream()) { |
| 166 | pDataSetsStream = (CPDF_Stream*)pDataSetsDirectObj; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 167 | } |
Tom Sepez | 8e5cd19 | 2016-01-26 13:20:26 -0800 | [diff] [blame] | 168 | } else if (pDataSetsPDFObj->IsStream()) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 169 | pDataSetsStream = (CPDF_Stream*)pDataSetsPDFObj; |
Tom Sepez | 2f2ffec | 2015-07-23 14:42:09 -0700 | [diff] [blame] | 170 | } |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 171 | } |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 172 | // L"datasets" |
| 173 | { |
Tom Sepez | 7a73eff | 2016-02-08 13:39:53 -0800 | [diff] [blame] | 174 | ScopedFileStream pDsfileWrite(FX_CreateMemoryStream()); |
dsinclair | cbfef57 | 2016-05-18 13:16:12 -0700 | [diff] [blame] | 175 | if (pXFADocView->GetDoc()->SavePackage(XFA_HASHCODE_Datasets, |
| 176 | pDsfileWrite.get(), nullptr) && |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 177 | pDsfileWrite->GetSize() > 0) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 178 | // Datasets |
Tom Sepez | 7a73eff | 2016-02-08 13:39:53 -0800 | [diff] [blame] | 179 | pContext->UpdateChecksum(pDsfileWrite.get()); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 180 | pContext->FinishChecksum(); |
Tom Sepez | ae51c81 | 2015-08-05 12:34:06 -0700 | [diff] [blame] | 181 | CPDF_Dictionary* pDataDict = new CPDF_Dictionary; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 182 | if (iDataSetsIndex != -1) { |
| 183 | if (pDataSetsStream) |
Tom Sepez | 7a73eff | 2016-02-08 13:39:53 -0800 | [diff] [blame] | 184 | pDataSetsStream->InitStreamFromFile(pDsfileWrite.get(), pDataDict); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 185 | } else { |
thestig | 1cd352e | 2016-06-07 17:53:06 -0700 | [diff] [blame] | 186 | CPDF_Stream* pData = new CPDF_Stream(nullptr, 0, nullptr); |
Tom Sepez | 7a73eff | 2016-02-08 13:39:53 -0800 | [diff] [blame] | 187 | pData->InitStreamFromFile(pDsfileWrite.get(), pDataDict); |
Tom Sepez | 3343d14 | 2015-11-02 09:54:54 -0800 | [diff] [blame] | 188 | pPDFDocument->AddIndirectObject(pData); |
| 189 | iLast = pArray->GetCount() - 2; |
Lei Zhang | 4880d1a | 2015-12-18 17:05:11 -0800 | [diff] [blame] | 190 | pArray->InsertAt(iLast, new CPDF_String("datasets", FALSE)); |
Tom Sepez | 3343d14 | 2015-11-02 09:54:54 -0800 | [diff] [blame] | 191 | pArray->InsertAt(iLast + 1, pData, pPDFDocument); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 192 | } |
Tom Sepez | 7a73eff | 2016-02-08 13:39:53 -0800 | [diff] [blame] | 193 | fileList->push_back(std::move(pDsfileWrite)); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 194 | } |
| 195 | } |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 196 | // L"form" |
| 197 | { |
Tom Sepez | 7a73eff | 2016-02-08 13:39:53 -0800 | [diff] [blame] | 198 | ScopedFileStream pfileWrite(FX_CreateMemoryStream()); |
dsinclair | cbfef57 | 2016-05-18 13:16:12 -0700 | [diff] [blame] | 199 | if (pXFADocView->GetDoc()->SavePackage(XFA_HASHCODE_Form, pfileWrite.get(), |
| 200 | pContext.get()) && |
Tom Sepez | 7a73eff | 2016-02-08 13:39:53 -0800 | [diff] [blame] | 201 | pfileWrite->GetSize() > 0) { |
Tom Sepez | ae51c81 | 2015-08-05 12:34:06 -0700 | [diff] [blame] | 202 | CPDF_Dictionary* pDataDict = new CPDF_Dictionary; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 203 | if (iFormIndex != -1) { |
| 204 | if (pFormStream) |
Tom Sepez | 7a73eff | 2016-02-08 13:39:53 -0800 | [diff] [blame] | 205 | pFormStream->InitStreamFromFile(pfileWrite.get(), pDataDict); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 206 | } else { |
thestig | 1cd352e | 2016-06-07 17:53:06 -0700 | [diff] [blame] | 207 | CPDF_Stream* pData = new CPDF_Stream(nullptr, 0, nullptr); |
Tom Sepez | 7a73eff | 2016-02-08 13:39:53 -0800 | [diff] [blame] | 208 | pData->InitStreamFromFile(pfileWrite.get(), pDataDict); |
Tom Sepez | 3343d14 | 2015-11-02 09:54:54 -0800 | [diff] [blame] | 209 | pPDFDocument->AddIndirectObject(pData); |
| 210 | iLast = pArray->GetCount() - 2; |
Lei Zhang | 4880d1a | 2015-12-18 17:05:11 -0800 | [diff] [blame] | 211 | pArray->InsertAt(iLast, new CPDF_String("form", FALSE)); |
Tom Sepez | 3343d14 | 2015-11-02 09:54:54 -0800 | [diff] [blame] | 212 | pArray->InsertAt(iLast + 1, pData, pPDFDocument); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 213 | } |
Tom Sepez | 7a73eff | 2016-02-08 13:39:53 -0800 | [diff] [blame] | 214 | fileList->push_back(std::move(pfileWrite)); |
Tom Sepez | 2f2ffec | 2015-07-23 14:42:09 -0700 | [diff] [blame] | 215 | } |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 216 | } |
Tom Sepez | 7a73eff | 2016-02-08 13:39:53 -0800 | [diff] [blame] | 217 | return true; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 218 | } |
| 219 | |
Tom Sepez | 7a73eff | 2016-02-08 13:39:53 -0800 | [diff] [blame] | 220 | bool SendPostSaveToXFADoc(CPDFXFA_Document* pDocument) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 221 | if (!pDocument) |
Tom Sepez | 7a73eff | 2016-02-08 13:39:53 -0800 | [diff] [blame] | 222 | return false; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 223 | |
Tom Sepez | d3116dc | 2015-11-24 15:58:06 -0800 | [diff] [blame] | 224 | if (pDocument->GetDocType() != DOCTYPE_DYNAMIC_XFA && |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 225 | pDocument->GetDocType() != DOCTYPE_STATIC_XFA) |
Tom Sepez | 7a73eff | 2016-02-08 13:39:53 -0800 | [diff] [blame] | 226 | return true; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 227 | |
dsinclair | df4bc59 | 2016-03-31 20:34:43 -0700 | [diff] [blame] | 228 | CXFA_FFDocView* pXFADocView = pDocument->GetXFADocView(); |
Tom Sepez | 7a73eff | 2016-02-08 13:39:53 -0800 | [diff] [blame] | 229 | if (!pXFADocView) |
| 230 | return false; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 231 | |
dsinclair | df4bc59 | 2016-03-31 20:34:43 -0700 | [diff] [blame] | 232 | CXFA_FFWidgetHandler* pWidgetHander = pXFADocView->GetWidgetHandler(); |
tsepez | e7b2853 | 2016-05-18 12:10:49 -0700 | [diff] [blame] | 233 | std::unique_ptr<CXFA_WidgetAccIterator> pWidgetAccIterator( |
| 234 | pXFADocView->CreateWidgetAccIterator()); |
| 235 | while (CXFA_WidgetAcc* pWidgetAcc = pWidgetAccIterator->MoveToNext()) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 236 | CXFA_EventParam preParam; |
| 237 | preParam.m_eType = XFA_EVENT_PostSave; |
| 238 | pWidgetHander->ProcessEvent(pWidgetAcc, &preParam); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 239 | } |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 240 | pXFADocView->UpdateDocView(); |
| 241 | pDocument->_ClearChangeMark(); |
Tom Sepez | 7a73eff | 2016-02-08 13:39:53 -0800 | [diff] [blame] | 242 | return true; |
Bo Xu | fdc00a7 | 2014-10-28 23:03:33 -0700 | [diff] [blame] | 243 | } |
| 244 | |
Tom Sepez | 7a73eff | 2016-02-08 13:39:53 -0800 | [diff] [blame] | 245 | bool SendPreSaveToXFADoc(CPDFXFA_Document* pDocument, |
| 246 | std::vector<ScopedFileStream>* fileList) { |
Tom Sepez | d3116dc | 2015-11-24 15:58:06 -0800 | [diff] [blame] | 247 | if (pDocument->GetDocType() != DOCTYPE_DYNAMIC_XFA && |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 248 | pDocument->GetDocType() != DOCTYPE_STATIC_XFA) |
Tom Sepez | 7a73eff | 2016-02-08 13:39:53 -0800 | [diff] [blame] | 249 | return true; |
| 250 | |
dsinclair | df4bc59 | 2016-03-31 20:34:43 -0700 | [diff] [blame] | 251 | CXFA_FFDocView* pXFADocView = pDocument->GetXFADocView(); |
Tom Sepez | 7a73eff | 2016-02-08 13:39:53 -0800 | [diff] [blame] | 252 | if (!pXFADocView) |
| 253 | return true; |
| 254 | |
dsinclair | df4bc59 | 2016-03-31 20:34:43 -0700 | [diff] [blame] | 255 | CXFA_FFWidgetHandler* pWidgetHander = pXFADocView->GetWidgetHandler(); |
tsepez | e7b2853 | 2016-05-18 12:10:49 -0700 | [diff] [blame] | 256 | std::unique_ptr<CXFA_WidgetAccIterator> pWidgetAccIterator( |
| 257 | pXFADocView->CreateWidgetAccIterator()); |
| 258 | while (CXFA_WidgetAcc* pWidgetAcc = pWidgetAccIterator->MoveToNext()) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 259 | CXFA_EventParam preParam; |
| 260 | preParam.m_eType = XFA_EVENT_PreSave; |
| 261 | pWidgetHander->ProcessEvent(pWidgetAcc, &preParam); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 262 | } |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 263 | pXFADocView->UpdateDocView(); |
Tom Sepez | 7a73eff | 2016-02-08 13:39:53 -0800 | [diff] [blame] | 264 | return SaveXFADocumentData(pDocument, fileList); |
Bo Xu | fdc00a7 | 2014-10-28 23:03:33 -0700 | [diff] [blame] | 265 | } |
Tom Sepez | 40e9ff3 | 2015-11-30 12:39:54 -0800 | [diff] [blame] | 266 | #endif // PDF_ENABLE_XFA |
Bo Xu | fdc00a7 | 2014-10-28 23:03:33 -0700 | [diff] [blame] | 267 | |
Tom Sepez | 7a73eff | 2016-02-08 13:39:53 -0800 | [diff] [blame] | 268 | bool FPDF_Doc_Save(FPDF_DOCUMENT document, |
| 269 | FPDF_FILEWRITE* pFileWrite, |
| 270 | FPDF_DWORD flags, |
| 271 | FPDF_BOOL bSetVersion, |
| 272 | int fileVerion) { |
Tom Sepez | 1b24628 | 2015-11-25 15:15:31 -0800 | [diff] [blame] | 273 | CPDF_Document* pPDFDoc = CPDFDocumentFromFPDFDocument(document); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 274 | if (!pPDFDoc) |
| 275 | return 0; |
Lei Zhang | a6d9f0e | 2015-06-13 00:48:38 -0700 | [diff] [blame] | 276 | |
Tom Sepez | 51da093 | 2015-11-25 16:05:49 -0800 | [diff] [blame] | 277 | #ifdef PDF_ENABLE_XFA |
Tom Sepez | 7a73eff | 2016-02-08 13:39:53 -0800 | [diff] [blame] | 278 | CPDFXFA_Document* pDoc = static_cast<CPDFXFA_Document*>(document); |
| 279 | std::vector<ScopedFileStream> fileList; |
| 280 | SendPreSaveToXFADoc(pDoc, &fileList); |
Tom Sepez | 40e9ff3 | 2015-11-30 12:39:54 -0800 | [diff] [blame] | 281 | #endif // PDF_ENABLE_XFA |
Tom Sepez | 1b24628 | 2015-11-25 15:15:31 -0800 | [diff] [blame] | 282 | |
Tom Sepez | 7a73eff | 2016-02-08 13:39:53 -0800 | [diff] [blame] | 283 | if (flags < FPDF_INCREMENTAL || flags > FPDF_REMOVE_SECURITY) |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 284 | flags = 0; |
Lei Zhang | a6d9f0e | 2015-06-13 00:48:38 -0700 | [diff] [blame] | 285 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 286 | CPDF_Creator FileMaker(pPDFDoc); |
| 287 | if (bSetVersion) |
| 288 | FileMaker.SetFileVersion(fileVerion); |
| 289 | if (flags == FPDF_REMOVE_SECURITY) { |
| 290 | flags = 0; |
| 291 | FileMaker.RemoveSecurity(); |
| 292 | } |
Tom Sepez | 1b24628 | 2015-11-25 15:15:31 -0800 | [diff] [blame] | 293 | |
thestig | 1cd352e | 2016-06-07 17:53:06 -0700 | [diff] [blame] | 294 | CFX_IFileWrite* pStreamWrite = new CFX_IFileWrite; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 295 | pStreamWrite->Init(pFileWrite); |
Wei Li | 97da976 | 2016-03-11 17:00:48 -0800 | [diff] [blame] | 296 | bool bRet = FileMaker.Create(pStreamWrite, flags); |
Tom Sepez | 51da093 | 2015-11-25 16:05:49 -0800 | [diff] [blame] | 297 | #ifdef PDF_ENABLE_XFA |
Tom Sepez | 7a73eff | 2016-02-08 13:39:53 -0800 | [diff] [blame] | 298 | SendPostSaveToXFADoc(pDoc); |
Tom Sepez | 40e9ff3 | 2015-11-30 12:39:54 -0800 | [diff] [blame] | 299 | #endif // PDF_ENABLE_XFA |
Lei Zhang | 2b1a2d5 | 2015-08-14 22:16:22 -0700 | [diff] [blame] | 300 | pStreamWrite->Release(); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 301 | return bRet; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 302 | } |
| 303 | |
Tom Sepez | 7a73eff | 2016-02-08 13:39:53 -0800 | [diff] [blame] | 304 | } // namespace |
| 305 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 306 | DLLEXPORT FPDF_BOOL STDCALL FPDF_SaveAsCopy(FPDF_DOCUMENT document, |
| 307 | FPDF_FILEWRITE* pFileWrite, |
| 308 | FPDF_DWORD flags) { |
Tom Sepez | 7a73eff | 2016-02-08 13:39:53 -0800 | [diff] [blame] | 309 | return FPDF_Doc_Save(document, pFileWrite, flags, FALSE, 0); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 310 | } |
| 311 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 312 | DLLEXPORT FPDF_BOOL STDCALL FPDF_SaveWithVersion(FPDF_DOCUMENT document, |
| 313 | FPDF_FILEWRITE* pFileWrite, |
| 314 | FPDF_DWORD flags, |
| 315 | int fileVersion) { |
Tom Sepez | 7a73eff | 2016-02-08 13:39:53 -0800 | [diff] [blame] | 316 | return FPDF_Doc_Save(document, pFileWrite, flags, TRUE, fileVersion); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 317 | } |