blob: 6894f8c2ed8e375e73333a1f849de79cef8c859e [file] [log] [blame]
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001// 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 Zhanga6d9f0e2015-06-13 00:48:38 -07004
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07005// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
6
Lei Zhangb4e7f302015-11-06 15:52:32 -08007#include "public/fpdf_save.h"
8
Dan Sinclair85c8e7f2016-11-21 13:50:32 -05009#include <memory>
10#include <utility>
Dan Sinclair3ebd1212016-03-09 09:59:23 -050011#include <vector>
12
dsinclair24154352016-10-04 11:01:48 -070013#include "core/fpdfapi/edit/cpdf_creator.h"
dsinclair488b7ad2016-10-04 11:55:50 -070014#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"
dsinclaira52ab742016-09-29 13:59:29 -070019#include "core/fxcrt/fx_ext.h"
dsinclair114e46a2016-09-29 17:18:21 -070020#include "fpdfsdk/fsdk_define.h"
Tom Sepez40e9ff32015-11-30 12:39:54 -080021#include "public/fpdf_edit.h"
22
Tom Sepez51da0932015-11-25 16:05:49 -080023#ifdef PDF_ENABLE_XFA
Dan Sinclairddb70162017-03-30 14:01:31 -040024#include "core/fxcrt/cfx_checksumcontext.h"
dsinclair521b7502016-11-02 13:02:28 -070025#include "fpdfsdk/fpdfxfa/cpdfxfa_context.h"
dsinclair4d29e782016-10-04 14:02:47 -070026#include "fpdfsdk/fpdfxfa/cxfa_fwladaptertimermgr.h"
Lei Zhangb4e7f302015-11-06 15:52:32 -080027#include "public/fpdf_formfill.h"
dsinclair5b493092016-09-29 20:20:24 -070028#include "xfa/fxfa/cxfa_eventparam.h"
Dan Sinclair80c48782017-03-23 12:11:20 -040029#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 Sepez51da0932015-11-25 16:05:49 -080033#endif
Tom Sepez1ed8a212015-05-11 15:25:39 -070034
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070035#if _FX_OS_ == _FX_ANDROID_
Dan Sinclair85c8e7f2016-11-21 13:50:32 -050036#include <time.h>
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070037#else
38#include <ctime>
39#endif
40
tsepezad2441e2016-10-24 10:19:11 -070041class CFX_IFileWrite final : public IFX_WriteStream {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070042 public:
tsepez833619b2016-12-07 09:21:17 -080043 static CFX_RetainPtr<CFX_IFileWrite> Create();
Dan Sinclair0bb13332017-03-30 16:12:02 -040044
tsepezf39074c2016-10-26 15:33:58 -070045 bool Init(FPDF_FILEWRITE* pFileWriteStruct);
46 bool WriteBlock(const void* pData, size_t size) override;
Lei Zhanga6d9f0e2015-06-13 00:48:38 -070047
Nico Weber9d8ec5a2015-08-04 13:00:21 -070048 protected:
Dan Sinclair0bb13332017-03-30 16:12:02 -040049 template <typename T, typename... Args>
50 friend CFX_RetainPtr<T> pdfium::MakeRetain(Args&&... args);
51
tsepez833619b2016-12-07 09:21:17 -080052 CFX_IFileWrite();
Lei Zhang2b1a2d52015-08-14 22:16:22 -070053 ~CFX_IFileWrite() override {}
54
Nico Weber9d8ec5a2015-08-04 13:00:21 -070055 FPDF_FILEWRITE* m_pFileWriteStruct;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070056};
57
tsepez833619b2016-12-07 09:21:17 -080058CFX_RetainPtr<CFX_IFileWrite> CFX_IFileWrite::Create() {
Dan Sinclair0bb13332017-03-30 16:12:02 -040059 return pdfium::MakeRetain<CFX_IFileWrite>();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070060}
61
tsepez833619b2016-12-07 09:21:17 -080062CFX_IFileWrite::CFX_IFileWrite() : m_pFileWriteStruct(nullptr) {}
63
tsepezf39074c2016-10-26 15:33:58 -070064bool CFX_IFileWrite::Init(FPDF_FILEWRITE* pFileWriteStruct) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070065 if (!pFileWriteStruct)
tsepezf39074c2016-10-26 15:33:58 -070066 return false;
Tom Sepez2f2ffec2015-07-23 14:42:09 -070067
Nico Weber9d8ec5a2015-08-04 13:00:21 -070068 m_pFileWriteStruct = pFileWriteStruct;
tsepezf39074c2016-10-26 15:33:58 -070069 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -070070}
71
tsepezf39074c2016-10-26 15:33:58 -070072bool CFX_IFileWrite::WriteBlock(const void* pData, size_t size) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070073 if (!m_pFileWriteStruct)
tsepezf39074c2016-10-26 15:33:58 -070074 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -070075
76 m_pFileWriteStruct->WriteBlock(m_pFileWriteStruct, pData, size);
tsepezf39074c2016-10-26 15:33:58 -070077 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -070078}
79
Tom Sepez7a73eff2016-02-08 13:39:53 -080080namespace {
81
Tom Sepez51da0932015-11-25 16:05:49 -080082#ifdef PDF_ENABLE_XFA
tsepez833619b2016-12-07 09:21:17 -080083bool SaveXFADocumentData(
84 CPDFXFA_Context* pContext,
85 std::vector<CFX_RetainPtr<IFX_SeekableStream>>* fileList) {
dsinclair521b7502016-11-02 13:02:28 -070086 if (!pContext)
Tom Sepez7a73eff2016-02-08 13:39:53 -080087 return false;
88
Dan Sinclaircdba7472017-03-23 09:17:10 -040089 if (pContext->GetDocType() != XFA_DocType::Dynamic &&
90 pContext->GetDocType() != XFA_DocType::Static) {
Tom Sepez7a73eff2016-02-08 13:39:53 -080091 return true;
Dan Sinclaircdba7472017-03-23 09:17:10 -040092 }
Tom Sepez7a73eff2016-02-08 13:39:53 -080093
dsinclair521b7502016-11-02 13:02:28 -070094 CXFA_FFDocView* pXFADocView = pContext->GetXFADocView();
Tom Sepez7a73eff2016-02-08 13:39:53 -080095 if (!pXFADocView)
96 return true;
Bo Xufdc00a72014-10-28 23:03:33 -070097
dsinclair521b7502016-11-02 13:02:28 -070098 CPDF_Document* pPDFDocument = pContext->GetPDFDoc();
99 if (!pPDFDocument)
Tom Sepez7a73eff2016-02-08 13:39:53 -0800100 return false;
Bo Xufdc00a72014-10-28 23:03:33 -0700101
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700102 CPDF_Dictionary* pRoot = pPDFDocument->GetRoot();
Tom Sepez7a73eff2016-02-08 13:39:53 -0800103 if (!pRoot)
104 return false;
105
dsinclair38fd8442016-09-15 10:15:32 -0700106 CPDF_Dictionary* pAcroForm = pRoot->GetDictFor("AcroForm");
Tom Sepez7a73eff2016-02-08 13:39:53 -0800107 if (!pAcroForm)
108 return false;
109
dsinclair38fd8442016-09-15 10:15:32 -0700110 CPDF_Object* pXFA = pAcroForm->GetObjectFor("XFA");
Tom Sepez7a73eff2016-02-08 13:39:53 -0800111 if (!pXFA)
112 return true;
113
thestigb8bf55f2016-05-21 21:08:05 -0700114 CPDF_Array* pArray = pXFA->AsArray();
Tom Sepez7a73eff2016-02-08 13:39:53 -0800115 if (!pArray)
116 return false;
117
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700118 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++) {
tsepezbd567552016-03-29 14:51:50 -0700124 CPDF_Object* pPDFObj = pArray->GetObjectAt(i);
Tom Sepez8e5cd192016-01-26 13:20:26 -0800125 if (!pPDFObj->IsString())
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700126 continue;
127 if (pPDFObj->GetString() == "form")
128 iFormIndex = i + 1;
129 else if (pPDFObj->GetString() == "datasets")
130 iDataSetsIndex = i + 1;
Lei Zhangd983b092015-12-14 16:58:33 -0800131 else if (pPDFObj->GetString() == "template")
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700132 iTemplate = i + 1;
133 }
Dan Sinclair0bb13332017-03-30 16:12:02 -0400134 auto pChecksum = pdfium::MakeUnique<CFX_ChecksumContext>();
dsinclair521b7502016-11-02 13:02:28 -0700135 pChecksum->StartChecksum();
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700136
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700137 // template
138 if (iTemplate > -1) {
Wei Li9b761132016-01-29 15:44:20 -0800139 CPDF_Stream* pTemplateStream = pArray->GetStreamAt(iTemplate);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700140 CPDF_StreamAcc streamAcc;
141 streamAcc.LoadAllData(pTemplateStream);
142 uint8_t* pData = (uint8_t*)streamAcc.GetData();
tsepezc3255f52016-03-25 14:52:27 -0700143 uint32_t dwSize2 = streamAcc.GetSize();
tsepez833619b2016-12-07 09:21:17 -0800144 CFX_RetainPtr<IFX_SeekableStream> pTemplate =
145 IFX_MemoryStream::Create(pData, dwSize2);
146 pChecksum->UpdateChecksum(pTemplate);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700147 }
thestig1cd352e2016-06-07 17:53:06 -0700148 CPDF_Stream* pFormStream = nullptr;
149 CPDF_Stream* pDataSetsStream = nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700150 if (iFormIndex != -1) {
151 // Get form CPDF_Stream
tsepezbd567552016-03-29 14:51:50 -0700152 CPDF_Object* pFormPDFObj = pArray->GetObjectAt(iFormIndex);
Tom Sepez8e5cd192016-01-26 13:20:26 -0800153 if (pFormPDFObj->IsReference()) {
154 CPDF_Object* pFormDirectObj = pFormPDFObj->GetDirect();
155 if (pFormDirectObj && pFormDirectObj->IsStream()) {
156 pFormStream = (CPDF_Stream*)pFormDirectObj;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700157 }
Tom Sepez8e5cd192016-01-26 13:20:26 -0800158 } else if (pFormPDFObj->IsStream()) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700159 pFormStream = (CPDF_Stream*)pFormPDFObj;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700160 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700161 }
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700162
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700163 if (iDataSetsIndex != -1) {
164 // Get datasets CPDF_Stream
tsepezbd567552016-03-29 14:51:50 -0700165 CPDF_Object* pDataSetsPDFObj = pArray->GetObjectAt(iDataSetsIndex);
Tom Sepez8e5cd192016-01-26 13:20:26 -0800166 if (pDataSetsPDFObj->IsReference()) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700167 CPDF_Reference* pDataSetsRefObj = (CPDF_Reference*)pDataSetsPDFObj;
Tom Sepez8e5cd192016-01-26 13:20:26 -0800168 CPDF_Object* pDataSetsDirectObj = pDataSetsRefObj->GetDirect();
169 if (pDataSetsDirectObj && pDataSetsDirectObj->IsStream()) {
170 pDataSetsStream = (CPDF_Stream*)pDataSetsDirectObj;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700171 }
Tom Sepez8e5cd192016-01-26 13:20:26 -0800172 } else if (pDataSetsPDFObj->IsStream()) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700173 pDataSetsStream = (CPDF_Stream*)pDataSetsPDFObj;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700174 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700175 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700176 // L"datasets"
177 {
tsepez833619b2016-12-07 09:21:17 -0800178 CFX_RetainPtr<IFX_SeekableStream> pDsfileWrite = IFX_MemoryStream::Create();
179 if (pXFADocView->GetDoc()->SavePackage(XFA_HASHCODE_Datasets, pDsfileWrite,
180 nullptr) &&
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700181 pDsfileWrite->GetSize() > 0) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700182 // Datasets
tsepez833619b2016-12-07 09:21:17 -0800183 pChecksum->UpdateChecksum(pDsfileWrite);
dsinclair521b7502016-11-02 13:02:28 -0700184 pChecksum->FinishChecksum();
tsepez9e05ee12016-11-21 13:19:10 -0800185 auto pDataDict = pdfium::MakeUnique<CPDF_Dictionary>(
186 pPDFDocument->GetByteStringPool());
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700187 if (iDataSetsIndex != -1) {
tsepez9e05ee12016-11-21 13:19:10 -0800188 if (pDataSetsStream) {
tsepez833619b2016-12-07 09:21:17 -0800189 pDataSetsStream->InitStreamFromFile(pDsfileWrite,
tsepez9e05ee12016-11-21 13:19:10 -0800190 std::move(pDataDict));
191 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700192 } else {
tsepez70c4afd2016-11-15 11:33:44 -0800193 CPDF_Stream* pData = pPDFDocument->NewIndirect<CPDF_Stream>();
tsepez833619b2016-12-07 09:21:17 -0800194 pData->InitStreamFromFile(pDsfileWrite, std::move(pDataDict));
Tom Sepez3343d142015-11-02 09:54:54 -0800195 iLast = pArray->GetCount() - 2;
tsepez8a3aa452016-11-16 12:26:06 -0800196 pArray->InsertNewAt<CPDF_String>(iLast, "datasets", false);
197 pArray->InsertNewAt<CPDF_Reference>(iLast + 1, pPDFDocument,
198 pData->GetObjNum());
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700199 }
Tom Sepez7a73eff2016-02-08 13:39:53 -0800200 fileList->push_back(std::move(pDsfileWrite));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700201 }
202 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700203 // L"form"
204 {
tsepez833619b2016-12-07 09:21:17 -0800205 CFX_RetainPtr<IFX_SeekableStream> pfileWrite = IFX_MemoryStream::Create();
206 if (pXFADocView->GetDoc()->SavePackage(XFA_HASHCODE_Form, pfileWrite,
dsinclair521b7502016-11-02 13:02:28 -0700207 pChecksum.get()) &&
Tom Sepez7a73eff2016-02-08 13:39:53 -0800208 pfileWrite->GetSize() > 0) {
tsepez9e05ee12016-11-21 13:19:10 -0800209 auto pDataDict = pdfium::MakeUnique<CPDF_Dictionary>(
210 pPDFDocument->GetByteStringPool());
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700211 if (iFormIndex != -1) {
tsepez833619b2016-12-07 09:21:17 -0800212 if (pFormStream)
213 pFormStream->InitStreamFromFile(pfileWrite, std::move(pDataDict));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700214 } else {
tsepez70c4afd2016-11-15 11:33:44 -0800215 CPDF_Stream* pData = pPDFDocument->NewIndirect<CPDF_Stream>();
tsepez833619b2016-12-07 09:21:17 -0800216 pData->InitStreamFromFile(pfileWrite, std::move(pDataDict));
Tom Sepez3343d142015-11-02 09:54:54 -0800217 iLast = pArray->GetCount() - 2;
tsepez8a3aa452016-11-16 12:26:06 -0800218 pArray->InsertNewAt<CPDF_String>(iLast, "form", false);
219 pArray->InsertNewAt<CPDF_Reference>(iLast + 1, pPDFDocument,
220 pData->GetObjNum());
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700221 }
Tom Sepez7a73eff2016-02-08 13:39:53 -0800222 fileList->push_back(std::move(pfileWrite));
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700223 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700224 }
Tom Sepez7a73eff2016-02-08 13:39:53 -0800225 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700226}
227
dsinclair521b7502016-11-02 13:02:28 -0700228bool SendPostSaveToXFADoc(CPDFXFA_Context* pContext) {
229 if (!pContext)
Tom Sepez7a73eff2016-02-08 13:39:53 -0800230 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700231
Dan Sinclaircdba7472017-03-23 09:17:10 -0400232 if (pContext->GetDocType() != XFA_DocType::Dynamic &&
233 pContext->GetDocType() != XFA_DocType::Static)
Tom Sepez7a73eff2016-02-08 13:39:53 -0800234 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700235
dsinclair521b7502016-11-02 13:02:28 -0700236 CXFA_FFDocView* pXFADocView = pContext->GetXFADocView();
Tom Sepez7a73eff2016-02-08 13:39:53 -0800237 if (!pXFADocView)
238 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700239
dsinclairdf4bc592016-03-31 20:34:43 -0700240 CXFA_FFWidgetHandler* pWidgetHander = pXFADocView->GetWidgetHandler();
tsepeze7b28532016-05-18 12:10:49 -0700241 std::unique_ptr<CXFA_WidgetAccIterator> pWidgetAccIterator(
242 pXFADocView->CreateWidgetAccIterator());
243 while (CXFA_WidgetAcc* pWidgetAcc = pWidgetAccIterator->MoveToNext()) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700244 CXFA_EventParam preParam;
245 preParam.m_eType = XFA_EVENT_PostSave;
246 pWidgetHander->ProcessEvent(pWidgetAcc, &preParam);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700247 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700248 pXFADocView->UpdateDocView();
dsinclair521b7502016-11-02 13:02:28 -0700249 pContext->ClearChangeMark();
Tom Sepez7a73eff2016-02-08 13:39:53 -0800250 return true;
Bo Xufdc00a72014-10-28 23:03:33 -0700251}
252
tsepez833619b2016-12-07 09:21:17 -0800253bool SendPreSaveToXFADoc(
254 CPDFXFA_Context* pContext,
255 std::vector<CFX_RetainPtr<IFX_SeekableStream>>* fileList) {
Dan Sinclaircdba7472017-03-23 09:17:10 -0400256 if (pContext->GetDocType() != XFA_DocType::Dynamic &&
257 pContext->GetDocType() != XFA_DocType::Static)
Tom Sepez7a73eff2016-02-08 13:39:53 -0800258 return true;
259
dsinclair521b7502016-11-02 13:02:28 -0700260 CXFA_FFDocView* pXFADocView = pContext->GetXFADocView();
Tom Sepez7a73eff2016-02-08 13:39:53 -0800261 if (!pXFADocView)
262 return true;
263
dsinclairdf4bc592016-03-31 20:34:43 -0700264 CXFA_FFWidgetHandler* pWidgetHander = pXFADocView->GetWidgetHandler();
tsepeze7b28532016-05-18 12:10:49 -0700265 std::unique_ptr<CXFA_WidgetAccIterator> pWidgetAccIterator(
266 pXFADocView->CreateWidgetAccIterator());
267 while (CXFA_WidgetAcc* pWidgetAcc = pWidgetAccIterator->MoveToNext()) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700268 CXFA_EventParam preParam;
269 preParam.m_eType = XFA_EVENT_PreSave;
270 pWidgetHander->ProcessEvent(pWidgetAcc, &preParam);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700271 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700272 pXFADocView->UpdateDocView();
dsinclair521b7502016-11-02 13:02:28 -0700273 return SaveXFADocumentData(pContext, fileList);
Bo Xufdc00a72014-10-28 23:03:33 -0700274}
Tom Sepez40e9ff32015-11-30 12:39:54 -0800275#endif // PDF_ENABLE_XFA
Bo Xufdc00a72014-10-28 23:03:33 -0700276
Tom Sepez7a73eff2016-02-08 13:39:53 -0800277bool FPDF_Doc_Save(FPDF_DOCUMENT document,
278 FPDF_FILEWRITE* pFileWrite,
279 FPDF_DWORD flags,
280 FPDF_BOOL bSetVersion,
281 int fileVerion) {
Tom Sepez1b246282015-11-25 15:15:31 -0800282 CPDF_Document* pPDFDoc = CPDFDocumentFromFPDFDocument(document);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700283 if (!pPDFDoc)
284 return 0;
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700285
Tom Sepez51da0932015-11-25 16:05:49 -0800286#ifdef PDF_ENABLE_XFA
dsinclair521b7502016-11-02 13:02:28 -0700287 CPDFXFA_Context* pContext = static_cast<CPDFXFA_Context*>(document);
tsepez833619b2016-12-07 09:21:17 -0800288 std::vector<CFX_RetainPtr<IFX_SeekableStream>> fileList;
dsinclair521b7502016-11-02 13:02:28 -0700289 SendPreSaveToXFADoc(pContext, &fileList);
Tom Sepez40e9ff32015-11-30 12:39:54 -0800290#endif // PDF_ENABLE_XFA
Tom Sepez1b246282015-11-25 15:15:31 -0800291
Tom Sepez7a73eff2016-02-08 13:39:53 -0800292 if (flags < FPDF_INCREMENTAL || flags > FPDF_REMOVE_SECURITY)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700293 flags = 0;
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700294
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700295 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 Sepez1b246282015-11-25 15:15:31 -0800302
tsepez833619b2016-12-07 09:21:17 -0800303 CFX_RetainPtr<CFX_IFileWrite> pStreamWrite = CFX_IFileWrite::Create();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700304 pStreamWrite->Init(pFileWrite);
Wei Li97da9762016-03-11 17:00:48 -0800305 bool bRet = FileMaker.Create(pStreamWrite, flags);
Tom Sepez51da0932015-11-25 16:05:49 -0800306#ifdef PDF_ENABLE_XFA
dsinclair521b7502016-11-02 13:02:28 -0700307 SendPostSaveToXFADoc(pContext);
Tom Sepez40e9ff32015-11-30 12:39:54 -0800308#endif // PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700309 return bRet;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700310}
311
Tom Sepez7a73eff2016-02-08 13:39:53 -0800312} // namespace
313
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700314DLLEXPORT FPDF_BOOL STDCALL FPDF_SaveAsCopy(FPDF_DOCUMENT document,
315 FPDF_FILEWRITE* pFileWrite,
316 FPDF_DWORD flags) {
tsepez4cf55152016-11-02 14:37:54 -0700317 return FPDF_Doc_Save(document, pFileWrite, flags, false, 0);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700318}
319
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700320DLLEXPORT FPDF_BOOL STDCALL FPDF_SaveWithVersion(FPDF_DOCUMENT document,
321 FPDF_FILEWRITE* pFileWrite,
322 FPDF_DWORD flags,
323 int fileVersion) {
tsepez4cf55152016-11-02 14:37:54 -0700324 return FPDF_Doc_Save(document, pFileWrite, flags, true, fileVersion);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700325}