blob: 2661acec1b8545e628e516aef73948bd6eb02001 [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 Sepez310438f2016-03-08 13:10:55 -08009#include "core/include/fpdfapi/cpdf_document.h"
Tom Sepez40e9ff32015-11-30 12:39:54 -080010#include "fpdfsdk/include/fsdk_define.h"
11#include "public/fpdf_formfill.h"
Tom Sepez2398d892016-02-17 16:46:26 -080012#include "third_party/base/stl_util.h"
Tom Sepez40e9ff32015-11-30 12:39:54 -080013
Tom Sepez51da0932015-11-25 16:05:49 -080014#ifdef PDF_ENABLE_XFA
Lei Zhang875b9c92016-01-08 13:51:10 -080015#include "fpdfsdk/include/fpdfxfa/fpdfxfa_app.h"
16#include "fpdfsdk/include/fpdfxfa/fpdfxfa_doc.h"
17#include "fpdfsdk/include/fpdfxfa/fpdfxfa_page.h"
Tom Sepez40e9ff32015-11-30 12:39:54 -080018#endif // PDF_ENABLE_XFA
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070019
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070020#if _FX_OS_ == _FX_ANDROID_
21#include "time.h"
22#else
23#include <ctime>
24#endif
25
Nico Weber9d8ec5a2015-08-04 13:00:21 -070026DLLEXPORT FPDF_DOCUMENT STDCALL FPDF_CreateNewDocument() {
Tom Sepezae51c812015-08-05 12:34:06 -070027 CPDF_Document* pDoc = new CPDF_Document;
Nico Weber9d8ec5a2015-08-04 13:00:21 -070028 pDoc->CreateNewDoc();
29 time_t currentTime;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070030
Nico Weber9d8ec5a2015-08-04 13:00:21 -070031 CFX_ByteString DateStr;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070032
Nico Weber9d8ec5a2015-08-04 13:00:21 -070033 if (FSDK_IsSandBoxPolicyEnabled(FPDF_POLICY_MACHINETIME_ACCESS)) {
34 if (-1 != time(&currentTime)) {
35 tm* pTM = localtime(&currentTime);
36 if (pTM) {
37 DateStr.Format("D:%04d%02d%02d%02d%02d%02d", pTM->tm_year + 1900,
38 pTM->tm_mon + 1, pTM->tm_mday, pTM->tm_hour, pTM->tm_min,
39 pTM->tm_sec);
40 }
41 }
42 }
Tom Sepezbdeeb8a2015-05-27 12:25:00 -070043
Nico Weber9d8ec5a2015-08-04 13:00:21 -070044 CPDF_Dictionary* pInfoDict = NULL;
45 pInfoDict = pDoc->GetInfo();
46 if (pInfoDict) {
47 if (FSDK_IsSandBoxPolicyEnabled(FPDF_POLICY_MACHINETIME_ACCESS))
Lei Zhang4880d1a2015-12-18 17:05:11 -080048 pInfoDict->SetAt("CreationDate", new CPDF_String(DateStr, FALSE));
Tom Sepezae51c812015-08-05 12:34:06 -070049 pInfoDict->SetAt("Creator", new CPDF_String(L"PDFium"));
Nico Weber9d8ec5a2015-08-04 13:00:21 -070050 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070051
Tom Sepezbf59a072015-10-21 14:07:23 -070052 return FPDFDocumentFromCPDFDocument(pDoc);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070053}
54
Nico Weber9d8ec5a2015-08-04 13:00:21 -070055DLLEXPORT void STDCALL FPDFPage_Delete(FPDF_DOCUMENT document, int page_index) {
Tom Sepez471a1032015-10-15 16:17:18 -070056 CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document);
57 if (!pDoc || page_index < 0 || page_index >= pDoc->GetPageCount())
Nico Weber9d8ec5a2015-08-04 13:00:21 -070058 return;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070059
Nico Weber9d8ec5a2015-08-04 13:00:21 -070060 pDoc->DeletePage(page_index);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070061}
62
Nico Weber9d8ec5a2015-08-04 13:00:21 -070063DLLEXPORT FPDF_PAGE STDCALL FPDFPage_New(FPDF_DOCUMENT document,
64 int page_index,
65 double width,
66 double height) {
Tom Sepez471a1032015-10-15 16:17:18 -070067 CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document);
68 if (!pDoc)
69 return nullptr;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070070
Nico Weber9d8ec5a2015-08-04 13:00:21 -070071 if (page_index < 0)
72 page_index = 0;
73 if (pDoc->GetPageCount() < page_index)
74 page_index = pDoc->GetPageCount();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070075
Nico Weber9d8ec5a2015-08-04 13:00:21 -070076 CPDF_Dictionary* pPageDict = pDoc->CreateNewPage(page_index);
77 if (!pPageDict)
78 return NULL;
Tom Sepezae51c812015-08-05 12:34:06 -070079 CPDF_Array* pMediaBoxArray = new CPDF_Array;
80 pMediaBoxArray->Add(new CPDF_Number(0));
81 pMediaBoxArray->Add(new CPDF_Number(0));
82 pMediaBoxArray->Add(new CPDF_Number(FX_FLOAT(width)));
83 pMediaBoxArray->Add(new CPDF_Number(FX_FLOAT(height)));
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070084
Nico Weber9d8ec5a2015-08-04 13:00:21 -070085 pPageDict->SetAt("MediaBox", pMediaBoxArray);
Tom Sepezae51c812015-08-05 12:34:06 -070086 pPageDict->SetAt("Rotate", new CPDF_Number(0));
87 pPageDict->SetAt("Resources", new CPDF_Dictionary);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070088
Tom Sepez40e9ff32015-11-30 12:39:54 -080089#ifdef PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -070090 CPDFXFA_Page* pPage =
Tom Sepezae51c812015-08-05 12:34:06 -070091 new CPDFXFA_Page((CPDFXFA_Document*)document, page_index);
Nico Weber9d8ec5a2015-08-04 13:00:21 -070092 pPage->LoadPDFPage(pPageDict);
Tom Sepez40e9ff32015-11-30 12:39:54 -080093#else // PDF_ENABLE_XFA
94 CPDF_Page* pPage = new CPDF_Page;
95 pPage->Load(pDoc, pPageDict);
Tom Sepezb5b2a912016-01-21 11:04:37 -080096 pPage->ParseContent(nullptr);
Tom Sepez40e9ff32015-11-30 12:39:54 -080097#endif // PDF_ENABLE_XFA
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070098
Nico Weber9d8ec5a2015-08-04 13:00:21 -070099 return pPage;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700100}
101
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700102DLLEXPORT int STDCALL FPDFPage_GetRotation(FPDF_PAGE page) {
Tom Sepezdb0be962015-10-16 14:00:21 -0700103 CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700104 if (!pPage || !pPage->m_pFormDict || !pPage->m_pFormDict->KeyExist("Type") ||
105 !pPage->m_pFormDict->GetElement("Type")->GetDirect() ||
106 pPage->m_pFormDict->GetElement("Type")->GetDirect()->GetString().Compare(
107 "Page")) {
108 return -1;
109 }
110 CPDF_Dictionary* pDict = pPage->m_pFormDict;
Lei Zhang997de612015-11-04 18:17:53 -0800111 if (!pDict)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700112 return -1;
Lei Zhang997de612015-11-04 18:17:53 -0800113
114 while (pDict) {
115 if (pDict->KeyExist("Rotate")) {
116 CPDF_Object* pRotateObj = pDict->GetElement("Rotate")->GetDirect();
117 return pRotateObj ? pRotateObj->GetInteger() / 90 : 0;
118 }
119 if (!pDict->KeyExist("Parent"))
120 break;
121
122 pDict = ToDictionary(pDict->GetElement("Parent")->GetDirect());
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700123 }
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700124
Lei Zhang997de612015-11-04 18:17:53 -0800125 return 0;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700126}
127
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700128DLLEXPORT void STDCALL FPDFPage_InsertObject(FPDF_PAGE page,
129 FPDF_PAGEOBJECT page_obj) {
Tom Sepezdb0be962015-10-16 14:00:21 -0700130 CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700131 if (!pPage || !pPage->m_pFormDict || !pPage->m_pFormDict->KeyExist("Type") ||
132 !pPage->m_pFormDict->GetElement("Type")->GetDirect() ||
133 pPage->m_pFormDict->GetElement("Type")->GetDirect()->GetString().Compare(
134 "Page")) {
135 return;
136 }
137 CPDF_PageObject* pPageObj = (CPDF_PageObject*)page_obj;
Lei Zhang997de612015-11-04 18:17:53 -0800138 if (!pPageObj)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700139 return;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700140
Tom Sepez2398d892016-02-17 16:46:26 -0800141 pPage->GetPageObjectList()->push_back(
142 std::unique_ptr<CPDF_PageObject>(pPageObj));
143
Wei Li7cf13c92016-02-19 11:53:03 -0800144 switch (pPageObj->GetType()) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700145 case FPDF_PAGEOBJ_PATH: {
Wei Li7cf13c92016-02-19 11:53:03 -0800146 CPDF_PathObject* pPathObj = pPageObj->AsPath();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700147 pPathObj->CalcBoundingBox();
148 break;
149 }
150 case FPDF_PAGEOBJ_TEXT: {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700151 break;
152 }
153 case FPDF_PAGEOBJ_IMAGE: {
Wei Li7cf13c92016-02-19 11:53:03 -0800154 CPDF_ImageObject* pImageObj = pPageObj->AsImage();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700155 pImageObj->CalcBoundingBox();
156 break;
157 }
158 case FPDF_PAGEOBJ_SHADING: {
Wei Li7cf13c92016-02-19 11:53:03 -0800159 CPDF_ShadingObject* pShadingObj = pPageObj->AsShading();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700160 pShadingObj->CalcBoundingBox();
161 break;
162 }
163 case FPDF_PAGEOBJ_FORM: {
Wei Li7cf13c92016-02-19 11:53:03 -0800164 CPDF_FormObject* pFormObj = pPageObj->AsForm();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700165 pFormObj->CalcBoundingBox();
166 break;
167 }
168 default:
169 break;
170 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700171}
172
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700173DLLEXPORT int STDCALL FPDFPage_CountObject(FPDF_PAGE page) {
Tom Sepezbf59a072015-10-21 14:07:23 -0700174 CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700175 if (!pPage || !pPage->m_pFormDict || !pPage->m_pFormDict->KeyExist("Type") ||
176 !pPage->m_pFormDict->GetElement("Type")->GetDirect() ||
177 pPage->m_pFormDict->GetElement("Type")->GetDirect()->GetString().Compare(
178 "Page")) {
179 return -1;
180 }
Tom Sepez2398d892016-02-17 16:46:26 -0800181 return pdfium::CollectionSize<int>(*pPage->GetPageObjectList());
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700182}
183
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700184DLLEXPORT FPDF_PAGEOBJECT STDCALL FPDFPage_GetObject(FPDF_PAGE page,
185 int index) {
Tom Sepezdb0be962015-10-16 14:00:21 -0700186 CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700187 if (!pPage || !pPage->m_pFormDict || !pPage->m_pFormDict->KeyExist("Type") ||
188 pPage->m_pFormDict->GetElement("Type")->GetDirect()->GetString().Compare(
189 "Page")) {
Tom Sepez2398d892016-02-17 16:46:26 -0800190 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700191 }
Tom Sepez2398d892016-02-17 16:46:26 -0800192 return pPage->GetPageObjectList()->GetPageObjectByIndex(index);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700193}
194
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700195DLLEXPORT FPDF_BOOL STDCALL FPDFPage_HasTransparency(FPDF_PAGE page) {
Tom Sepezdb0be962015-10-16 14:00:21 -0700196 CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
197 return pPage && pPage->BackgroundAlphaNeeded();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700198}
199
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700200DLLEXPORT FPDF_BOOL STDCALL
201FPDFPageObj_HasTransparency(FPDF_PAGEOBJECT pageObject) {
202 if (!pageObject)
203 return FALSE;
204 CPDF_PageObject* pPageObj = (CPDF_PageObject*)pageObject;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700205
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700206 const CPDF_GeneralStateData* pGeneralState = pPageObj->m_GeneralState;
207 int blend_type =
208 pGeneralState ? pGeneralState->m_BlendType : FXDIB_BLEND_NORMAL;
209 if (blend_type != FXDIB_BLEND_NORMAL)
210 return TRUE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700211
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700212 CPDF_Dictionary* pSMaskDict =
Dan Sinclairf1251c12015-10-20 16:24:45 -0400213 pGeneralState ? ToDictionary(pGeneralState->m_pSoftMask) : NULL;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700214 if (pSMaskDict)
215 return TRUE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700216
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700217 if (pGeneralState && pGeneralState->m_FillAlpha != 1.0f)
218 return TRUE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700219
Wei Li7cf13c92016-02-19 11:53:03 -0800220 if (pPageObj->IsPath()) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700221 if (pGeneralState && pGeneralState->m_StrokeAlpha != 1.0f)
222 return TRUE;
223 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700224
Wei Li7cf13c92016-02-19 11:53:03 -0800225 if (pPageObj->IsForm()) {
226 CPDF_FormObject* pFormObj = pPageObj->AsForm();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700227 if (pFormObj->m_pForm &&
228 (pFormObj->m_pForm->m_Transparency & PDFTRANS_ISOLATED))
229 return TRUE;
230 if (pFormObj->m_pForm &&
231 (!(pFormObj->m_pForm->m_Transparency & PDFTRANS_ISOLATED) &&
232 (pFormObj->m_pForm->m_Transparency & PDFTRANS_GROUP)))
233 return TRUE;
234 }
235 return FALSE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700236}
237
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700238DLLEXPORT FPDF_BOOL STDCALL FPDFPage_GenerateContent(FPDF_PAGE page) {
Tom Sepezdb0be962015-10-16 14:00:21 -0700239 CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700240 if (!pPage || !pPage->m_pFormDict || !pPage->m_pFormDict->KeyExist("Type") ||
241 !pPage->m_pFormDict->GetElement("Type")->GetDirect() ||
242 pPage->m_pFormDict->GetElement("Type")->GetDirect()->GetString().Compare(
243 "Page")) {
244 return FALSE;
245 }
Tom Sepeze19e06e2016-01-21 10:49:56 -0800246 CPDF_PageContentGenerator CG(pPage);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700247 CG.GenerateContent();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700248
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700249 return TRUE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700250}
251
252DLLEXPORT void STDCALL FPDFPageObj_Transform(FPDF_PAGEOBJECT page_object,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700253 double a,
254 double b,
255 double c,
256 double d,
257 double e,
258 double f) {
259 CPDF_PageObject* pPageObj = (CPDF_PageObject*)page_object;
Lei Zhang997de612015-11-04 18:17:53 -0800260 if (!pPageObj)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700261 return;
Lei Zhangcb78ef52015-10-02 10:10:49 -0700262
Tom Sepez60d909e2015-12-10 15:34:55 -0800263 CFX_Matrix matrix((FX_FLOAT)a, (FX_FLOAT)b, (FX_FLOAT)c, (FX_FLOAT)d,
264 (FX_FLOAT)e, (FX_FLOAT)f);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700265 pPageObj->Transform(matrix);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700266}
267DLLEXPORT void STDCALL FPDFPage_TransformAnnots(FPDF_PAGE page,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700268 double a,
269 double b,
270 double c,
271 double d,
272 double e,
273 double f) {
Tom Sepezdb0be962015-10-16 14:00:21 -0700274 CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700275 if (!pPage)
276 return;
277 CPDF_AnnotList AnnotList(pPage);
Lei Zhang1b700c32015-10-30 23:55:35 -0700278 for (size_t i = 0; i < AnnotList.Count(); ++i) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700279 CPDF_Annot* pAnnot = AnnotList.GetAt(i);
280 // transformAnnots Rectangle
Tom Sepez281a9ea2016-02-26 14:24:28 -0800281 CFX_FloatRect rect;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700282 pAnnot->GetRect(rect);
Tom Sepez60d909e2015-12-10 15:34:55 -0800283 CFX_Matrix matrix((FX_FLOAT)a, (FX_FLOAT)b, (FX_FLOAT)c, (FX_FLOAT)d,
284 (FX_FLOAT)e, (FX_FLOAT)f);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700285 rect.Transform(&matrix);
286 CPDF_Array* pRectArray = NULL;
Wei Li9b761132016-01-29 15:44:20 -0800287 pRectArray = pAnnot->GetAnnotDict()->GetArrayBy("Rect");
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700288 if (!pRectArray)
Lei Zhang4880d1a2015-12-18 17:05:11 -0800289 pRectArray = new CPDF_Array;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700290 pRectArray->SetAt(0, new CPDF_Number(rect.left));
291 pRectArray->SetAt(1, new CPDF_Number(rect.bottom));
292 pRectArray->SetAt(2, new CPDF_Number(rect.right));
293 pRectArray->SetAt(3, new CPDF_Number(rect.top));
294 pAnnot->GetAnnotDict()->SetAt("Rect", pRectArray);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700295
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700296 // Transform AP's rectangle
297 // To Do
298 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700299}
Bo Xu394010d2014-06-12 13:41:50 -0700300
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700301DLLEXPORT void STDCALL FPDFPage_SetRotation(FPDF_PAGE page, int rotate) {
Tom Sepezdb0be962015-10-16 14:00:21 -0700302 CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700303 if (!pPage || !pPage->m_pFormDict || !pPage->m_pFormDict->KeyExist("Type") ||
304 !pPage->m_pFormDict->GetElement("Type")->GetDirect() ||
305 pPage->m_pFormDict->GetElement("Type")->GetDirect()->GetString().Compare(
306 "Page")) {
307 return;
308 }
309 CPDF_Dictionary* pDict = pPage->m_pFormDict;
310 rotate %= 4;
Bo Xu394010d2014-06-12 13:41:50 -0700311
Tom Sepezae51c812015-08-05 12:34:06 -0700312 pDict->SetAt("Rotate", new CPDF_Number(rotate * 90));
Nico Weber0ce77e32014-07-16 13:19:08 -0700313}