blob: aff74d160136c73311afdf3b5ed0db9e9e80a7c6 [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 Sepez40e9ff32015-11-30 12:39:54 -08009#include "fpdfsdk/include/fsdk_define.h"
10#include "public/fpdf_formfill.h"
11
Tom Sepez51da0932015-11-25 16:05:49 -080012#ifdef PDF_ENABLE_XFA
Bo Xufdc00a72014-10-28 23:03:33 -070013#include "../include/fpdfxfa/fpdfxfa_app.h"
Lei Zhangbde53d22015-11-12 22:21:30 -080014#include "../include/fpdfxfa/fpdfxfa_doc.h"
Bo Xufdc00a72014-10-28 23:03:33 -070015#include "../include/fpdfxfa/fpdfxfa_page.h"
Tom Sepez40e9ff32015-11-30 12:39:54 -080016#endif // PDF_ENABLE_XFA
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070017
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070018#if _FX_OS_ == _FX_ANDROID_
19#include "time.h"
20#else
21#include <ctime>
22#endif
23
Nico Weber9d8ec5a2015-08-04 13:00:21 -070024DLLEXPORT FPDF_DOCUMENT STDCALL FPDF_CreateNewDocument() {
Tom Sepezae51c812015-08-05 12:34:06 -070025 CPDF_Document* pDoc = new CPDF_Document;
Nico Weber9d8ec5a2015-08-04 13:00:21 -070026 pDoc->CreateNewDoc();
27 time_t currentTime;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070028
Nico Weber9d8ec5a2015-08-04 13:00:21 -070029 CFX_ByteString DateStr;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070030
Nico Weber9d8ec5a2015-08-04 13:00:21 -070031 if (FSDK_IsSandBoxPolicyEnabled(FPDF_POLICY_MACHINETIME_ACCESS)) {
32 if (-1 != time(&currentTime)) {
33 tm* pTM = localtime(&currentTime);
34 if (pTM) {
35 DateStr.Format("D:%04d%02d%02d%02d%02d%02d", pTM->tm_year + 1900,
36 pTM->tm_mon + 1, pTM->tm_mday, pTM->tm_hour, pTM->tm_min,
37 pTM->tm_sec);
38 }
39 }
40 }
Tom Sepezbdeeb8a2015-05-27 12:25:00 -070041
Nico Weber9d8ec5a2015-08-04 13:00:21 -070042 CPDF_Dictionary* pInfoDict = NULL;
43 pInfoDict = pDoc->GetInfo();
44 if (pInfoDict) {
45 if (FSDK_IsSandBoxPolicyEnabled(FPDF_POLICY_MACHINETIME_ACCESS))
46 pInfoDict->SetAt("CreationDate", new CPDF_String(DateStr));
Tom Sepezae51c812015-08-05 12:34:06 -070047 pInfoDict->SetAt("Creator", new CPDF_String(L"PDFium"));
Nico Weber9d8ec5a2015-08-04 13:00:21 -070048 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070049
Tom Sepezbf59a072015-10-21 14:07:23 -070050 return FPDFDocumentFromCPDFDocument(pDoc);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070051}
52
Nico Weber9d8ec5a2015-08-04 13:00:21 -070053DLLEXPORT void STDCALL FPDFPage_Delete(FPDF_DOCUMENT document, int page_index) {
Tom Sepez471a1032015-10-15 16:17:18 -070054 CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document);
55 if (!pDoc || page_index < 0 || page_index >= pDoc->GetPageCount())
Nico Weber9d8ec5a2015-08-04 13:00:21 -070056 return;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070057
Nico Weber9d8ec5a2015-08-04 13:00:21 -070058 pDoc->DeletePage(page_index);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070059}
60
Nico Weber9d8ec5a2015-08-04 13:00:21 -070061DLLEXPORT FPDF_PAGE STDCALL FPDFPage_New(FPDF_DOCUMENT document,
62 int page_index,
63 double width,
64 double height) {
Tom Sepez471a1032015-10-15 16:17:18 -070065 CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document);
66 if (!pDoc)
67 return nullptr;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070068
Nico Weber9d8ec5a2015-08-04 13:00:21 -070069 if (page_index < 0)
70 page_index = 0;
71 if (pDoc->GetPageCount() < page_index)
72 page_index = pDoc->GetPageCount();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070073
Nico Weber9d8ec5a2015-08-04 13:00:21 -070074 CPDF_Dictionary* pPageDict = pDoc->CreateNewPage(page_index);
75 if (!pPageDict)
76 return NULL;
Tom Sepezae51c812015-08-05 12:34:06 -070077 CPDF_Array* pMediaBoxArray = new CPDF_Array;
78 pMediaBoxArray->Add(new CPDF_Number(0));
79 pMediaBoxArray->Add(new CPDF_Number(0));
80 pMediaBoxArray->Add(new CPDF_Number(FX_FLOAT(width)));
81 pMediaBoxArray->Add(new CPDF_Number(FX_FLOAT(height)));
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070082
Nico Weber9d8ec5a2015-08-04 13:00:21 -070083 pPageDict->SetAt("MediaBox", pMediaBoxArray);
Tom Sepezae51c812015-08-05 12:34:06 -070084 pPageDict->SetAt("Rotate", new CPDF_Number(0));
85 pPageDict->SetAt("Resources", new CPDF_Dictionary);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070086
Tom Sepez40e9ff32015-11-30 12:39:54 -080087#ifdef PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -070088 CPDFXFA_Page* pPage =
Tom Sepezae51c812015-08-05 12:34:06 -070089 new CPDFXFA_Page((CPDFXFA_Document*)document, page_index);
Nico Weber9d8ec5a2015-08-04 13:00:21 -070090 pPage->LoadPDFPage(pPageDict);
Tom Sepez40e9ff32015-11-30 12:39:54 -080091#else // PDF_ENABLE_XFA
92 CPDF_Page* pPage = new CPDF_Page;
93 pPage->Load(pDoc, pPageDict);
94 pPage->ParseContent();
95#endif // PDF_ENABLE_XFA
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070096
Nico Weber9d8ec5a2015-08-04 13:00:21 -070097 return pPage;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070098}
99
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700100DLLEXPORT int STDCALL FPDFPage_GetRotation(FPDF_PAGE page) {
Tom Sepezdb0be962015-10-16 14:00:21 -0700101 CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700102 if (!pPage || !pPage->m_pFormDict || !pPage->m_pFormDict->KeyExist("Type") ||
103 !pPage->m_pFormDict->GetElement("Type")->GetDirect() ||
104 pPage->m_pFormDict->GetElement("Type")->GetDirect()->GetString().Compare(
105 "Page")) {
106 return -1;
107 }
108 CPDF_Dictionary* pDict = pPage->m_pFormDict;
Lei Zhang997de612015-11-04 18:17:53 -0800109 if (!pDict)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700110 return -1;
Lei Zhang997de612015-11-04 18:17:53 -0800111
112 while (pDict) {
113 if (pDict->KeyExist("Rotate")) {
114 CPDF_Object* pRotateObj = pDict->GetElement("Rotate")->GetDirect();
115 return pRotateObj ? pRotateObj->GetInteger() / 90 : 0;
116 }
117 if (!pDict->KeyExist("Parent"))
118 break;
119
120 pDict = ToDictionary(pDict->GetElement("Parent")->GetDirect());
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700121 }
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700122
Lei Zhang997de612015-11-04 18:17:53 -0800123 return 0;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700124}
125
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700126DLLEXPORT void STDCALL FPDFPage_InsertObject(FPDF_PAGE page,
127 FPDF_PAGEOBJECT page_obj) {
Tom Sepezdb0be962015-10-16 14:00:21 -0700128 CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700129 if (!pPage || !pPage->m_pFormDict || !pPage->m_pFormDict->KeyExist("Type") ||
130 !pPage->m_pFormDict->GetElement("Type")->GetDirect() ||
131 pPage->m_pFormDict->GetElement("Type")->GetDirect()->GetString().Compare(
132 "Page")) {
133 return;
134 }
135 CPDF_PageObject* pPageObj = (CPDF_PageObject*)page_obj;
Lei Zhang997de612015-11-04 18:17:53 -0800136 if (!pPageObj)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700137 return;
138 FX_POSITION LastPersition = pPage->GetLastObjectPosition();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700139
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700140 pPage->InsertObject(LastPersition, pPageObj);
141 switch (pPageObj->m_Type) {
142 case FPDF_PAGEOBJ_PATH: {
143 CPDF_PathObject* pPathObj = (CPDF_PathObject*)pPageObj;
144 pPathObj->CalcBoundingBox();
145 break;
146 }
147 case FPDF_PAGEOBJ_TEXT: {
148 // CPDF_PathObject* pPathObj = (CPDF_PathObject*)pPageObj;
149 // pPathObj->CalcBoundingBox();
150 break;
151 }
152 case FPDF_PAGEOBJ_IMAGE: {
153 CPDF_ImageObject* pImageObj = (CPDF_ImageObject*)pPageObj;
154 pImageObj->CalcBoundingBox();
155 break;
156 }
157 case FPDF_PAGEOBJ_SHADING: {
158 CPDF_ShadingObject* pShadingObj = (CPDF_ShadingObject*)pPageObj;
159 pShadingObj->CalcBoundingBox();
160 break;
161 }
162 case FPDF_PAGEOBJ_FORM: {
163 CPDF_FormObject* pFormObj = (CPDF_FormObject*)pPageObj;
164 pFormObj->CalcBoundingBox();
165 break;
166 }
167 default:
168 break;
169 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700170}
171
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700172DLLEXPORT int STDCALL FPDFPage_CountObject(FPDF_PAGE page) {
Tom Sepezbf59a072015-10-21 14:07:23 -0700173 CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700174 if (!pPage || !pPage->m_pFormDict || !pPage->m_pFormDict->KeyExist("Type") ||
175 !pPage->m_pFormDict->GetElement("Type")->GetDirect() ||
176 pPage->m_pFormDict->GetElement("Type")->GetDirect()->GetString().Compare(
177 "Page")) {
178 return -1;
179 }
180 return pPage->CountObjects();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700181}
182
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700183DLLEXPORT FPDF_PAGEOBJECT STDCALL FPDFPage_GetObject(FPDF_PAGE page,
184 int index) {
Tom Sepezdb0be962015-10-16 14:00:21 -0700185 CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700186 if (!pPage || !pPage->m_pFormDict || !pPage->m_pFormDict->KeyExist("Type") ||
187 pPage->m_pFormDict->GetElement("Type")->GetDirect()->GetString().Compare(
188 "Page")) {
189 return NULL;
190 }
191 return pPage->GetObjectByIndex(index);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700192}
193
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700194DLLEXPORT FPDF_BOOL STDCALL FPDFPage_HasTransparency(FPDF_PAGE page) {
Tom Sepezdb0be962015-10-16 14:00:21 -0700195 CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
196 return pPage && pPage->BackgroundAlphaNeeded();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700197}
198
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700199DLLEXPORT FPDF_BOOL STDCALL
200FPDFPageObj_HasTransparency(FPDF_PAGEOBJECT pageObject) {
201 if (!pageObject)
202 return FALSE;
203 CPDF_PageObject* pPageObj = (CPDF_PageObject*)pageObject;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700204
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700205 const CPDF_GeneralStateData* pGeneralState = pPageObj->m_GeneralState;
206 int blend_type =
207 pGeneralState ? pGeneralState->m_BlendType : FXDIB_BLEND_NORMAL;
208 if (blend_type != FXDIB_BLEND_NORMAL)
209 return TRUE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700210
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700211 CPDF_Dictionary* pSMaskDict =
Dan Sinclairf1251c12015-10-20 16:24:45 -0400212 pGeneralState ? ToDictionary(pGeneralState->m_pSoftMask) : NULL;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700213 if (pSMaskDict)
214 return TRUE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700215
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700216 if (pGeneralState && pGeneralState->m_FillAlpha != 1.0f)
217 return TRUE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700218
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700219 if (pPageObj->m_Type == PDFPAGE_PATH) {
220 if (pGeneralState && pGeneralState->m_StrokeAlpha != 1.0f)
221 return TRUE;
222 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700223
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700224 if (pPageObj->m_Type == PDFPAGE_FORM) {
225 CPDF_FormObject* pFormObj = (CPDF_FormObject*)pPageObj;
226 if (pFormObj->m_pForm &&
227 (pFormObj->m_pForm->m_Transparency & PDFTRANS_ISOLATED))
228 return TRUE;
229 if (pFormObj->m_pForm &&
230 (!(pFormObj->m_pForm->m_Transparency & PDFTRANS_ISOLATED) &&
231 (pFormObj->m_pForm->m_Transparency & PDFTRANS_GROUP)))
232 return TRUE;
233 }
234 return FALSE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700235}
236
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700237DLLEXPORT FPDF_BOOL STDCALL FPDFPage_GenerateContent(FPDF_PAGE page) {
Tom Sepezdb0be962015-10-16 14:00:21 -0700238 CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700239 if (!pPage || !pPage->m_pFormDict || !pPage->m_pFormDict->KeyExist("Type") ||
240 !pPage->m_pFormDict->GetElement("Type")->GetDirect() ||
241 pPage->m_pFormDict->GetElement("Type")->GetDirect()->GetString().Compare(
242 "Page")) {
243 return FALSE;
244 }
245 CPDF_PageContentGenerate CG(pPage);
246 CG.GenerateContent();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700247
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700248 return TRUE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700249}
250
251DLLEXPORT void STDCALL FPDFPageObj_Transform(FPDF_PAGEOBJECT page_object,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700252 double a,
253 double b,
254 double c,
255 double d,
256 double e,
257 double f) {
258 CPDF_PageObject* pPageObj = (CPDF_PageObject*)page_object;
Lei Zhang997de612015-11-04 18:17:53 -0800259 if (!pPageObj)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700260 return;
Lei Zhangcb78ef52015-10-02 10:10:49 -0700261
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700262 CFX_AffineMatrix matrix((FX_FLOAT)a, (FX_FLOAT)b, (FX_FLOAT)c, (FX_FLOAT)d,
263 (FX_FLOAT)e, (FX_FLOAT)f);
264 pPageObj->Transform(matrix);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700265}
266DLLEXPORT void STDCALL FPDFPage_TransformAnnots(FPDF_PAGE page,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700267 double a,
268 double b,
269 double c,
270 double d,
271 double e,
272 double f) {
Tom Sepezdb0be962015-10-16 14:00:21 -0700273 CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700274 if (!pPage)
275 return;
276 CPDF_AnnotList AnnotList(pPage);
Lei Zhang1b700c32015-10-30 23:55:35 -0700277 for (size_t i = 0; i < AnnotList.Count(); ++i) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700278 CPDF_Annot* pAnnot = AnnotList.GetAt(i);
279 // transformAnnots Rectangle
280 CPDF_Rect rect;
281 pAnnot->GetRect(rect);
282 CFX_AffineMatrix matrix((FX_FLOAT)a, (FX_FLOAT)b, (FX_FLOAT)c, (FX_FLOAT)d,
283 (FX_FLOAT)e, (FX_FLOAT)f);
284 rect.Transform(&matrix);
285 CPDF_Array* pRectArray = NULL;
286 pRectArray = pAnnot->GetAnnotDict()->GetArray("Rect");
287 if (!pRectArray)
288 pRectArray = CPDF_Array::Create();
289 pRectArray->SetAt(0, new CPDF_Number(rect.left));
290 pRectArray->SetAt(1, new CPDF_Number(rect.bottom));
291 pRectArray->SetAt(2, new CPDF_Number(rect.right));
292 pRectArray->SetAt(3, new CPDF_Number(rect.top));
293 pAnnot->GetAnnotDict()->SetAt("Rect", pRectArray);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700294
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700295 // Transform AP's rectangle
296 // To Do
297 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700298}
Bo Xu394010d2014-06-12 13:41:50 -0700299
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700300DLLEXPORT void STDCALL FPDFPage_SetRotation(FPDF_PAGE page, int rotate) {
Tom Sepezdb0be962015-10-16 14:00:21 -0700301 CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700302 if (!pPage || !pPage->m_pFormDict || !pPage->m_pFormDict->KeyExist("Type") ||
303 !pPage->m_pFormDict->GetElement("Type")->GetDirect() ||
304 pPage->m_pFormDict->GetElement("Type")->GetDirect()->GetString().Compare(
305 "Page")) {
306 return;
307 }
308 CPDF_Dictionary* pDict = pPage->m_pFormDict;
309 rotate %= 4;
Bo Xu394010d2014-06-12 13:41:50 -0700310
Tom Sepezae51c812015-08-05 12:34:06 -0700311 pDict->SetAt("Rotate", new CPDF_Number(rotate * 90));
Nico Weber0ce77e32014-07-16 13:19:08 -0700312}