blob: 493329c3604b27edfb40522f33fee40a6a647113 [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
Dan Sinclairaa403d32016-03-15 14:57:22 -04009#include "core/fpdfapi/fpdf_parser/include/cpdf_array.h"
10#include "core/fpdfapi/fpdf_parser/include/cpdf_document.h"
11#include "core/fpdfapi/fpdf_parser/include/cpdf_number.h"
12#include "core/fpdfapi/fpdf_parser/include/cpdf_string.h"
13#include "core/include/fpdfapi/fpdf_page.h"
Tom Sepez40e9ff32015-11-30 12:39:54 -080014#include "fpdfsdk/include/fsdk_define.h"
15#include "public/fpdf_formfill.h"
Tom Sepez2398d892016-02-17 16:46:26 -080016#include "third_party/base/stl_util.h"
Tom Sepez40e9ff32015-11-30 12:39:54 -080017
Tom Sepez51da0932015-11-25 16:05:49 -080018#ifdef PDF_ENABLE_XFA
Lei Zhang875b9c92016-01-08 13:51:10 -080019#include "fpdfsdk/include/fpdfxfa/fpdfxfa_app.h"
20#include "fpdfsdk/include/fpdfxfa/fpdfxfa_doc.h"
21#include "fpdfsdk/include/fpdfxfa/fpdfxfa_page.h"
Tom Sepez40e9ff32015-11-30 12:39:54 -080022#endif // PDF_ENABLE_XFA
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070023
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070024#if _FX_OS_ == _FX_ANDROID_
25#include "time.h"
26#else
27#include <ctime>
28#endif
29
Nico Weber9d8ec5a2015-08-04 13:00:21 -070030DLLEXPORT FPDF_DOCUMENT STDCALL FPDF_CreateNewDocument() {
Tom Sepezae51c812015-08-05 12:34:06 -070031 CPDF_Document* pDoc = new CPDF_Document;
Nico Weber9d8ec5a2015-08-04 13:00:21 -070032 pDoc->CreateNewDoc();
33 time_t currentTime;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070034
Nico Weber9d8ec5a2015-08-04 13:00:21 -070035 CFX_ByteString DateStr;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070036
Nico Weber9d8ec5a2015-08-04 13:00:21 -070037 if (FSDK_IsSandBoxPolicyEnabled(FPDF_POLICY_MACHINETIME_ACCESS)) {
38 if (-1 != time(&currentTime)) {
39 tm* pTM = localtime(&currentTime);
40 if (pTM) {
41 DateStr.Format("D:%04d%02d%02d%02d%02d%02d", pTM->tm_year + 1900,
42 pTM->tm_mon + 1, pTM->tm_mday, pTM->tm_hour, pTM->tm_min,
43 pTM->tm_sec);
44 }
45 }
46 }
Tom Sepezbdeeb8a2015-05-27 12:25:00 -070047
Nico Weber9d8ec5a2015-08-04 13:00:21 -070048 CPDF_Dictionary* pInfoDict = NULL;
49 pInfoDict = pDoc->GetInfo();
50 if (pInfoDict) {
51 if (FSDK_IsSandBoxPolicyEnabled(FPDF_POLICY_MACHINETIME_ACCESS))
Lei Zhang4880d1a2015-12-18 17:05:11 -080052 pInfoDict->SetAt("CreationDate", new CPDF_String(DateStr, FALSE));
Tom Sepezae51c812015-08-05 12:34:06 -070053 pInfoDict->SetAt("Creator", new CPDF_String(L"PDFium"));
Nico Weber9d8ec5a2015-08-04 13:00:21 -070054 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070055
Tom Sepezbf59a072015-10-21 14:07:23 -070056 return FPDFDocumentFromCPDFDocument(pDoc);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070057}
58
Nico Weber9d8ec5a2015-08-04 13:00:21 -070059DLLEXPORT void STDCALL FPDFPage_Delete(FPDF_DOCUMENT document, int page_index) {
Tom Sepez744da702016-03-15 12:43:09 -070060 if (UnderlyingDocumentType* pDoc = UnderlyingFromFPDFDocument(document))
61 pDoc->DeletePage(page_index);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070062}
63
Nico Weber9d8ec5a2015-08-04 13:00:21 -070064DLLEXPORT FPDF_PAGE STDCALL FPDFPage_New(FPDF_DOCUMENT document,
65 int page_index,
66 double width,
67 double height) {
Tom Sepez471a1032015-10-15 16:17:18 -070068 CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document);
69 if (!pDoc)
70 return nullptr;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070071
Nico Weber9d8ec5a2015-08-04 13:00:21 -070072 if (page_index < 0)
73 page_index = 0;
74 if (pDoc->GetPageCount() < page_index)
75 page_index = pDoc->GetPageCount();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070076
Nico Weber9d8ec5a2015-08-04 13:00:21 -070077 CPDF_Dictionary* pPageDict = pDoc->CreateNewPage(page_index);
78 if (!pPageDict)
79 return NULL;
Tom Sepezae51c812015-08-05 12:34:06 -070080 CPDF_Array* pMediaBoxArray = new CPDF_Array;
81 pMediaBoxArray->Add(new CPDF_Number(0));
82 pMediaBoxArray->Add(new CPDF_Number(0));
83 pMediaBoxArray->Add(new CPDF_Number(FX_FLOAT(width)));
84 pMediaBoxArray->Add(new CPDF_Number(FX_FLOAT(height)));
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070085
Nico Weber9d8ec5a2015-08-04 13:00:21 -070086 pPageDict->SetAt("MediaBox", pMediaBoxArray);
Tom Sepezae51c812015-08-05 12:34:06 -070087 pPageDict->SetAt("Rotate", new CPDF_Number(0));
88 pPageDict->SetAt("Resources", new CPDF_Dictionary);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070089
Tom Sepez40e9ff32015-11-30 12:39:54 -080090#ifdef PDF_ENABLE_XFA
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 Sepez40e9ff32015-11-30 12:39:54 -080094#else // PDF_ENABLE_XFA
95 CPDF_Page* pPage = new CPDF_Page;
96 pPage->Load(pDoc, pPageDict);
Tom Sepezb5b2a912016-01-21 11:04:37 -080097 pPage->ParseContent(nullptr);
Tom Sepez40e9ff32015-11-30 12:39:54 -080098#endif // PDF_ENABLE_XFA
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070099
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700100 return pPage;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700101}
102
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700103DLLEXPORT int STDCALL FPDFPage_GetRotation(FPDF_PAGE page) {
Tom Sepezdb0be962015-10-16 14:00:21 -0700104 CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700105 if (!pPage || !pPage->m_pFormDict || !pPage->m_pFormDict->KeyExist("Type") ||
106 !pPage->m_pFormDict->GetElement("Type")->GetDirect() ||
107 pPage->m_pFormDict->GetElement("Type")->GetDirect()->GetString().Compare(
108 "Page")) {
109 return -1;
110 }
111 CPDF_Dictionary* pDict = pPage->m_pFormDict;
Lei Zhang997de612015-11-04 18:17:53 -0800112 if (!pDict)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700113 return -1;
Lei Zhang997de612015-11-04 18:17:53 -0800114
115 while (pDict) {
116 if (pDict->KeyExist("Rotate")) {
117 CPDF_Object* pRotateObj = pDict->GetElement("Rotate")->GetDirect();
118 return pRotateObj ? pRotateObj->GetInteger() / 90 : 0;
119 }
120 if (!pDict->KeyExist("Parent"))
121 break;
122
123 pDict = ToDictionary(pDict->GetElement("Parent")->GetDirect());
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700124 }
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700125
Lei Zhang997de612015-11-04 18:17:53 -0800126 return 0;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700127}
128
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700129DLLEXPORT void STDCALL FPDFPage_InsertObject(FPDF_PAGE page,
130 FPDF_PAGEOBJECT page_obj) {
Tom Sepezdb0be962015-10-16 14:00:21 -0700131 CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700132 if (!pPage || !pPage->m_pFormDict || !pPage->m_pFormDict->KeyExist("Type") ||
133 !pPage->m_pFormDict->GetElement("Type")->GetDirect() ||
134 pPage->m_pFormDict->GetElement("Type")->GetDirect()->GetString().Compare(
135 "Page")) {
136 return;
137 }
138 CPDF_PageObject* pPageObj = (CPDF_PageObject*)page_obj;
Lei Zhang997de612015-11-04 18:17:53 -0800139 if (!pPageObj)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700140 return;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700141
Tom Sepez2398d892016-02-17 16:46:26 -0800142 pPage->GetPageObjectList()->push_back(
143 std::unique_ptr<CPDF_PageObject>(pPageObj));
144
Wei Li7cf13c92016-02-19 11:53:03 -0800145 switch (pPageObj->GetType()) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700146 case FPDF_PAGEOBJ_PATH: {
Wei Li7cf13c92016-02-19 11:53:03 -0800147 CPDF_PathObject* pPathObj = pPageObj->AsPath();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700148 pPathObj->CalcBoundingBox();
149 break;
150 }
151 case FPDF_PAGEOBJ_TEXT: {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700152 break;
153 }
154 case FPDF_PAGEOBJ_IMAGE: {
Wei Li7cf13c92016-02-19 11:53:03 -0800155 CPDF_ImageObject* pImageObj = pPageObj->AsImage();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700156 pImageObj->CalcBoundingBox();
157 break;
158 }
159 case FPDF_PAGEOBJ_SHADING: {
Wei Li7cf13c92016-02-19 11:53:03 -0800160 CPDF_ShadingObject* pShadingObj = pPageObj->AsShading();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700161 pShadingObj->CalcBoundingBox();
162 break;
163 }
164 case FPDF_PAGEOBJ_FORM: {
Wei Li7cf13c92016-02-19 11:53:03 -0800165 CPDF_FormObject* pFormObj = pPageObj->AsForm();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700166 pFormObj->CalcBoundingBox();
167 break;
168 }
169 default:
170 break;
171 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700172}
173
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700174DLLEXPORT int STDCALL FPDFPage_CountObject(FPDF_PAGE page) {
Tom Sepezbf59a072015-10-21 14:07:23 -0700175 CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700176 if (!pPage || !pPage->m_pFormDict || !pPage->m_pFormDict->KeyExist("Type") ||
177 !pPage->m_pFormDict->GetElement("Type")->GetDirect() ||
178 pPage->m_pFormDict->GetElement("Type")->GetDirect()->GetString().Compare(
179 "Page")) {
180 return -1;
181 }
Tom Sepez2398d892016-02-17 16:46:26 -0800182 return pdfium::CollectionSize<int>(*pPage->GetPageObjectList());
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700183}
184
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700185DLLEXPORT FPDF_PAGEOBJECT STDCALL FPDFPage_GetObject(FPDF_PAGE page,
186 int index) {
Tom Sepezdb0be962015-10-16 14:00:21 -0700187 CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700188 if (!pPage || !pPage->m_pFormDict || !pPage->m_pFormDict->KeyExist("Type") ||
189 pPage->m_pFormDict->GetElement("Type")->GetDirect()->GetString().Compare(
190 "Page")) {
Tom Sepez2398d892016-02-17 16:46:26 -0800191 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700192 }
Tom Sepez2398d892016-02-17 16:46:26 -0800193 return pPage->GetPageObjectList()->GetPageObjectByIndex(index);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700194}
195
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700196DLLEXPORT FPDF_BOOL STDCALL FPDFPage_HasTransparency(FPDF_PAGE page) {
Tom Sepezdb0be962015-10-16 14:00:21 -0700197 CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
198 return pPage && pPage->BackgroundAlphaNeeded();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700199}
200
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700201DLLEXPORT FPDF_BOOL STDCALL
202FPDFPageObj_HasTransparency(FPDF_PAGEOBJECT pageObject) {
203 if (!pageObject)
204 return FALSE;
205 CPDF_PageObject* pPageObj = (CPDF_PageObject*)pageObject;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700206
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700207 const CPDF_GeneralStateData* pGeneralState = pPageObj->m_GeneralState;
208 int blend_type =
209 pGeneralState ? pGeneralState->m_BlendType : FXDIB_BLEND_NORMAL;
210 if (blend_type != FXDIB_BLEND_NORMAL)
211 return TRUE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700212
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700213 CPDF_Dictionary* pSMaskDict =
Dan Sinclairf1251c12015-10-20 16:24:45 -0400214 pGeneralState ? ToDictionary(pGeneralState->m_pSoftMask) : NULL;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700215 if (pSMaskDict)
216 return TRUE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700217
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700218 if (pGeneralState && pGeneralState->m_FillAlpha != 1.0f)
219 return TRUE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700220
Wei Li7cf13c92016-02-19 11:53:03 -0800221 if (pPageObj->IsPath()) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700222 if (pGeneralState && pGeneralState->m_StrokeAlpha != 1.0f)
223 return TRUE;
224 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700225
Wei Li7cf13c92016-02-19 11:53:03 -0800226 if (pPageObj->IsForm()) {
227 CPDF_FormObject* pFormObj = pPageObj->AsForm();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700228 if (pFormObj->m_pForm &&
229 (pFormObj->m_pForm->m_Transparency & PDFTRANS_ISOLATED))
230 return TRUE;
231 if (pFormObj->m_pForm &&
232 (!(pFormObj->m_pForm->m_Transparency & PDFTRANS_ISOLATED) &&
233 (pFormObj->m_pForm->m_Transparency & PDFTRANS_GROUP)))
234 return TRUE;
235 }
236 return FALSE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700237}
238
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700239DLLEXPORT FPDF_BOOL STDCALL FPDFPage_GenerateContent(FPDF_PAGE page) {
Tom Sepezdb0be962015-10-16 14:00:21 -0700240 CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700241 if (!pPage || !pPage->m_pFormDict || !pPage->m_pFormDict->KeyExist("Type") ||
242 !pPage->m_pFormDict->GetElement("Type")->GetDirect() ||
243 pPage->m_pFormDict->GetElement("Type")->GetDirect()->GetString().Compare(
244 "Page")) {
245 return FALSE;
246 }
Tom Sepeze19e06e2016-01-21 10:49:56 -0800247 CPDF_PageContentGenerator CG(pPage);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700248 CG.GenerateContent();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700249
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700250 return TRUE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700251}
252
253DLLEXPORT void STDCALL FPDFPageObj_Transform(FPDF_PAGEOBJECT page_object,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700254 double a,
255 double b,
256 double c,
257 double d,
258 double e,
259 double f) {
260 CPDF_PageObject* pPageObj = (CPDF_PageObject*)page_object;
Lei Zhang997de612015-11-04 18:17:53 -0800261 if (!pPageObj)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700262 return;
Lei Zhangcb78ef52015-10-02 10:10:49 -0700263
Tom Sepez60d909e2015-12-10 15:34:55 -0800264 CFX_Matrix matrix((FX_FLOAT)a, (FX_FLOAT)b, (FX_FLOAT)c, (FX_FLOAT)d,
265 (FX_FLOAT)e, (FX_FLOAT)f);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700266 pPageObj->Transform(matrix);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700267}
268DLLEXPORT void STDCALL FPDFPage_TransformAnnots(FPDF_PAGE page,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700269 double a,
270 double b,
271 double c,
272 double d,
273 double e,
274 double f) {
Tom Sepezdb0be962015-10-16 14:00:21 -0700275 CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700276 if (!pPage)
277 return;
278 CPDF_AnnotList AnnotList(pPage);
Lei Zhang1b700c32015-10-30 23:55:35 -0700279 for (size_t i = 0; i < AnnotList.Count(); ++i) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700280 CPDF_Annot* pAnnot = AnnotList.GetAt(i);
281 // transformAnnots Rectangle
Tom Sepez281a9ea2016-02-26 14:24:28 -0800282 CFX_FloatRect rect;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700283 pAnnot->GetRect(rect);
Tom Sepez60d909e2015-12-10 15:34:55 -0800284 CFX_Matrix matrix((FX_FLOAT)a, (FX_FLOAT)b, (FX_FLOAT)c, (FX_FLOAT)d,
285 (FX_FLOAT)e, (FX_FLOAT)f);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700286 rect.Transform(&matrix);
287 CPDF_Array* pRectArray = NULL;
Wei Li9b761132016-01-29 15:44:20 -0800288 pRectArray = pAnnot->GetAnnotDict()->GetArrayBy("Rect");
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700289 if (!pRectArray)
Lei Zhang4880d1a2015-12-18 17:05:11 -0800290 pRectArray = new CPDF_Array;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700291 pRectArray->SetAt(0, new CPDF_Number(rect.left));
292 pRectArray->SetAt(1, new CPDF_Number(rect.bottom));
293 pRectArray->SetAt(2, new CPDF_Number(rect.right));
294 pRectArray->SetAt(3, new CPDF_Number(rect.top));
295 pAnnot->GetAnnotDict()->SetAt("Rect", pRectArray);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700296
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700297 // Transform AP's rectangle
298 // To Do
299 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700300}
Bo Xu394010d2014-06-12 13:41:50 -0700301
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700302DLLEXPORT void STDCALL FPDFPage_SetRotation(FPDF_PAGE page, int rotate) {
Tom Sepezdb0be962015-10-16 14:00:21 -0700303 CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700304 if (!pPage || !pPage->m_pFormDict || !pPage->m_pFormDict->KeyExist("Type") ||
305 !pPage->m_pFormDict->GetElement("Type")->GetDirect() ||
306 pPage->m_pFormDict->GetElement("Type")->GetDirect()->GetString().Compare(
307 "Page")) {
308 return;
309 }
310 CPDF_Dictionary* pDict = pPage->m_pFormDict;
311 rotate %= 4;
Bo Xu394010d2014-06-12 13:41:50 -0700312
Tom Sepezae51c812015-08-05 12:34:06 -0700313 pDict->SetAt("Rotate", new CPDF_Number(rotate * 90));
Nico Weber0ce77e32014-07-16 13:19:08 -0700314}