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 | 85c8e7f | 2016-11-21 13:50:32 -0500 | [diff] [blame] | 9 | #include <memory> |
| 10 | #include <utility> |
Dan Sinclair | 3ebd121 | 2016-03-09 09:59:23 -0500 | [diff] [blame] | 11 | #include <vector> |
| 12 | |
dsinclair | 2415435 | 2016-10-04 11:01:48 -0700 | [diff] [blame] | 13 | #include "core/fpdfapi/edit/cpdf_creator.h" |
dsinclair | 488b7ad | 2016-10-04 11:55:50 -0700 | [diff] [blame] | 14 | #include "core/fpdfapi/parser/cpdf_array.h" |
| 15 | #include "core/fpdfapi/parser/cpdf_document.h" |
| 16 | #include "core/fpdfapi/parser/cpdf_reference.h" |
| 17 | #include "core/fpdfapi/parser/cpdf_stream_acc.h" |
| 18 | #include "core/fpdfapi/parser/cpdf_string.h" |
dsinclair | a52ab74 | 2016-09-29 13:59:29 -0700 | [diff] [blame] | 19 | #include "core/fxcrt/fx_ext.h" |
dsinclair | 114e46a | 2016-09-29 17:18:21 -0700 | [diff] [blame] | 20 | #include "fpdfsdk/fsdk_define.h" |
Tom Sepez | 40e9ff3 | 2015-11-30 12:39:54 -0800 | [diff] [blame] | 21 | #include "public/fpdf_edit.h" |
| 22 | |
Tom Sepez | 51da093 | 2015-11-25 16:05:49 -0800 | [diff] [blame] | 23 | #ifdef PDF_ENABLE_XFA |
Dan Sinclair | ddb7016 | 2017-03-30 14:01:31 -0400 | [diff] [blame] | 24 | #include "core/fxcrt/cfx_checksumcontext.h" |
dsinclair | 521b750 | 2016-11-02 13:02:28 -0700 | [diff] [blame] | 25 | #include "fpdfsdk/fpdfxfa/cpdfxfa_context.h" |
dsinclair | 4d29e78 | 2016-10-04 14:02:47 -0700 | [diff] [blame] | 26 | #include "fpdfsdk/fpdfxfa/cxfa_fwladaptertimermgr.h" |
Lei Zhang | b4e7f30 | 2015-11-06 15:52:32 -0800 | [diff] [blame] | 27 | #include "public/fpdf_formfill.h" |
dsinclair | 5b49309 | 2016-09-29 20:20:24 -0700 | [diff] [blame] | 28 | #include "xfa/fxfa/cxfa_eventparam.h" |
Dan Sinclair | 80c4878 | 2017-03-23 12:11:20 -0400 | [diff] [blame] | 29 | #include "xfa/fxfa/cxfa_ffapp.h" |
| 30 | #include "xfa/fxfa/cxfa_ffdocview.h" |
| 31 | #include "xfa/fxfa/cxfa_ffwidgethandler.h" |
| 32 | #include "xfa/fxfa/cxfa_widgetacciterator.h" |
Tom Sepez | 51da093 | 2015-11-25 16:05:49 -0800 | [diff] [blame] | 33 | #endif |
Tom Sepez | 1ed8a21 | 2015-05-11 15:25:39 -0700 | [diff] [blame] | 34 | |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 35 | #if _FX_OS_ == _FX_ANDROID_ |
Dan Sinclair | 85c8e7f | 2016-11-21 13:50:32 -0500 | [diff] [blame] | 36 | #include <time.h> |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 37 | #else |
| 38 | #include <ctime> |
| 39 | #endif |
| 40 | |
tsepez | ad2441e | 2016-10-24 10:19:11 -0700 | [diff] [blame] | 41 | class CFX_IFileWrite final : public IFX_WriteStream { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 42 | public: |
tsepez | 833619b | 2016-12-07 09:21:17 -0800 | [diff] [blame] | 43 | static CFX_RetainPtr<CFX_IFileWrite> Create(); |
Dan Sinclair | 0bb1333 | 2017-03-30 16:12:02 -0400 | [diff] [blame^] | 44 | |
tsepez | f39074c | 2016-10-26 15:33:58 -0700 | [diff] [blame] | 45 | bool Init(FPDF_FILEWRITE* pFileWriteStruct); |
| 46 | bool WriteBlock(const void* pData, size_t size) override; |
Lei Zhang | a6d9f0e | 2015-06-13 00:48:38 -0700 | [diff] [blame] | 47 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 48 | protected: |
Dan Sinclair | 0bb1333 | 2017-03-30 16:12:02 -0400 | [diff] [blame^] | 49 | template <typename T, typename... Args> |
| 50 | friend CFX_RetainPtr<T> pdfium::MakeRetain(Args&&... args); |
| 51 | |
tsepez | 833619b | 2016-12-07 09:21:17 -0800 | [diff] [blame] | 52 | CFX_IFileWrite(); |
Lei Zhang | 2b1a2d5 | 2015-08-14 22:16:22 -0700 | [diff] [blame] | 53 | ~CFX_IFileWrite() override {} |
| 54 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 55 | FPDF_FILEWRITE* m_pFileWriteStruct; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 56 | }; |
| 57 | |
tsepez | 833619b | 2016-12-07 09:21:17 -0800 | [diff] [blame] | 58 | CFX_RetainPtr<CFX_IFileWrite> CFX_IFileWrite::Create() { |
Dan Sinclair | 0bb1333 | 2017-03-30 16:12:02 -0400 | [diff] [blame^] | 59 | return pdfium::MakeRetain<CFX_IFileWrite>(); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 60 | } |
| 61 | |
tsepez | 833619b | 2016-12-07 09:21:17 -0800 | [diff] [blame] | 62 | CFX_IFileWrite::CFX_IFileWrite() : m_pFileWriteStruct(nullptr) {} |
| 63 | |
tsepez | f39074c | 2016-10-26 15:33:58 -0700 | [diff] [blame] | 64 | bool CFX_IFileWrite::Init(FPDF_FILEWRITE* pFileWriteStruct) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 65 | if (!pFileWriteStruct) |
tsepez | f39074c | 2016-10-26 15:33:58 -0700 | [diff] [blame] | 66 | return false; |
Tom Sepez | 2f2ffec | 2015-07-23 14:42:09 -0700 | [diff] [blame] | 67 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 68 | m_pFileWriteStruct = pFileWriteStruct; |
tsepez | f39074c | 2016-10-26 15:33:58 -0700 | [diff] [blame] | 69 | return true; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 70 | } |
| 71 | |
tsepez | f39074c | 2016-10-26 15:33:58 -0700 | [diff] [blame] | 72 | bool CFX_IFileWrite::WriteBlock(const void* pData, size_t size) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 73 | if (!m_pFileWriteStruct) |
tsepez | f39074c | 2016-10-26 15:33:58 -0700 | [diff] [blame] | 74 | return false; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 75 | |
| 76 | m_pFileWriteStruct->WriteBlock(m_pFileWriteStruct, pData, size); |
tsepez | f39074c | 2016-10-26 15:33:58 -0700 | [diff] [blame] | 77 | return true; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 78 | } |
| 79 | |
Tom Sepez | 7a73eff | 2016-02-08 13:39:53 -0800 | [diff] [blame] | 80 | namespace { |
| 81 | |
Tom Sepez | 51da093 | 2015-11-25 16:05:49 -0800 | [diff] [blame] | 82 | #ifdef PDF_ENABLE_XFA |
tsepez | 833619b | 2016-12-07 09:21:17 -0800 | [diff] [blame] | 83 | bool SaveXFADocumentData( |
| 84 | CPDFXFA_Context* pContext, |
| 85 | std::vector<CFX_RetainPtr<IFX_SeekableStream>>* fileList) { |
dsinclair | 521b750 | 2016-11-02 13:02:28 -0700 | [diff] [blame] | 86 | if (!pContext) |
Tom Sepez | 7a73eff | 2016-02-08 13:39:53 -0800 | [diff] [blame] | 87 | return false; |
| 88 | |
Dan Sinclair | cdba747 | 2017-03-23 09:17:10 -0400 | [diff] [blame] | 89 | if (pContext->GetDocType() != XFA_DocType::Dynamic && |
| 90 | pContext->GetDocType() != XFA_DocType::Static) { |
Tom Sepez | 7a73eff | 2016-02-08 13:39:53 -0800 | [diff] [blame] | 91 | return true; |
Dan Sinclair | cdba747 | 2017-03-23 09:17:10 -0400 | [diff] [blame] | 92 | } |
Tom Sepez | 7a73eff | 2016-02-08 13:39:53 -0800 | [diff] [blame] | 93 | |
dsinclair | 521b750 | 2016-11-02 13:02:28 -0700 | [diff] [blame] | 94 | CXFA_FFDocView* pXFADocView = pContext->GetXFADocView(); |
Tom Sepez | 7a73eff | 2016-02-08 13:39:53 -0800 | [diff] [blame] | 95 | if (!pXFADocView) |
| 96 | return true; |
Bo Xu | fdc00a7 | 2014-10-28 23:03:33 -0700 | [diff] [blame] | 97 | |
dsinclair | 521b750 | 2016-11-02 13:02:28 -0700 | [diff] [blame] | 98 | CPDF_Document* pPDFDocument = pContext->GetPDFDoc(); |
| 99 | if (!pPDFDocument) |
Tom Sepez | 7a73eff | 2016-02-08 13:39:53 -0800 | [diff] [blame] | 100 | return false; |
Bo Xu | fdc00a7 | 2014-10-28 23:03:33 -0700 | [diff] [blame] | 101 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 102 | CPDF_Dictionary* pRoot = pPDFDocument->GetRoot(); |
Tom Sepez | 7a73eff | 2016-02-08 13:39:53 -0800 | [diff] [blame] | 103 | if (!pRoot) |
| 104 | return false; |
| 105 | |
dsinclair | 38fd844 | 2016-09-15 10:15:32 -0700 | [diff] [blame] | 106 | CPDF_Dictionary* pAcroForm = pRoot->GetDictFor("AcroForm"); |
Tom Sepez | 7a73eff | 2016-02-08 13:39:53 -0800 | [diff] [blame] | 107 | if (!pAcroForm) |
| 108 | return false; |
| 109 | |
dsinclair | 38fd844 | 2016-09-15 10:15:32 -0700 | [diff] [blame] | 110 | CPDF_Object* pXFA = pAcroForm->GetObjectFor("XFA"); |
Tom Sepez | 7a73eff | 2016-02-08 13:39:53 -0800 | [diff] [blame] | 111 | if (!pXFA) |
| 112 | return true; |
| 113 | |
thestig | b8bf55f | 2016-05-21 21:08:05 -0700 | [diff] [blame] | 114 | CPDF_Array* pArray = pXFA->AsArray(); |
Tom Sepez | 7a73eff | 2016-02-08 13:39:53 -0800 | [diff] [blame] | 115 | if (!pArray) |
| 116 | return false; |
| 117 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 118 | int size = pArray->GetCount(); |
| 119 | int iFormIndex = -1; |
| 120 | int iDataSetsIndex = -1; |
| 121 | int iTemplate = -1; |
| 122 | int iLast = size - 2; |
| 123 | for (int i = 0; i < size - 1; i++) { |
tsepez | bd56755 | 2016-03-29 14:51:50 -0700 | [diff] [blame] | 124 | CPDF_Object* pPDFObj = pArray->GetObjectAt(i); |
Tom Sepez | 8e5cd19 | 2016-01-26 13:20:26 -0800 | [diff] [blame] | 125 | if (!pPDFObj->IsString()) |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 126 | continue; |
| 127 | if (pPDFObj->GetString() == "form") |
| 128 | iFormIndex = i + 1; |
| 129 | else if (pPDFObj->GetString() == "datasets") |
| 130 | iDataSetsIndex = i + 1; |
Lei Zhang | d983b09 | 2015-12-14 16:58:33 -0800 | [diff] [blame] | 131 | else if (pPDFObj->GetString() == "template") |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 132 | iTemplate = i + 1; |
| 133 | } |
Dan Sinclair | 0bb1333 | 2017-03-30 16:12:02 -0400 | [diff] [blame^] | 134 | auto pChecksum = pdfium::MakeUnique<CFX_ChecksumContext>(); |
dsinclair | 521b750 | 2016-11-02 13:02:28 -0700 | [diff] [blame] | 135 | pChecksum->StartChecksum(); |
Lei Zhang | a6d9f0e | 2015-06-13 00:48:38 -0700 | [diff] [blame] | 136 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 137 | // template |
| 138 | if (iTemplate > -1) { |
Wei Li | 9b76113 | 2016-01-29 15:44:20 -0800 | [diff] [blame] | 139 | CPDF_Stream* pTemplateStream = pArray->GetStreamAt(iTemplate); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 140 | CPDF_StreamAcc streamAcc; |
| 141 | streamAcc.LoadAllData(pTemplateStream); |
| 142 | uint8_t* pData = (uint8_t*)streamAcc.GetData(); |
tsepez | c3255f5 | 2016-03-25 14:52:27 -0700 | [diff] [blame] | 143 | uint32_t dwSize2 = streamAcc.GetSize(); |
tsepez | 833619b | 2016-12-07 09:21:17 -0800 | [diff] [blame] | 144 | CFX_RetainPtr<IFX_SeekableStream> pTemplate = |
| 145 | IFX_MemoryStream::Create(pData, dwSize2); |
| 146 | pChecksum->UpdateChecksum(pTemplate); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 147 | } |
thestig | 1cd352e | 2016-06-07 17:53:06 -0700 | [diff] [blame] | 148 | CPDF_Stream* pFormStream = nullptr; |
| 149 | CPDF_Stream* pDataSetsStream = nullptr; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 150 | if (iFormIndex != -1) { |
| 151 | // Get form CPDF_Stream |
tsepez | bd56755 | 2016-03-29 14:51:50 -0700 | [diff] [blame] | 152 | CPDF_Object* pFormPDFObj = pArray->GetObjectAt(iFormIndex); |
Tom Sepez | 8e5cd19 | 2016-01-26 13:20:26 -0800 | [diff] [blame] | 153 | if (pFormPDFObj->IsReference()) { |
| 154 | CPDF_Object* pFormDirectObj = pFormPDFObj->GetDirect(); |
| 155 | if (pFormDirectObj && pFormDirectObj->IsStream()) { |
| 156 | pFormStream = (CPDF_Stream*)pFormDirectObj; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 157 | } |
Tom Sepez | 8e5cd19 | 2016-01-26 13:20:26 -0800 | [diff] [blame] | 158 | } else if (pFormPDFObj->IsStream()) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 159 | pFormStream = (CPDF_Stream*)pFormPDFObj; |
Tom Sepez | 2f2ffec | 2015-07-23 14:42:09 -0700 | [diff] [blame] | 160 | } |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 161 | } |
Lei Zhang | a6d9f0e | 2015-06-13 00:48:38 -0700 | [diff] [blame] | 162 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 163 | if (iDataSetsIndex != -1) { |
| 164 | // Get datasets CPDF_Stream |
tsepez | bd56755 | 2016-03-29 14:51:50 -0700 | [diff] [blame] | 165 | CPDF_Object* pDataSetsPDFObj = pArray->GetObjectAt(iDataSetsIndex); |
Tom Sepez | 8e5cd19 | 2016-01-26 13:20:26 -0800 | [diff] [blame] | 166 | if (pDataSetsPDFObj->IsReference()) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 167 | CPDF_Reference* pDataSetsRefObj = (CPDF_Reference*)pDataSetsPDFObj; |
Tom Sepez | 8e5cd19 | 2016-01-26 13:20:26 -0800 | [diff] [blame] | 168 | CPDF_Object* pDataSetsDirectObj = pDataSetsRefObj->GetDirect(); |
| 169 | if (pDataSetsDirectObj && pDataSetsDirectObj->IsStream()) { |
| 170 | pDataSetsStream = (CPDF_Stream*)pDataSetsDirectObj; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 171 | } |
Tom Sepez | 8e5cd19 | 2016-01-26 13:20:26 -0800 | [diff] [blame] | 172 | } else if (pDataSetsPDFObj->IsStream()) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 173 | pDataSetsStream = (CPDF_Stream*)pDataSetsPDFObj; |
Tom Sepez | 2f2ffec | 2015-07-23 14:42:09 -0700 | [diff] [blame] | 174 | } |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 175 | } |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 176 | // L"datasets" |
| 177 | { |
tsepez | 833619b | 2016-12-07 09:21:17 -0800 | [diff] [blame] | 178 | CFX_RetainPtr<IFX_SeekableStream> pDsfileWrite = IFX_MemoryStream::Create(); |
| 179 | if (pXFADocView->GetDoc()->SavePackage(XFA_HASHCODE_Datasets, pDsfileWrite, |
| 180 | nullptr) && |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 181 | pDsfileWrite->GetSize() > 0) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 182 | // Datasets |
tsepez | 833619b | 2016-12-07 09:21:17 -0800 | [diff] [blame] | 183 | pChecksum->UpdateChecksum(pDsfileWrite); |
dsinclair | 521b750 | 2016-11-02 13:02:28 -0700 | [diff] [blame] | 184 | pChecksum->FinishChecksum(); |
tsepez | 9e05ee1 | 2016-11-21 13:19:10 -0800 | [diff] [blame] | 185 | auto pDataDict = pdfium::MakeUnique<CPDF_Dictionary>( |
| 186 | pPDFDocument->GetByteStringPool()); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 187 | if (iDataSetsIndex != -1) { |
tsepez | 9e05ee1 | 2016-11-21 13:19:10 -0800 | [diff] [blame] | 188 | if (pDataSetsStream) { |
tsepez | 833619b | 2016-12-07 09:21:17 -0800 | [diff] [blame] | 189 | pDataSetsStream->InitStreamFromFile(pDsfileWrite, |
tsepez | 9e05ee1 | 2016-11-21 13:19:10 -0800 | [diff] [blame] | 190 | std::move(pDataDict)); |
| 191 | } |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 192 | } else { |
tsepez | 70c4afd | 2016-11-15 11:33:44 -0800 | [diff] [blame] | 193 | CPDF_Stream* pData = pPDFDocument->NewIndirect<CPDF_Stream>(); |
tsepez | 833619b | 2016-12-07 09:21:17 -0800 | [diff] [blame] | 194 | pData->InitStreamFromFile(pDsfileWrite, std::move(pDataDict)); |
Tom Sepez | 3343d14 | 2015-11-02 09:54:54 -0800 | [diff] [blame] | 195 | iLast = pArray->GetCount() - 2; |
tsepez | 8a3aa45 | 2016-11-16 12:26:06 -0800 | [diff] [blame] | 196 | pArray->InsertNewAt<CPDF_String>(iLast, "datasets", false); |
| 197 | pArray->InsertNewAt<CPDF_Reference>(iLast + 1, pPDFDocument, |
| 198 | pData->GetObjNum()); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 199 | } |
Tom Sepez | 7a73eff | 2016-02-08 13:39:53 -0800 | [diff] [blame] | 200 | fileList->push_back(std::move(pDsfileWrite)); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 201 | } |
| 202 | } |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 203 | // L"form" |
| 204 | { |
tsepez | 833619b | 2016-12-07 09:21:17 -0800 | [diff] [blame] | 205 | CFX_RetainPtr<IFX_SeekableStream> pfileWrite = IFX_MemoryStream::Create(); |
| 206 | if (pXFADocView->GetDoc()->SavePackage(XFA_HASHCODE_Form, pfileWrite, |
dsinclair | 521b750 | 2016-11-02 13:02:28 -0700 | [diff] [blame] | 207 | pChecksum.get()) && |
Tom Sepez | 7a73eff | 2016-02-08 13:39:53 -0800 | [diff] [blame] | 208 | pfileWrite->GetSize() > 0) { |
tsepez | 9e05ee1 | 2016-11-21 13:19:10 -0800 | [diff] [blame] | 209 | auto pDataDict = pdfium::MakeUnique<CPDF_Dictionary>( |
| 210 | pPDFDocument->GetByteStringPool()); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 211 | if (iFormIndex != -1) { |
tsepez | 833619b | 2016-12-07 09:21:17 -0800 | [diff] [blame] | 212 | if (pFormStream) |
| 213 | pFormStream->InitStreamFromFile(pfileWrite, std::move(pDataDict)); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 214 | } else { |
tsepez | 70c4afd | 2016-11-15 11:33:44 -0800 | [diff] [blame] | 215 | CPDF_Stream* pData = pPDFDocument->NewIndirect<CPDF_Stream>(); |
tsepez | 833619b | 2016-12-07 09:21:17 -0800 | [diff] [blame] | 216 | pData->InitStreamFromFile(pfileWrite, std::move(pDataDict)); |
Tom Sepez | 3343d14 | 2015-11-02 09:54:54 -0800 | [diff] [blame] | 217 | iLast = pArray->GetCount() - 2; |
tsepez | 8a3aa45 | 2016-11-16 12:26:06 -0800 | [diff] [blame] | 218 | pArray->InsertNewAt<CPDF_String>(iLast, "form", false); |
| 219 | pArray->InsertNewAt<CPDF_Reference>(iLast + 1, pPDFDocument, |
| 220 | pData->GetObjNum()); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 221 | } |
Tom Sepez | 7a73eff | 2016-02-08 13:39:53 -0800 | [diff] [blame] | 222 | fileList->push_back(std::move(pfileWrite)); |
Tom Sepez | 2f2ffec | 2015-07-23 14:42:09 -0700 | [diff] [blame] | 223 | } |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 224 | } |
Tom Sepez | 7a73eff | 2016-02-08 13:39:53 -0800 | [diff] [blame] | 225 | return true; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 226 | } |
| 227 | |
dsinclair | 521b750 | 2016-11-02 13:02:28 -0700 | [diff] [blame] | 228 | bool SendPostSaveToXFADoc(CPDFXFA_Context* pContext) { |
| 229 | if (!pContext) |
Tom Sepez | 7a73eff | 2016-02-08 13:39:53 -0800 | [diff] [blame] | 230 | return false; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 231 | |
Dan Sinclair | cdba747 | 2017-03-23 09:17:10 -0400 | [diff] [blame] | 232 | if (pContext->GetDocType() != XFA_DocType::Dynamic && |
| 233 | pContext->GetDocType() != XFA_DocType::Static) |
Tom Sepez | 7a73eff | 2016-02-08 13:39:53 -0800 | [diff] [blame] | 234 | return true; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 235 | |
dsinclair | 521b750 | 2016-11-02 13:02:28 -0700 | [diff] [blame] | 236 | CXFA_FFDocView* pXFADocView = pContext->GetXFADocView(); |
Tom Sepez | 7a73eff | 2016-02-08 13:39:53 -0800 | [diff] [blame] | 237 | if (!pXFADocView) |
| 238 | return false; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 239 | |
dsinclair | df4bc59 | 2016-03-31 20:34:43 -0700 | [diff] [blame] | 240 | CXFA_FFWidgetHandler* pWidgetHander = pXFADocView->GetWidgetHandler(); |
tsepez | e7b2853 | 2016-05-18 12:10:49 -0700 | [diff] [blame] | 241 | std::unique_ptr<CXFA_WidgetAccIterator> pWidgetAccIterator( |
| 242 | pXFADocView->CreateWidgetAccIterator()); |
| 243 | while (CXFA_WidgetAcc* pWidgetAcc = pWidgetAccIterator->MoveToNext()) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 244 | CXFA_EventParam preParam; |
| 245 | preParam.m_eType = XFA_EVENT_PostSave; |
| 246 | pWidgetHander->ProcessEvent(pWidgetAcc, &preParam); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 247 | } |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 248 | pXFADocView->UpdateDocView(); |
dsinclair | 521b750 | 2016-11-02 13:02:28 -0700 | [diff] [blame] | 249 | pContext->ClearChangeMark(); |
Tom Sepez | 7a73eff | 2016-02-08 13:39:53 -0800 | [diff] [blame] | 250 | return true; |
Bo Xu | fdc00a7 | 2014-10-28 23:03:33 -0700 | [diff] [blame] | 251 | } |
| 252 | |
tsepez | 833619b | 2016-12-07 09:21:17 -0800 | [diff] [blame] | 253 | bool SendPreSaveToXFADoc( |
| 254 | CPDFXFA_Context* pContext, |
| 255 | std::vector<CFX_RetainPtr<IFX_SeekableStream>>* fileList) { |
Dan Sinclair | cdba747 | 2017-03-23 09:17:10 -0400 | [diff] [blame] | 256 | if (pContext->GetDocType() != XFA_DocType::Dynamic && |
| 257 | pContext->GetDocType() != XFA_DocType::Static) |
Tom Sepez | 7a73eff | 2016-02-08 13:39:53 -0800 | [diff] [blame] | 258 | return true; |
| 259 | |
dsinclair | 521b750 | 2016-11-02 13:02:28 -0700 | [diff] [blame] | 260 | CXFA_FFDocView* pXFADocView = pContext->GetXFADocView(); |
Tom Sepez | 7a73eff | 2016-02-08 13:39:53 -0800 | [diff] [blame] | 261 | if (!pXFADocView) |
| 262 | return true; |
| 263 | |
dsinclair | df4bc59 | 2016-03-31 20:34:43 -0700 | [diff] [blame] | 264 | CXFA_FFWidgetHandler* pWidgetHander = pXFADocView->GetWidgetHandler(); |
tsepez | e7b2853 | 2016-05-18 12:10:49 -0700 | [diff] [blame] | 265 | std::unique_ptr<CXFA_WidgetAccIterator> pWidgetAccIterator( |
| 266 | pXFADocView->CreateWidgetAccIterator()); |
| 267 | while (CXFA_WidgetAcc* pWidgetAcc = pWidgetAccIterator->MoveToNext()) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 268 | CXFA_EventParam preParam; |
| 269 | preParam.m_eType = XFA_EVENT_PreSave; |
| 270 | pWidgetHander->ProcessEvent(pWidgetAcc, &preParam); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 271 | } |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 272 | pXFADocView->UpdateDocView(); |
dsinclair | 521b750 | 2016-11-02 13:02:28 -0700 | [diff] [blame] | 273 | return SaveXFADocumentData(pContext, fileList); |
Bo Xu | fdc00a7 | 2014-10-28 23:03:33 -0700 | [diff] [blame] | 274 | } |
Tom Sepez | 40e9ff3 | 2015-11-30 12:39:54 -0800 | [diff] [blame] | 275 | #endif // PDF_ENABLE_XFA |
Bo Xu | fdc00a7 | 2014-10-28 23:03:33 -0700 | [diff] [blame] | 276 | |
Tom Sepez | 7a73eff | 2016-02-08 13:39:53 -0800 | [diff] [blame] | 277 | bool FPDF_Doc_Save(FPDF_DOCUMENT document, |
| 278 | FPDF_FILEWRITE* pFileWrite, |
| 279 | FPDF_DWORD flags, |
| 280 | FPDF_BOOL bSetVersion, |
| 281 | int fileVerion) { |
Tom Sepez | 1b24628 | 2015-11-25 15:15:31 -0800 | [diff] [blame] | 282 | CPDF_Document* pPDFDoc = CPDFDocumentFromFPDFDocument(document); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 283 | if (!pPDFDoc) |
| 284 | return 0; |
Lei Zhang | a6d9f0e | 2015-06-13 00:48:38 -0700 | [diff] [blame] | 285 | |
Tom Sepez | 51da093 | 2015-11-25 16:05:49 -0800 | [diff] [blame] | 286 | #ifdef PDF_ENABLE_XFA |
dsinclair | 521b750 | 2016-11-02 13:02:28 -0700 | [diff] [blame] | 287 | CPDFXFA_Context* pContext = static_cast<CPDFXFA_Context*>(document); |
tsepez | 833619b | 2016-12-07 09:21:17 -0800 | [diff] [blame] | 288 | std::vector<CFX_RetainPtr<IFX_SeekableStream>> fileList; |
dsinclair | 521b750 | 2016-11-02 13:02:28 -0700 | [diff] [blame] | 289 | SendPreSaveToXFADoc(pContext, &fileList); |
Tom Sepez | 40e9ff3 | 2015-11-30 12:39:54 -0800 | [diff] [blame] | 290 | #endif // PDF_ENABLE_XFA |
Tom Sepez | 1b24628 | 2015-11-25 15:15:31 -0800 | [diff] [blame] | 291 | |
Tom Sepez | 7a73eff | 2016-02-08 13:39:53 -0800 | [diff] [blame] | 292 | if (flags < FPDF_INCREMENTAL || flags > FPDF_REMOVE_SECURITY) |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 293 | flags = 0; |
Lei Zhang | a6d9f0e | 2015-06-13 00:48:38 -0700 | [diff] [blame] | 294 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 295 | CPDF_Creator FileMaker(pPDFDoc); |
| 296 | if (bSetVersion) |
| 297 | FileMaker.SetFileVersion(fileVerion); |
| 298 | if (flags == FPDF_REMOVE_SECURITY) { |
| 299 | flags = 0; |
| 300 | FileMaker.RemoveSecurity(); |
| 301 | } |
Tom Sepez | 1b24628 | 2015-11-25 15:15:31 -0800 | [diff] [blame] | 302 | |
tsepez | 833619b | 2016-12-07 09:21:17 -0800 | [diff] [blame] | 303 | CFX_RetainPtr<CFX_IFileWrite> pStreamWrite = CFX_IFileWrite::Create(); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 304 | pStreamWrite->Init(pFileWrite); |
Wei Li | 97da976 | 2016-03-11 17:00:48 -0800 | [diff] [blame] | 305 | bool bRet = FileMaker.Create(pStreamWrite, flags); |
Tom Sepez | 51da093 | 2015-11-25 16:05:49 -0800 | [diff] [blame] | 306 | #ifdef PDF_ENABLE_XFA |
dsinclair | 521b750 | 2016-11-02 13:02:28 -0700 | [diff] [blame] | 307 | SendPostSaveToXFADoc(pContext); |
Tom Sepez | 40e9ff3 | 2015-11-30 12:39:54 -0800 | [diff] [blame] | 308 | #endif // PDF_ENABLE_XFA |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 309 | return bRet; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 310 | } |
| 311 | |
Tom Sepez | 7a73eff | 2016-02-08 13:39:53 -0800 | [diff] [blame] | 312 | } // namespace |
| 313 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 314 | DLLEXPORT FPDF_BOOL STDCALL FPDF_SaveAsCopy(FPDF_DOCUMENT document, |
| 315 | FPDF_FILEWRITE* pFileWrite, |
| 316 | FPDF_DWORD flags) { |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 317 | return FPDF_Doc_Save(document, pFileWrite, flags, false, 0); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 318 | } |
| 319 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 320 | DLLEXPORT FPDF_BOOL STDCALL FPDF_SaveWithVersion(FPDF_DOCUMENT document, |
| 321 | FPDF_FILEWRITE* pFileWrite, |
| 322 | FPDF_DWORD flags, |
| 323 | int fileVersion) { |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 324 | return FPDF_Doc_Save(document, pFileWrite, flags, true, fileVersion); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 325 | } |