1 // 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.
5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
7 #include "../../public/fpdf_edit.h"
8 #include "../../public/fpdf_formfill.h"
9 #include "../include/fsdk_define.h"
11 #if _FX_OS_ == _FX_ANDROID_
17 DLLEXPORT FPDF_DOCUMENT STDCALL FPDF_CreateNewDocument() {
18 CPDF_Document* pDoc = new CPDF_Document;
22 CFX_ByteString DateStr;
24 if (FSDK_IsSandBoxPolicyEnabled(FPDF_POLICY_MACHINETIME_ACCESS)) {
25 if (-1 != time(¤tTime)) {
26 tm* pTM = localtime(¤tTime);
28 DateStr.Format("D:%04d%02d%02d%02d%02d%02d", pTM->tm_year + 1900,
29 pTM->tm_mon + 1, pTM->tm_mday, pTM->tm_hour, pTM->tm_min,
35 CPDF_Dictionary* pInfoDict = NULL;
36 pInfoDict = pDoc->GetInfo();
38 if (FSDK_IsSandBoxPolicyEnabled(FPDF_POLICY_MACHINETIME_ACCESS))
39 pInfoDict->SetAt("CreationDate", new CPDF_String(DateStr));
40 pInfoDict->SetAt("Creator", new CPDF_String(L"PDFium"));
46 DLLEXPORT void STDCALL FPDFPage_Delete(FPDF_DOCUMENT document, int page_index) {
47 CPDF_Document* pDoc = CPDF_Document::FromFPDFDocument(document);
48 if (!pDoc || page_index < 0 || page_index >= pDoc->GetPageCount())
51 pDoc->DeletePage(page_index);
54 DLLEXPORT FPDF_PAGE STDCALL FPDFPage_New(FPDF_DOCUMENT document,
58 CPDF_Document* pDoc = CPDF_Document::FromFPDFDocument(document);
64 if (pDoc->GetPageCount() < page_index)
65 page_index = pDoc->GetPageCount();
67 CPDF_Dictionary* pPageDict = pDoc->CreateNewPage(page_index);
70 CPDF_Array* pMediaBoxArray = new CPDF_Array;
71 pMediaBoxArray->Add(new CPDF_Number(0));
72 pMediaBoxArray->Add(new CPDF_Number(0));
73 pMediaBoxArray->Add(new CPDF_Number(FX_FLOAT(width)));
74 pMediaBoxArray->Add(new CPDF_Number(FX_FLOAT(height)));
76 pPageDict->SetAt("MediaBox", pMediaBoxArray);
77 pPageDict->SetAt("Rotate", new CPDF_Number(0));
78 pPageDict->SetAt("Resources", new CPDF_Dictionary);
80 CPDF_Page* pPage = new CPDF_Page;
81 pPage->Load(pDoc, pPageDict);
82 pPage->ParseContent();
87 DLLEXPORT int STDCALL FPDFPage_GetRotation(FPDF_PAGE page) {
88 CPDF_Page* pPage = (CPDF_Page*)page;
89 if (!pPage || !pPage->m_pFormDict || !pPage->m_pFormDict->KeyExist("Type") ||
90 !pPage->m_pFormDict->GetElement("Type")->GetDirect() ||
91 pPage->m_pFormDict->GetElement("Type")->GetDirect()->GetString().Compare(
95 CPDF_Dictionary* pDict = pPage->m_pFormDict;
99 if (pDict->KeyExist("Rotate"))
100 rotate = pDict->GetElement("Rotate")->GetDirect()
101 ? pDict->GetElement("Rotate")->GetDirect()->GetInteger() / 90
104 if (pDict->KeyExist("Parent")) {
105 CPDF_Dictionary* pPages =
106 (CPDF_Dictionary*)pDict->GetElement("Parent")->GetDirect();
108 if (pPages->KeyExist("Rotate")) {
110 pPages->GetElement("Rotate")->GetDirect()
111 ? pPages->GetElement("Rotate")->GetDirect()->GetInteger() /
115 } else if (pPages->KeyExist("Parent"))
117 (CPDF_Dictionary*)pPages->GetElement("Parent")->GetDirect();
130 DLLEXPORT void STDCALL FPDFPage_InsertObject(FPDF_PAGE page,
131 FPDF_PAGEOBJECT page_obj) {
132 CPDF_Page* pPage = (CPDF_Page*)page;
133 if (!pPage || !pPage->m_pFormDict || !pPage->m_pFormDict->KeyExist("Type") ||
134 !pPage->m_pFormDict->GetElement("Type")->GetDirect() ||
135 pPage->m_pFormDict->GetElement("Type")->GetDirect()->GetString().Compare(
139 CPDF_PageObject* pPageObj = (CPDF_PageObject*)page_obj;
140 if (pPageObj == NULL)
142 FX_POSITION LastPersition = pPage->GetLastObjectPosition();
144 pPage->InsertObject(LastPersition, pPageObj);
145 switch (pPageObj->m_Type) {
146 case FPDF_PAGEOBJ_PATH: {
147 CPDF_PathObject* pPathObj = (CPDF_PathObject*)pPageObj;
148 pPathObj->CalcBoundingBox();
151 case FPDF_PAGEOBJ_TEXT: {
152 // CPDF_PathObject* pPathObj = (CPDF_PathObject*)pPageObj;
153 // pPathObj->CalcBoundingBox();
156 case FPDF_PAGEOBJ_IMAGE: {
157 CPDF_ImageObject* pImageObj = (CPDF_ImageObject*)pPageObj;
158 pImageObj->CalcBoundingBox();
161 case FPDF_PAGEOBJ_SHADING: {
162 CPDF_ShadingObject* pShadingObj = (CPDF_ShadingObject*)pPageObj;
163 pShadingObj->CalcBoundingBox();
166 case FPDF_PAGEOBJ_FORM: {
167 CPDF_FormObject* pFormObj = (CPDF_FormObject*)pPageObj;
168 pFormObj->CalcBoundingBox();
175 // pPage->ParseContent();
176 // pPage->GenerateContent();
179 DLLEXPORT int STDCALL FPDFPage_CountObject(FPDF_PAGE page) {
180 CPDF_Page* pPage = (CPDF_Page*)page;
181 if (!pPage || !pPage->m_pFormDict || !pPage->m_pFormDict->KeyExist("Type") ||
182 !pPage->m_pFormDict->GetElement("Type")->GetDirect() ||
183 pPage->m_pFormDict->GetElement("Type")->GetDirect()->GetString().Compare(
187 return pPage->CountObjects();
191 DLLEXPORT FPDF_PAGEOBJECT STDCALL FPDFPage_GetObject(FPDF_PAGE page,
193 CPDF_Page* pPage = (CPDF_Page*)page;
194 if (!pPage || !pPage->m_pFormDict || !pPage->m_pFormDict->KeyExist("Type") ||
195 pPage->m_pFormDict->GetElement("Type")->GetDirect()->GetString().Compare(
199 return pPage->GetObjectByIndex(index);
203 DLLEXPORT FPDF_BOOL STDCALL FPDFPage_HasTransparency(FPDF_PAGE page) {
206 CPDF_Page* pPage = (CPDF_Page*)page;
208 return pPage->BackgroundAlphaNeeded();
211 DLLEXPORT FPDF_BOOL STDCALL
212 FPDFPageObj_HasTransparency(FPDF_PAGEOBJECT pageObject) {
215 CPDF_PageObject* pPageObj = (CPDF_PageObject*)pageObject;
217 const CPDF_GeneralStateData* pGeneralState = pPageObj->m_GeneralState;
219 pGeneralState ? pGeneralState->m_BlendType : FXDIB_BLEND_NORMAL;
220 if (blend_type != FXDIB_BLEND_NORMAL)
223 CPDF_Dictionary* pSMaskDict =
224 pGeneralState ? (CPDF_Dictionary*)pGeneralState->m_pSoftMask : NULL;
228 if (pGeneralState && pGeneralState->m_FillAlpha != 1.0f)
231 if (pPageObj->m_Type == PDFPAGE_PATH) {
232 if (pGeneralState && pGeneralState->m_StrokeAlpha != 1.0f)
236 if (pPageObj->m_Type == PDFPAGE_FORM) {
237 CPDF_FormObject* pFormObj = (CPDF_FormObject*)pPageObj;
238 if (pFormObj->m_pForm &&
239 (pFormObj->m_pForm->m_Transparency & PDFTRANS_ISOLATED))
241 if (pFormObj->m_pForm &&
242 (!(pFormObj->m_pForm->m_Transparency & PDFTRANS_ISOLATED) &&
243 (pFormObj->m_pForm->m_Transparency & PDFTRANS_GROUP)))
249 DLLEXPORT FPDF_BOOL STDCALL FPDFPage_GenerateContent(FPDF_PAGE page) {
250 CPDF_Page* pPage = (CPDF_Page*)page;
251 if (!pPage || !pPage->m_pFormDict || !pPage->m_pFormDict->KeyExist("Type") ||
252 !pPage->m_pFormDict->GetElement("Type")->GetDirect() ||
253 pPage->m_pFormDict->GetElement("Type")->GetDirect()->GetString().Compare(
257 CPDF_PageContentGenerate CG(pPage);
258 CG.GenerateContent();
263 DLLEXPORT void STDCALL FPDFPageObj_Transform(FPDF_PAGEOBJECT page_object,
270 CPDF_PageObject* pPageObj = (CPDF_PageObject*)page_object;
271 if (pPageObj == NULL)
274 CFX_AffineMatrix matrix((FX_FLOAT)a, (FX_FLOAT)b, (FX_FLOAT)c, (FX_FLOAT)d,
275 (FX_FLOAT)e, (FX_FLOAT)f);
276 pPageObj->Transform(matrix);
278 DLLEXPORT void STDCALL FPDFPage_TransformAnnots(FPDF_PAGE page,
287 CPDF_Page* pPage = (CPDF_Page*)page;
288 CPDF_AnnotList AnnotList(pPage);
289 for (int i = 0; i < AnnotList.Count(); i++) {
290 CPDF_Annot* pAnnot = AnnotList.GetAt(i);
291 // transformAnnots Rectangle
293 pAnnot->GetRect(rect);
294 CFX_AffineMatrix matrix((FX_FLOAT)a, (FX_FLOAT)b, (FX_FLOAT)c, (FX_FLOAT)d,
295 (FX_FLOAT)e, (FX_FLOAT)f);
296 rect.Transform(&matrix);
297 CPDF_Array* pRectArray = NULL;
298 pRectArray = pAnnot->GetAnnotDict()->GetArray("Rect");
300 pRectArray = CPDF_Array::Create();
301 pRectArray->SetAt(0, new CPDF_Number(rect.left));
302 pRectArray->SetAt(1, new CPDF_Number(rect.bottom));
303 pRectArray->SetAt(2, new CPDF_Number(rect.right));
304 pRectArray->SetAt(3, new CPDF_Number(rect.top));
305 pAnnot->GetAnnotDict()->SetAt("Rect", pRectArray);
307 // Transform AP's rectangle
312 DLLEXPORT void STDCALL FPDFPage_SetRotation(FPDF_PAGE page, int rotate) {
313 CPDF_Page* pPage = (CPDF_Page*)page;
314 if (!pPage || !pPage->m_pFormDict || !pPage->m_pFormDict->KeyExist("Type") ||
315 !pPage->m_pFormDict->GetElement("Type")->GetDirect() ||
316 pPage->m_pFormDict->GetElement("Type")->GetDirect()->GetString().Compare(
320 CPDF_Dictionary* pDict = pPage->m_pFormDict;
323 pDict->SetAt("Rotate", new CPDF_Number(rotate * 90));