blob: 75ed27ced92ee1037371128f2baf0039cb79742b [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_edit.h"
8
Tom Sepez51da0932015-11-25 16:05:49 -08009#ifdef PDF_ENABLE_XFA
Bo Xufdc00a72014-10-28 23:03:33 -070010#include "../include/fpdfxfa/fpdfxfa_app.h"
Lei Zhangbde53d22015-11-12 22:21:30 -080011#include "../include/fpdfxfa/fpdfxfa_doc.h"
Bo Xufdc00a72014-10-28 23:03:33 -070012#include "../include/fpdfxfa/fpdfxfa_page.h"
Tom Sepez51da0932015-11-25 16:05:49 -080013#endif
Lei Zhangbde53d22015-11-12 22:21:30 -080014#include "fpdfsdk/include/fsdk_define.h"
Lei Zhangb4e7f302015-11-06 15:52:32 -080015#include "public/fpdf_formfill.h"
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070016
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070017#if _FX_OS_ == _FX_ANDROID_
18#include "time.h"
19#else
20#include <ctime>
21#endif
22
Nico Weber9d8ec5a2015-08-04 13:00:21 -070023DLLEXPORT FPDF_DOCUMENT STDCALL FPDF_CreateNewDocument() {
Tom Sepezae51c812015-08-05 12:34:06 -070024 CPDF_Document* pDoc = new CPDF_Document;
Nico Weber9d8ec5a2015-08-04 13:00:21 -070025 pDoc->CreateNewDoc();
26 time_t currentTime;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070027
Nico Weber9d8ec5a2015-08-04 13:00:21 -070028 CFX_ByteString DateStr;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070029
Nico Weber9d8ec5a2015-08-04 13:00:21 -070030 if (FSDK_IsSandBoxPolicyEnabled(FPDF_POLICY_MACHINETIME_ACCESS)) {
31 if (-1 != time(&currentTime)) {
32 tm* pTM = localtime(&currentTime);
33 if (pTM) {
34 DateStr.Format("D:%04d%02d%02d%02d%02d%02d", pTM->tm_year + 1900,
35 pTM->tm_mon + 1, pTM->tm_mday, pTM->tm_hour, pTM->tm_min,
36 pTM->tm_sec);
37 }
38 }
39 }
Tom Sepezbdeeb8a2015-05-27 12:25:00 -070040
Nico Weber9d8ec5a2015-08-04 13:00:21 -070041 CPDF_Dictionary* pInfoDict = NULL;
42 pInfoDict = pDoc->GetInfo();
43 if (pInfoDict) {
44 if (FSDK_IsSandBoxPolicyEnabled(FPDF_POLICY_MACHINETIME_ACCESS))
45 pInfoDict->SetAt("CreationDate", new CPDF_String(DateStr));
Tom Sepezae51c812015-08-05 12:34:06 -070046 pInfoDict->SetAt("Creator", new CPDF_String(L"PDFium"));
Nico Weber9d8ec5a2015-08-04 13:00:21 -070047 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070048
Tom Sepezbf59a072015-10-21 14:07:23 -070049 return FPDFDocumentFromCPDFDocument(pDoc);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070050}
51
Nico Weber9d8ec5a2015-08-04 13:00:21 -070052DLLEXPORT void STDCALL FPDFPage_Delete(FPDF_DOCUMENT document, int page_index) {
Tom Sepez471a1032015-10-15 16:17:18 -070053 CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document);
54 if (!pDoc || page_index < 0 || page_index >= pDoc->GetPageCount())
Nico Weber9d8ec5a2015-08-04 13:00:21 -070055 return;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070056
Nico Weber9d8ec5a2015-08-04 13:00:21 -070057 pDoc->DeletePage(page_index);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070058}
59
Nico Weber9d8ec5a2015-08-04 13:00:21 -070060DLLEXPORT FPDF_PAGE STDCALL FPDFPage_New(FPDF_DOCUMENT document,
61 int page_index,
62 double width,
63 double height) {
Tom Sepez471a1032015-10-15 16:17:18 -070064 CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document);
65 if (!pDoc)
66 return nullptr;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070067
Nico Weber9d8ec5a2015-08-04 13:00:21 -070068 if (page_index < 0)
69 page_index = 0;
70 if (pDoc->GetPageCount() < page_index)
71 page_index = pDoc->GetPageCount();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070072
Nico Weber9d8ec5a2015-08-04 13:00:21 -070073 CPDF_Dictionary* pPageDict = pDoc->CreateNewPage(page_index);
74 if (!pPageDict)
75 return NULL;
Tom Sepezae51c812015-08-05 12:34:06 -070076 CPDF_Array* pMediaBoxArray = new CPDF_Array;
77 pMediaBoxArray->Add(new CPDF_Number(0));
78 pMediaBoxArray->Add(new CPDF_Number(0));
79 pMediaBoxArray->Add(new CPDF_Number(FX_FLOAT(width)));
80 pMediaBoxArray->Add(new CPDF_Number(FX_FLOAT(height)));
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070081
Nico Weber9d8ec5a2015-08-04 13:00:21 -070082 pPageDict->SetAt("MediaBox", pMediaBoxArray);
Tom Sepezae51c812015-08-05 12:34:06 -070083 pPageDict->SetAt("Rotate", new CPDF_Number(0));
84 pPageDict->SetAt("Resources", new CPDF_Dictionary);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070085
Tom Sepez51da0932015-11-25 16:05:49 -080086#ifndef PDF_ENABLE_XFA
87 CPDF_Page* pPage = new CPDF_Page;
88 pPage->Load(pDoc, pPageDict);
89 pPage->ParseContent();
90#else
Nico Weber9d8ec5a2015-08-04 13:00:21 -070091 CPDFXFA_Page* pPage =
Tom Sepezae51c812015-08-05 12:34:06 -070092 new CPDFXFA_Page((CPDFXFA_Document*)document, page_index);
Nico Weber9d8ec5a2015-08-04 13:00:21 -070093 pPage->LoadPDFPage(pPageDict);
Tom Sepez51da0932015-11-25 16:05:49 -080094#endif
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070095
Nico Weber9d8ec5a2015-08-04 13:00:21 -070096 return pPage;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070097}
98
Nico Weber9d8ec5a2015-08-04 13:00:21 -070099DLLEXPORT int STDCALL FPDFPage_GetRotation(FPDF_PAGE page) {
Tom Sepezdb0be962015-10-16 14:00:21 -0700100 CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700101 if (!pPage || !pPage->m_pFormDict || !pPage->m_pFormDict->KeyExist("Type") ||
102 !pPage->m_pFormDict->GetElement("Type")->GetDirect() ||
103 pPage->m_pFormDict->GetElement("Type")->GetDirect()->GetString().Compare(
104 "Page")) {
105 return -1;
106 }
107 CPDF_Dictionary* pDict = pPage->m_pFormDict;
Lei Zhang997de612015-11-04 18:17:53 -0800108 if (!pDict)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700109 return -1;
Lei Zhang997de612015-11-04 18:17:53 -0800110
111 while (pDict) {
112 if (pDict->KeyExist("Rotate")) {
113 CPDF_Object* pRotateObj = pDict->GetElement("Rotate")->GetDirect();
114 return pRotateObj ? pRotateObj->GetInteger() / 90 : 0;
115 }
116 if (!pDict->KeyExist("Parent"))
117 break;
118
119 pDict = ToDictionary(pDict->GetElement("Parent")->GetDirect());
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700120 }
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700121
Lei Zhang997de612015-11-04 18:17:53 -0800122 return 0;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700123}
124
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700125DLLEXPORT void STDCALL FPDFPage_InsertObject(FPDF_PAGE page,
126 FPDF_PAGEOBJECT page_obj) {
Tom Sepezdb0be962015-10-16 14:00:21 -0700127 CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700128 if (!pPage || !pPage->m_pFormDict || !pPage->m_pFormDict->KeyExist("Type") ||
129 !pPage->m_pFormDict->GetElement("Type")->GetDirect() ||
130 pPage->m_pFormDict->GetElement("Type")->GetDirect()->GetString().Compare(
131 "Page")) {
132 return;
133 }
134 CPDF_PageObject* pPageObj = (CPDF_PageObject*)page_obj;
Lei Zhang997de612015-11-04 18:17:53 -0800135 if (!pPageObj)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700136 return;
137 FX_POSITION LastPersition = pPage->GetLastObjectPosition();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700138
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700139 pPage->InsertObject(LastPersition, pPageObj);
140 switch (pPageObj->m_Type) {
141 case FPDF_PAGEOBJ_PATH: {
142 CPDF_PathObject* pPathObj = (CPDF_PathObject*)pPageObj;
143 pPathObj->CalcBoundingBox();
144 break;
145 }
146 case FPDF_PAGEOBJ_TEXT: {
147 // CPDF_PathObject* pPathObj = (CPDF_PathObject*)pPageObj;
148 // pPathObj->CalcBoundingBox();
149 break;
150 }
151 case FPDF_PAGEOBJ_IMAGE: {
152 CPDF_ImageObject* pImageObj = (CPDF_ImageObject*)pPageObj;
153 pImageObj->CalcBoundingBox();
154 break;
155 }
156 case FPDF_PAGEOBJ_SHADING: {
157 CPDF_ShadingObject* pShadingObj = (CPDF_ShadingObject*)pPageObj;
158 pShadingObj->CalcBoundingBox();
159 break;
160 }
161 case FPDF_PAGEOBJ_FORM: {
162 CPDF_FormObject* pFormObj = (CPDF_FormObject*)pPageObj;
163 pFormObj->CalcBoundingBox();
164 break;
165 }
166 default:
167 break;
168 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700169}
170
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700171DLLEXPORT int STDCALL FPDFPage_CountObject(FPDF_PAGE page) {
Tom Sepezbf59a072015-10-21 14:07:23 -0700172 CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700173 if (!pPage || !pPage->m_pFormDict || !pPage->m_pFormDict->KeyExist("Type") ||
174 !pPage->m_pFormDict->GetElement("Type")->GetDirect() ||
175 pPage->m_pFormDict->GetElement("Type")->GetDirect()->GetString().Compare(
176 "Page")) {
177 return -1;
178 }
179 return pPage->CountObjects();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700180}
181
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700182DLLEXPORT FPDF_PAGEOBJECT STDCALL FPDFPage_GetObject(FPDF_PAGE page,
183 int index) {
Tom Sepezdb0be962015-10-16 14:00:21 -0700184 CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700185 if (!pPage || !pPage->m_pFormDict || !pPage->m_pFormDict->KeyExist("Type") ||
186 pPage->m_pFormDict->GetElement("Type")->GetDirect()->GetString().Compare(
187 "Page")) {
188 return NULL;
189 }
190 return pPage->GetObjectByIndex(index);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700191}
192
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700193DLLEXPORT FPDF_BOOL STDCALL FPDFPage_HasTransparency(FPDF_PAGE page) {
Tom Sepezdb0be962015-10-16 14:00:21 -0700194 CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
195 return pPage && pPage->BackgroundAlphaNeeded();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700196}
197
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700198DLLEXPORT FPDF_BOOL STDCALL
199FPDFPageObj_HasTransparency(FPDF_PAGEOBJECT pageObject) {
200 if (!pageObject)
201 return FALSE;
202 CPDF_PageObject* pPageObj = (CPDF_PageObject*)pageObject;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700203
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700204 const CPDF_GeneralStateData* pGeneralState = pPageObj->m_GeneralState;
205 int blend_type =
206 pGeneralState ? pGeneralState->m_BlendType : FXDIB_BLEND_NORMAL;
207 if (blend_type != FXDIB_BLEND_NORMAL)
208 return TRUE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700209
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700210 CPDF_Dictionary* pSMaskDict =
Dan Sinclairf1251c12015-10-20 16:24:45 -0400211 pGeneralState ? ToDictionary(pGeneralState->m_pSoftMask) : NULL;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700212 if (pSMaskDict)
213 return TRUE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700214
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700215 if (pGeneralState && pGeneralState->m_FillAlpha != 1.0f)
216 return TRUE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700217
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700218 if (pPageObj->m_Type == PDFPAGE_PATH) {
219 if (pGeneralState && pGeneralState->m_StrokeAlpha != 1.0f)
220 return TRUE;
221 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700222
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700223 if (pPageObj->m_Type == PDFPAGE_FORM) {
224 CPDF_FormObject* pFormObj = (CPDF_FormObject*)pPageObj;
225 if (pFormObj->m_pForm &&
226 (pFormObj->m_pForm->m_Transparency & PDFTRANS_ISOLATED))
227 return TRUE;
228 if (pFormObj->m_pForm &&
229 (!(pFormObj->m_pForm->m_Transparency & PDFTRANS_ISOLATED) &&
230 (pFormObj->m_pForm->m_Transparency & PDFTRANS_GROUP)))
231 return TRUE;
232 }
233 return FALSE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700234}
235
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700236DLLEXPORT FPDF_BOOL STDCALL FPDFPage_GenerateContent(FPDF_PAGE page) {
Tom Sepezdb0be962015-10-16 14:00:21 -0700237 CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700238 if (!pPage || !pPage->m_pFormDict || !pPage->m_pFormDict->KeyExist("Type") ||
239 !pPage->m_pFormDict->GetElement("Type")->GetDirect() ||
240 pPage->m_pFormDict->GetElement("Type")->GetDirect()->GetString().Compare(
241 "Page")) {
242 return FALSE;
243 }
244 CPDF_PageContentGenerate CG(pPage);
245 CG.GenerateContent();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700246
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700247 return TRUE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700248}
249
250DLLEXPORT void STDCALL FPDFPageObj_Transform(FPDF_PAGEOBJECT page_object,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700251 double a,
252 double b,
253 double c,
254 double d,
255 double e,
256 double f) {
257 CPDF_PageObject* pPageObj = (CPDF_PageObject*)page_object;
Lei Zhang997de612015-11-04 18:17:53 -0800258 if (!pPageObj)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700259 return;
Lei Zhangcb78ef52015-10-02 10:10:49 -0700260
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700261 CFX_AffineMatrix matrix((FX_FLOAT)a, (FX_FLOAT)b, (FX_FLOAT)c, (FX_FLOAT)d,
262 (FX_FLOAT)e, (FX_FLOAT)f);
263 pPageObj->Transform(matrix);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700264}
265DLLEXPORT void STDCALL FPDFPage_TransformAnnots(FPDF_PAGE page,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700266 double a,
267 double b,
268 double c,
269 double d,
270 double e,
271 double f) {
Tom Sepezdb0be962015-10-16 14:00:21 -0700272 CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700273 if (!pPage)
274 return;
275 CPDF_AnnotList AnnotList(pPage);
Lei Zhang1b700c32015-10-30 23:55:35 -0700276 for (size_t i = 0; i < AnnotList.Count(); ++i) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700277 CPDF_Annot* pAnnot = AnnotList.GetAt(i);
278 // transformAnnots Rectangle
279 CPDF_Rect rect;
280 pAnnot->GetRect(rect);
281 CFX_AffineMatrix matrix((FX_FLOAT)a, (FX_FLOAT)b, (FX_FLOAT)c, (FX_FLOAT)d,
282 (FX_FLOAT)e, (FX_FLOAT)f);
283 rect.Transform(&matrix);
284 CPDF_Array* pRectArray = NULL;
285 pRectArray = pAnnot->GetAnnotDict()->GetArray("Rect");
286 if (!pRectArray)
287 pRectArray = CPDF_Array::Create();
288 pRectArray->SetAt(0, new CPDF_Number(rect.left));
289 pRectArray->SetAt(1, new CPDF_Number(rect.bottom));
290 pRectArray->SetAt(2, new CPDF_Number(rect.right));
291 pRectArray->SetAt(3, new CPDF_Number(rect.top));
292 pAnnot->GetAnnotDict()->SetAt("Rect", pRectArray);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700293
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700294 // Transform AP's rectangle
295 // To Do
296 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700297}
Bo Xu394010d2014-06-12 13:41:50 -0700298
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700299DLLEXPORT void STDCALL FPDFPage_SetRotation(FPDF_PAGE page, int rotate) {
Tom Sepezdb0be962015-10-16 14:00:21 -0700300 CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700301 if (!pPage || !pPage->m_pFormDict || !pPage->m_pFormDict->KeyExist("Type") ||
302 !pPage->m_pFormDict->GetElement("Type")->GetDirect() ||
303 pPage->m_pFormDict->GetElement("Type")->GetDirect()->GetString().Compare(
304 "Page")) {
305 return;
306 }
307 CPDF_Dictionary* pDict = pPage->m_pFormDict;
308 rotate %= 4;
Bo Xu394010d2014-06-12 13:41:50 -0700309
Tom Sepezae51c812015-08-05 12:34:06 -0700310 pDict->SetAt("Rotate", new CPDF_Number(rotate * 90));
Nico Weber0ce77e32014-07-16 13:19:08 -0700311}