Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
cjs_document.cpp
Go to the documentation of this file.
1// Copyright 2017 The PDFium Authors
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
6
7#include "fxjs/cjs_document.h"
8
9#include <stdint.h>
10
11#include <utility>
12
13#include "constants/access_permissions.h"
14#include "core/fpdfapi/page/cpdf_pageimagecache.h"
15#include "core/fpdfapi/page/cpdf_pageobject.h"
16#include "core/fpdfapi/page/cpdf_textobject.h"
17#include "core/fpdfapi/parser/cpdf_array.h"
18#include "core/fpdfapi/parser/cpdf_dictionary.h"
19#include "core/fpdfapi/parser/cpdf_name.h"
20#include "core/fpdfapi/parser/cpdf_string.h"
21#include "core/fpdfdoc/cpdf_interactiveform.h"
22#include "core/fpdfdoc/cpdf_nametree.h"
23#include "fpdfsdk/cpdfsdk_annotiteration.h"
24#include "fpdfsdk/cpdfsdk_formfillenvironment.h"
25#include "fpdfsdk/cpdfsdk_interactiveform.h"
26#include "fpdfsdk/cpdfsdk_pageview.h"
27#include "fxjs/cjs_annot.h"
28#include "fxjs/cjs_app.h"
29#include "fxjs/cjs_delaydata.h"
30#include "fxjs/cjs_event_context.h"
31#include "fxjs/cjs_field.h"
32#include "fxjs/cjs_icon.h"
33#include "fxjs/js_resources.h"
34#include "third_party/base/check.h"
35#include "third_party/base/containers/span.h"
36#include "v8/include/v8-container.h"
37
38const JSPropertySpec CJS_Document::PropertySpecs[] = {
39 {"ADBE", get_ADBE_static, set_ADBE_static},
40 {"author", get_author_static, set_author_static},
41 {"baseURL", get_base_URL_static, set_base_URL_static},
42 {"bookmarkRoot", get_bookmark_root_static, set_bookmark_root_static},
43 {"calculate", get_calculate_static, set_calculate_static},
44 {"Collab", get_collab_static, set_collab_static},
45 {"creationDate", get_creation_date_static, set_creation_date_static},
46 {"creator", get_creator_static, set_creator_static},
47 {"delay", get_delay_static, set_delay_static},
48 {"dirty", get_dirty_static, set_dirty_static},
49 {"documentFileName", get_document_file_name_static,
50 set_document_file_name_static},
51 {"external", get_external_static, set_external_static},
52 {"filesize", get_filesize_static, set_filesize_static},
53 {"icons", get_icons_static, set_icons_static},
54 {"info", get_info_static, set_info_static},
55 {"keywords", get_keywords_static, set_keywords_static},
56 {"layout", get_layout_static, set_layout_static},
57 {"media", get_media_static, set_media_static},
58 {"modDate", get_mod_date_static, set_mod_date_static},
59 {"mouseX", get_mouse_x_static, set_mouse_x_static},
60 {"mouseY", get_mouse_y_static, set_mouse_y_static},
61 {"numFields", get_num_fields_static, set_num_fields_static},
62 {"numPages", get_num_pages_static, set_num_pages_static},
63 {"pageNum", get_page_num_static, set_page_num_static},
64 {"pageWindowRect", get_page_window_rect_static,
65 set_page_window_rect_static},
66 {"path", get_path_static, set_path_static},
67 {"producer", get_producer_static, set_producer_static},
68 {"subject", get_subject_static, set_subject_static},
69 {"title", get_title_static, set_title_static},
70 {"URL", get_URL_static, set_URL_static},
71 {"zoom", get_zoom_static, set_zoom_static},
72 {"zoomType", get_zoom_type_static, set_zoom_type_static}};
73
74const JSMethodSpec CJS_Document::MethodSpecs[] = {
75 {"addAnnot", addAnnot_static},
76 {"addField", addField_static},
77 {"addLink", addLink_static},
78 {"addIcon", addIcon_static},
79 {"calculateNow", calculateNow_static},
80 {"closeDoc", closeDoc_static},
81 {"createDataObject", createDataObject_static},
82 {"deletePages", deletePages_static},
83 {"exportAsText", exportAsText_static},
84 {"exportAsFDF", exportAsFDF_static},
85 {"exportAsXFDF", exportAsXFDF_static},
86 {"extractPages", extractPages_static},
87 {"getAnnot", getAnnot_static},
88 {"getAnnots", getAnnots_static},
89 {"getAnnot3D", getAnnot3D_static},
90 {"getAnnots3D", getAnnots3D_static},
91 {"getField", getField_static},
92 {"getIcon", getIcon_static},
93 {"getLinks", getLinks_static},
94 {"getNthFieldName", getNthFieldName_static},
95 {"getOCGs", getOCGs_static},
96 {"getPageBox", getPageBox_static},
97 {"getPageNthWord", getPageNthWord_static},
98 {"getPageNthWordQuads", getPageNthWordQuads_static},
99 {"getPageNumWords", getPageNumWords_static},
100 {"getPrintParams", getPrintParams_static},
101 {"getURL", getURL_static},
102 {"gotoNamedDest", gotoNamedDest_static},
103 {"importAnFDF", importAnFDF_static},
104 {"importAnXFDF", importAnXFDF_static},
105 {"importTextData", importTextData_static},
106 {"insertPages", insertPages_static},
107 {"mailDoc", mailDoc_static},
108 {"mailForm", mailForm_static},
109 {"print", print_static},
110 {"removeField", removeField_static},
111 {"replacePages", replacePages_static},
112 {"resetForm", resetForm_static},
113 {"removeIcon", removeIcon_static},
114 {"saveAs", saveAs_static},
115 {"submitForm", submitForm_static},
116 {"syncAnnotScan", syncAnnotScan_static}};
117
118uint32_t CJS_Document::ObjDefnID = 0;
119const char CJS_Document::kName[] = "Document";
120
121// static
122uint32_t CJS_Document::GetObjDefnID() {
123 return ObjDefnID;
124}
125
126// static
127void CJS_Document::DefineJSObjects(CFXJS_Engine* pEngine) {
128 ObjDefnID = pEngine->DefineObj(CJS_Document::kName, FXJSOBJTYPE_GLOBAL,
129 JSConstructor<CJS_Document>, JSDestructor);
130 DefineProps(pEngine, ObjDefnID, PropertySpecs);
131 DefineMethods(pEngine, ObjDefnID, MethodSpecs);
132}
133
134CJS_Document::CJS_Document(v8::Local<v8::Object> pObject, CJS_Runtime* pRuntime)
135 : CJS_Object(pObject, pRuntime) {
136 SetFormFillEnv(GetRuntime()->GetFormFillEnv());
137}
138
139CJS_Document::~CJS_Document() = default;
140
141// The total number of fields in document.
142CJS_Result CJS_Document::get_num_fields(CJS_Runtime* pRuntime) {
143 if (!m_pFormFillEnv)
145
146 CPDF_InteractiveForm* pPDFForm = GetCoreInteractiveForm();
147 return CJS_Result::Success(pRuntime->NewNumber(
148 static_cast<int>(pPDFForm->CountFields(WideString()))));
149}
150
151CJS_Result CJS_Document::set_num_fields(CJS_Runtime* pRuntime,
152 v8::Local<v8::Value> vp) {
154}
155
156CJS_Result CJS_Document::get_dirty(CJS_Runtime* pRuntime) {
157 if (!m_pFormFillEnv)
159
160 return CJS_Result::Success(
161 pRuntime->NewBoolean(!!m_pFormFillEnv->GetChangeMark()));
162}
163
164CJS_Result CJS_Document::set_dirty(CJS_Runtime* pRuntime,
165 v8::Local<v8::Value> vp) {
166 if (!m_pFormFillEnv)
168
169 pRuntime->ToBoolean(vp) ? m_pFormFillEnv->SetChangeMark()
170 : m_pFormFillEnv->ClearChangeMark();
172}
173
174CJS_Result CJS_Document::get_ADBE(CJS_Runtime* pRuntime) {
175 return CJS_Result::Success(pRuntime->NewUndefined());
176}
177
178CJS_Result CJS_Document::set_ADBE(CJS_Runtime* pRuntime,
179 v8::Local<v8::Value> vp) {
181}
182
183CJS_Result CJS_Document::get_page_num(CJS_Runtime* pRuntime) {
184 if (!m_pFormFillEnv)
186
187 CPDFSDK_PageView* pPageView = m_pFormFillEnv->GetCurrentView();
188 if (!pPageView)
189 return CJS_Result::Success(pRuntime->NewUndefined());
190
191 return CJS_Result::Success(pRuntime->NewNumber(pPageView->GetPageIndex()));
192}
193
194CJS_Result CJS_Document::set_page_num(CJS_Runtime* pRuntime,
195 v8::Local<v8::Value> vp) {
196 if (!m_pFormFillEnv)
198
199 int iPageCount = m_pFormFillEnv->GetPageCount();
200 int iPageNum = pRuntime->ToInt32(vp);
201 if (iPageNum >= 0 && iPageNum < iPageCount)
202 m_pFormFillEnv->JS_docgotoPage(iPageNum);
203 else if (iPageNum >= iPageCount)
204 m_pFormFillEnv->JS_docgotoPage(iPageCount - 1);
205 else if (iPageNum < 0)
206 m_pFormFillEnv->JS_docgotoPage(0);
207
209}
210
211CJS_Result CJS_Document::addAnnot(CJS_Runtime* pRuntime,
212 pdfium::span<v8::Local<v8::Value>> params) {
213 // Not supported, but do not return an error.
215}
216
217CJS_Result CJS_Document::addField(CJS_Runtime* pRuntime,
218 pdfium::span<v8::Local<v8::Value>> params) {
219 // Not supported, but do not return an error.
221}
222
223CJS_Result CJS_Document::exportAsText(
224 CJS_Runtime* pRuntime,
225 pdfium::span<v8::Local<v8::Value>> params) {
226 // Unsafe, not supported, but do not return an error.
228}
229
230CJS_Result CJS_Document::exportAsFDF(
231 CJS_Runtime* pRuntime,
232 pdfium::span<v8::Local<v8::Value>> params) {
233 // Unsafe, not supported, but do not return an error.
235}
236
237CJS_Result CJS_Document::exportAsXFDF(
238 CJS_Runtime* pRuntime,
239 pdfium::span<v8::Local<v8::Value>> params) {
240 // Unsafe, not supported, but do not return an error.
242}
243
244CJS_Result CJS_Document::getField(CJS_Runtime* pRuntime,
245 pdfium::span<v8::Local<v8::Value>> params) {
246 if (params.empty())
248
249 if (!m_pFormFillEnv)
251
252 WideString wideName = pRuntime->ToWideString(params[0]);
253 CPDF_InteractiveForm* pPDFForm = GetCoreInteractiveForm();
254 if (pPDFForm->CountFields(wideName) <= 0)
255 return CJS_Result::Success(pRuntime->NewUndefined());
256
257 v8::Local<v8::Object> pFieldObj = pRuntime->NewFXJSBoundObject(
258 CJS_Field::GetObjDefnID(), FXJSOBJTYPE_DYNAMIC);
259 if (pFieldObj.IsEmpty())
261
262 auto* pJSField = static_cast<CJS_Field*>(
263 CFXJS_Engine::GetObjectPrivate(pRuntime->GetIsolate(), pFieldObj));
264 if (!pJSField)
266
267 pJSField->AttachField(this, wideName);
268 return CJS_Result::Success(pJSField->ToV8Object());
269}
270
271// Gets the name of the nth field in the document
272CJS_Result CJS_Document::getNthFieldName(
273 CJS_Runtime* pRuntime,
274 pdfium::span<v8::Local<v8::Value>> params) {
275 if (params.size() != 1)
277 if (!m_pFormFillEnv)
279
280 int nIndex = pRuntime->ToInt32(params[0]);
281 if (nIndex < 0)
283
284 CPDF_InteractiveForm* pPDFForm = GetCoreInteractiveForm();
285 CPDF_FormField* pField = pPDFForm->GetField(nIndex, WideString());
286 if (!pField)
289 pRuntime->NewString(pField->GetFullName().AsStringView()));
290}
291
292CJS_Result CJS_Document::importAnFDF(
293 CJS_Runtime* pRuntime,
294 pdfium::span<v8::Local<v8::Value>> params) {
295 // Unsafe, not supported.
297}
298
299CJS_Result CJS_Document::importAnXFDF(
300 CJS_Runtime* pRuntime,
301 pdfium::span<v8::Local<v8::Value>> params) {
302 // Unsafe, not supported.
304}
305
306CJS_Result CJS_Document::importTextData(
307 CJS_Runtime* pRuntime,
308 pdfium::span<v8::Local<v8::Value>> params) {
309 // Unsafe, not supported.
311}
312
313CJS_Result CJS_Document::mailDoc(CJS_Runtime* pRuntime,
314 pdfium::span<v8::Local<v8::Value>> params) {
315 if (!m_pFormFillEnv)
317
318 v8::LocalVector<v8::Value> newParams = ExpandKeywordParams(
319 pRuntime, params, 6, "bUI", "cTo", "cCc", "cBcc", "cSubject", "cMsg");
320
321 bool bUI = true;
322 if (IsExpandedParamKnown(newParams[0]))
323 bUI = pRuntime->ToBoolean(newParams[0]);
324
325 WideString cTo;
326 if (IsExpandedParamKnown(newParams[1]))
327 cTo = pRuntime->ToWideString(newParams[1]);
328
329 WideString cCc;
330 if (IsExpandedParamKnown(newParams[2]))
331 cCc = pRuntime->ToWideString(newParams[2]);
332
333 WideString cBcc;
334 if (IsExpandedParamKnown(newParams[3]))
335 cBcc = pRuntime->ToWideString(newParams[3]);
336
337 WideString cSubject;
338 if (IsExpandedParamKnown(newParams[4]))
339 cSubject = pRuntime->ToWideString(newParams[4]);
340
341 WideString cMsg;
342 if (IsExpandedParamKnown(newParams[5]))
343 cMsg = pRuntime->ToWideString(newParams[5]);
344
345 pRuntime->BeginBlock();
346 m_pFormFillEnv->JS_docmailForm(pdfium::span<const uint8_t>(), bUI, cTo,
347 cSubject, cCc, cBcc, cMsg);
348 pRuntime->EndBlock();
350}
351
352// exports the form data and mails the resulting fdf file as an attachment to
353// all recipients.
354// comment: need reader supports
355CJS_Result CJS_Document::mailForm(CJS_Runtime* pRuntime,
356 pdfium::span<v8::Local<v8::Value>> params) {
357 if (!m_pFormFillEnv)
359
360 using pdfium::access_permissions::kExtractForAccessibility;
361 if (!m_pFormFillEnv->HasPermissions(kExtractForAccessibility))
363
364 CPDFSDK_InteractiveForm* pInteractiveForm = GetSDKInteractiveForm();
365 ByteString sTextBuf = pInteractiveForm->ExportFormToFDFTextBuf();
366 if (sTextBuf.IsEmpty())
367 return CJS_Result::Failure(L"Bad FDF format.");
368
369 v8::LocalVector<v8::Value> newParams = ExpandKeywordParams(
370 pRuntime, params, 6, "bUI", "cTo", "cCc", "cBcc", "cSubject", "cMsg");
371
372 bool bUI = true;
373 if (IsExpandedParamKnown(newParams[0]))
374 bUI = pRuntime->ToBoolean(newParams[0]);
375
376 WideString cTo;
377 if (IsExpandedParamKnown(newParams[1]))
378 cTo = pRuntime->ToWideString(newParams[1]);
379
380 WideString cCc;
381 if (IsExpandedParamKnown(newParams[2]))
382 cCc = pRuntime->ToWideString(newParams[2]);
383
384 WideString cBcc;
385 if (IsExpandedParamKnown(newParams[3]))
386 cBcc = pRuntime->ToWideString(newParams[3]);
387
388 WideString cSubject;
389 if (IsExpandedParamKnown(newParams[4]))
390 cSubject = pRuntime->ToWideString(newParams[4]);
391
392 WideString cMsg;
393 if (IsExpandedParamKnown(newParams[5]))
394 cMsg = pRuntime->ToWideString(newParams[5]);
395
396 pRuntime->BeginBlock();
397 m_pFormFillEnv->JS_docmailForm(sTextBuf.raw_span(), bUI, cTo, cSubject, cCc,
398 cBcc, cMsg);
399 pRuntime->EndBlock();
401}
402
403CJS_Result CJS_Document::print(CJS_Runtime* pRuntime,
404 pdfium::span<v8::Local<v8::Value>> params) {
405 v8::LocalVector<v8::Value> newParams = ExpandKeywordParams(
406 pRuntime, params, 8, "bUI", "nStart", "nEnd", "bSilent", "bShrinkToFit",
407 "bPrintAsImage", "bReverse", "bAnnotations");
408
409 bool bUI = true;
410 if (IsExpandedParamKnown(newParams[0]))
411 bUI = pRuntime->ToBoolean(newParams[0]);
412
413 int nStart = 0;
414 if (IsExpandedParamKnown(newParams[1]))
415 nStart = pRuntime->ToInt32(newParams[1]);
416
417 int nEnd = 0;
418 if (IsExpandedParamKnown(newParams[2]))
419 nEnd = pRuntime->ToInt32(newParams[2]);
420
421 bool bSilent = false;
422 if (IsExpandedParamKnown(newParams[3]))
423 bSilent = pRuntime->ToBoolean(newParams[3]);
424
425 bool bShrinkToFit = false;
426 if (IsExpandedParamKnown(newParams[4]))
427 bShrinkToFit = pRuntime->ToBoolean(newParams[4]);
428
429 bool bPrintAsImage = false;
430 if (IsExpandedParamKnown(newParams[5]))
431 bPrintAsImage = pRuntime->ToBoolean(newParams[5]);
432
433 bool bReverse = false;
434 if (IsExpandedParamKnown(newParams[6]))
435 bReverse = pRuntime->ToBoolean(newParams[6]);
436
437 bool bAnnotations = false;
438 if (IsExpandedParamKnown(newParams[7]))
439 bAnnotations = pRuntime->ToBoolean(newParams[7]);
440
441 if (!m_pFormFillEnv)
443
444 CJS_EventContext* pHandler = pRuntime->GetCurrentEventContext();
445 if (!pHandler->IsUserGesture())
447
448 m_pFormFillEnv->JS_docprint(bUI, nStart, nEnd, bSilent, bShrinkToFit,
449 bPrintAsImage, bReverse, bAnnotations);
451}
452
453// removes the specified field from the document.
454// comment:
455// note: if the filed name is not rational, adobe is dumb for it.
456CJS_Result CJS_Document::removeField(
457 CJS_Runtime* pRuntime,
458 pdfium::span<v8::Local<v8::Value>> params) {
459 if (params.size() != 1)
461 if (!m_pFormFillEnv)
463
464 if (!m_pFormFillEnv->HasPermissions(
465 pdfium::access_permissions::kModifyContent |
466 pdfium::access_permissions::kModifyAnnotation)) {
468 }
469
470 WideString sFieldName = pRuntime->ToWideString(params[0]);
471 CPDFSDK_InteractiveForm* pInteractiveForm = GetSDKInteractiveForm();
472 std::vector<ObservedPtr<CPDFSDK_Widget>> widgets;
473 pInteractiveForm->GetWidgets(sFieldName, &widgets);
474 if (widgets.empty())
476
477 for (const auto& pWidget : widgets) {
478 if (!pWidget)
479 continue;
480
481 IPDF_Page* pPage = pWidget->GetPage();
482 DCHECK(pPage);
483
484 // If there is currently no pageview associated with the page being used
485 // do not create one. We may be in the process of tearing down the document
486 // and creating a new pageview at this point will cause bad things.
487 CPDFSDK_PageView* pPageView = m_pFormFillEnv->GetPageView(pPage);
488 if (!pPageView)
489 continue;
490
491 CFX_FloatRect rcAnnot = pWidget->GetRect();
492 rcAnnot.Inflate(1.0f, 1.0f, 1.0f, 1.0f);
493
494 std::vector<CFX_FloatRect> aRefresh(1, rcAnnot);
495 pPageView->UpdateRects(aRefresh);
496 }
497 m_pFormFillEnv->SetChangeMark();
499}
500
501// reset filed values within a document.
502// comment:
503// note: if the fields names r not rational, aodbe is dumb for it.
504
505CJS_Result CJS_Document::resetForm(CJS_Runtime* pRuntime,
506 pdfium::span<v8::Local<v8::Value>> params) {
507 if (!m_pFormFillEnv)
509
510 if (!m_pFormFillEnv->HasPermissions(
511 pdfium::access_permissions::kModifyContent |
512 pdfium::access_permissions::kModifyAnnotation |
513 pdfium::access_permissions::kFillForm)) {
515 }
516
517 CPDF_InteractiveForm* pPDFForm = GetCoreInteractiveForm();
518 if (params.empty()) {
519 pPDFForm->ResetForm();
520 m_pFormFillEnv->SetChangeMark();
522 }
523
524 v8::Local<v8::Array> array;
525 if (params[0]->IsString()) {
526 array = pRuntime->NewArray();
527 pRuntime->PutArrayElement(array, 0, params[0]);
528 } else {
529 array = pRuntime->ToArray(params[0]);
530 }
531
532 std::vector<CPDF_FormField*> aFields;
533 for (size_t i = 0; i < pRuntime->GetArrayLength(array); ++i) {
534 WideString swVal =
535 pRuntime->ToWideString(pRuntime->GetArrayElement(array, i));
536 const size_t jsz = pPDFForm->CountFields(swVal);
537 for (size_t j = 0; j < jsz; ++j)
538 aFields.push_back(pPDFForm->GetField(j, swVal));
539 }
540
541 if (!aFields.empty()) {
542 pPDFForm->ResetForm(aFields, true);
543 m_pFormFillEnv->SetChangeMark();
544 }
545
547}
548
549CJS_Result CJS_Document::saveAs(CJS_Runtime* pRuntime,
550 pdfium::span<v8::Local<v8::Value>> params) {
551 // Unsafe, not supported.
553}
554
555CJS_Result CJS_Document::syncAnnotScan(
556 CJS_Runtime* pRuntime,
557 pdfium::span<v8::Local<v8::Value>> params) {
559}
560
561CJS_Result CJS_Document::submitForm(CJS_Runtime* pRuntime,
562 pdfium::span<v8::Local<v8::Value>> params) {
563 size_t nSize = params.size();
564 if (nSize < 1)
566 if (!m_pFormFillEnv)
568
569 CJS_EventContext* pHandler = pRuntime->GetCurrentEventContext();
570 if (!pHandler->IsUserGesture())
572
573 v8::Local<v8::Array> aFields;
574 WideString strURL;
575 bool bFDF = true;
576 bool bEmpty = false;
577 if (params[0]->IsString()) {
578 strURL = pRuntime->ToWideString(params[0]);
579 if (nSize > 1)
580 bFDF = pRuntime->ToBoolean(params[1]);
581 if (nSize > 2)
582 bEmpty = pRuntime->ToBoolean(params[2]);
583 if (nSize > 3)
584 aFields = pRuntime->ToArray(params[3]);
585 } else if (params[0]->IsObject()) {
586 v8::Local<v8::Object> pObj = pRuntime->ToObject(params[0]);
587 v8::Local<v8::Value> pValue = pRuntime->GetObjectProperty(pObj, "cURL");
588 if (!pValue.IsEmpty())
589 strURL = pRuntime->ToWideString(pValue);
590
591 bFDF = pRuntime->ToBoolean(pRuntime->GetObjectProperty(pObj, "bFDF"));
592 bEmpty = pRuntime->ToBoolean(pRuntime->GetObjectProperty(pObj, "bEmpty"));
593 aFields = pRuntime->ToArray(pRuntime->GetObjectProperty(pObj, "aFields"));
594 }
595
596 CPDF_InteractiveForm* pPDFForm = GetCoreInteractiveForm();
597 if (pRuntime->GetArrayLength(aFields) == 0 && bEmpty) {
598 if (pPDFForm->CheckRequiredFields(nullptr, true)) {
599 pRuntime->BeginBlock();
600 GetSDKInteractiveForm()->SubmitForm(strURL);
601 pRuntime->EndBlock();
602 }
604 }
605
606 std::vector<CPDF_FormField*> fieldObjects;
607 for (size_t i = 0; i < pRuntime->GetArrayLength(aFields); ++i) {
608 WideString sName =
609 pRuntime->ToWideString(pRuntime->GetArrayElement(aFields, i));
610 const size_t jsz = pPDFForm->CountFields(sName);
611 for (size_t j = 0; j < jsz; ++j) {
612 CPDF_FormField* pField = pPDFForm->GetField(j, sName);
613 if (!bEmpty && pField->GetValue().IsEmpty())
614 continue;
615
616 fieldObjects.push_back(pField);
617 }
618 }
619
620 if (pPDFForm->CheckRequiredFields(&fieldObjects, true)) {
621 pRuntime->BeginBlock();
622 GetSDKInteractiveForm()->SubmitFields(strURL, fieldObjects, true, !bFDF);
623 pRuntime->EndBlock();
624 }
626}
627
628void CJS_Document::SetFormFillEnv(CPDFSDK_FormFillEnvironment* pFormFillEnv) {
629 m_pFormFillEnv.Reset(pFormFillEnv);
630}
631
632CJS_Result CJS_Document::get_bookmark_root(CJS_Runtime* pRuntime) {
634}
635
636CJS_Result CJS_Document::set_bookmark_root(CJS_Runtime* pRuntime,
637 v8::Local<v8::Value> vp) {
639}
640
641CJS_Result CJS_Document::get_author(CJS_Runtime* pRuntime) {
642 return getPropertyInternal(pRuntime, "Author");
643}
644
645CJS_Result CJS_Document::set_author(CJS_Runtime* pRuntime,
646 v8::Local<v8::Value> vp) {
647 // Read-only.
649}
650
651CJS_Result CJS_Document::get_info(CJS_Runtime* pRuntime) {
652 if (!m_pFormFillEnv)
654
655 RetainPtr<const CPDF_Dictionary> pDictionary =
656 m_pFormFillEnv->GetPDFDocument()->GetInfo();
657 if (!pDictionary)
659
660 WideString cwAuthor = pDictionary->GetUnicodeTextFor("Author");
661 WideString cwTitle = pDictionary->GetUnicodeTextFor("Title");
662 WideString cwSubject = pDictionary->GetUnicodeTextFor("Subject");
663 WideString cwKeywords = pDictionary->GetUnicodeTextFor("Keywords");
664 WideString cwCreator = pDictionary->GetUnicodeTextFor("Creator");
665 WideString cwProducer = pDictionary->GetUnicodeTextFor("Producer");
666 WideString cwCreationDate = pDictionary->GetUnicodeTextFor("CreationDate");
667 WideString cwModDate = pDictionary->GetUnicodeTextFor("ModDate");
668 WideString cwTrapped = pDictionary->GetUnicodeTextFor("Trapped");
669
670 v8::Local<v8::Object> pObj = pRuntime->NewObject();
671 pRuntime->PutObjectProperty(pObj, "Author",
672 pRuntime->NewString(cwAuthor.AsStringView()));
673 pRuntime->PutObjectProperty(pObj, "Title",
674 pRuntime->NewString(cwTitle.AsStringView()));
675 pRuntime->PutObjectProperty(pObj, "Subject",
676 pRuntime->NewString(cwSubject.AsStringView()));
677 pRuntime->PutObjectProperty(pObj, "Keywords",
678 pRuntime->NewString(cwKeywords.AsStringView()));
679 pRuntime->PutObjectProperty(pObj, "Creator",
680 pRuntime->NewString(cwCreator.AsStringView()));
681 pRuntime->PutObjectProperty(pObj, "Producer",
682 pRuntime->NewString(cwProducer.AsStringView()));
683 pRuntime->PutObjectProperty(
684 pObj, "CreationDate", pRuntime->NewString(cwCreationDate.AsStringView()));
685 pRuntime->PutObjectProperty(pObj, "ModDate",
686 pRuntime->NewString(cwModDate.AsStringView()));
687 pRuntime->PutObjectProperty(pObj, "Trapped",
688 pRuntime->NewString(cwTrapped.AsStringView()));
689
690 // PutObjectProperty() calls below may re-enter JS and change info dict.
691 CPDF_DictionaryLocker locker(ToDictionary(pDictionary->Clone()));
692 for (const auto& it : locker) {
693 const ByteString& bsKey = it.first;
694 const RetainPtr<CPDF_Object>& pValueObj = it.second;
695 if (pValueObj->IsString() || pValueObj->IsName()) {
696 pRuntime->PutObjectProperty(
697 pObj, bsKey.AsStringView(),
698 pRuntime->NewString(pValueObj->GetUnicodeText().AsStringView()));
699 } else if (pValueObj->IsNumber()) {
700 pRuntime->PutObjectProperty(pObj, bsKey.AsStringView(),
701 pRuntime->NewNumber(pValueObj->GetNumber()));
702 } else if (pValueObj->IsBoolean()) {
703 pRuntime->PutObjectProperty(
704 pObj, bsKey.AsStringView(),
705 pRuntime->NewBoolean(!!pValueObj->GetInteger()));
706 }
707 }
708 return CJS_Result::Success(pObj);
709}
710
711CJS_Result CJS_Document::set_info(CJS_Runtime* pRuntime,
712 v8::Local<v8::Value> vp) {
714}
715
716CJS_Result CJS_Document::getPropertyInternal(CJS_Runtime* pRuntime,
717 const ByteString& propName) {
718 if (!m_pFormFillEnv)
720
721 RetainPtr<CPDF_Dictionary> pDictionary =
722 m_pFormFillEnv->GetPDFDocument()->GetInfo();
723 if (!pDictionary)
725
726 return CJS_Result::Success(pRuntime->NewString(
727 pDictionary->GetUnicodeTextFor(propName).AsStringView()));
728}
729
730CJS_Result CJS_Document::get_creation_date(CJS_Runtime* pRuntime) {
731 return getPropertyInternal(pRuntime, "CreationDate");
732}
733
734CJS_Result CJS_Document::set_creation_date(CJS_Runtime* pRuntime,
735 v8::Local<v8::Value> vp) {
736 // Read-only.
738}
739
740CJS_Result CJS_Document::get_creator(CJS_Runtime* pRuntime) {
741 return getPropertyInternal(pRuntime, "Creator");
742}
743
744CJS_Result CJS_Document::set_creator(CJS_Runtime* pRuntime,
745 v8::Local<v8::Value> vp) {
746 // Read-only.
748}
749
750CJS_Result CJS_Document::get_delay(CJS_Runtime* pRuntime) {
751 if (!m_pFormFillEnv)
753 return CJS_Result::Success(pRuntime->NewBoolean(m_bDelay));
754}
755
756CJS_Result CJS_Document::set_delay(CJS_Runtime* pRuntime,
757 v8::Local<v8::Value> vp) {
758 if (!m_pFormFillEnv)
760
761 using pdfium::access_permissions::kModifyContent;
762 if (!m_pFormFillEnv->HasPermissions(kModifyContent))
764
765 m_bDelay = pRuntime->ToBoolean(vp);
766 if (m_bDelay) {
767 m_DelayData.clear();
769 }
770
771 std::list<std::unique_ptr<CJS_DelayData>> DelayDataToProcess;
772 DelayDataToProcess.swap(m_DelayData);
773 for (const auto& pData : DelayDataToProcess)
774 CJS_Field::DoDelay(m_pFormFillEnv.Get(), pData.get());
775
777}
778
779CJS_Result CJS_Document::get_keywords(CJS_Runtime* pRuntime) {
780 return getPropertyInternal(pRuntime, "Keywords");
781}
782
783CJS_Result CJS_Document::set_keywords(CJS_Runtime* pRuntime,
784 v8::Local<v8::Value> vp) {
785 // Read-only.
787}
788
789CJS_Result CJS_Document::get_mod_date(CJS_Runtime* pRuntime) {
790 return getPropertyInternal(pRuntime, "ModDate");
791}
792
793CJS_Result CJS_Document::set_mod_date(CJS_Runtime* pRuntime,
794 v8::Local<v8::Value> vp) {
795 // Read-only.
797}
798
799CJS_Result CJS_Document::get_producer(CJS_Runtime* pRuntime) {
800 return getPropertyInternal(pRuntime, "Producer");
801}
802
803CJS_Result CJS_Document::set_producer(CJS_Runtime* pRuntime,
804 v8::Local<v8::Value> vp) {
805 // Read-only.
807}
808
809CJS_Result CJS_Document::get_subject(CJS_Runtime* pRuntime) {
810 return getPropertyInternal(pRuntime, "Subject");
811}
812
813CJS_Result CJS_Document::set_subject(CJS_Runtime* pRuntime,
814 v8::Local<v8::Value> vp) {
815 // Read-only.
817}
818
819CJS_Result CJS_Document::get_title(CJS_Runtime* pRuntime) {
820 if (!m_pFormFillEnv)
822 return getPropertyInternal(pRuntime, "Title");
823}
824
825CJS_Result CJS_Document::set_title(CJS_Runtime* pRuntime,
826 v8::Local<v8::Value> vp) {
827 // Read-only.
829}
830
831CJS_Result CJS_Document::get_num_pages(CJS_Runtime* pRuntime) {
832 if (!m_pFormFillEnv)
834 return CJS_Result::Success(
835 pRuntime->NewNumber(m_pFormFillEnv->GetPageCount()));
836}
837
838CJS_Result CJS_Document::set_num_pages(CJS_Runtime* pRuntime,
839 v8::Local<v8::Value> vp) {
841}
842
843CJS_Result CJS_Document::get_external(CJS_Runtime* pRuntime) {
844 // In Chrome case, should always return true.
845 return CJS_Result::Success(pRuntime->NewBoolean(true));
846}
847
848CJS_Result CJS_Document::set_external(CJS_Runtime* pRuntime,
849 v8::Local<v8::Value> vp) {
851}
852
853CJS_Result CJS_Document::get_filesize(CJS_Runtime* pRuntime) {
854 return CJS_Result::Success(pRuntime->NewNumber(0));
855}
856
857CJS_Result CJS_Document::set_filesize(CJS_Runtime* pRuntime,
858 v8::Local<v8::Value> vp) {
860}
861
862CJS_Result CJS_Document::get_mouse_x(CJS_Runtime* pRuntime) {
864}
865
866CJS_Result CJS_Document::set_mouse_x(CJS_Runtime* pRuntime,
867 v8::Local<v8::Value> vp) {
869}
870
871CJS_Result CJS_Document::get_mouse_y(CJS_Runtime* pRuntime) {
873}
874
875CJS_Result CJS_Document::set_mouse_y(CJS_Runtime* pRuntime,
876 v8::Local<v8::Value> vp) {
878}
879
880CJS_Result CJS_Document::get_URL(CJS_Runtime* pRuntime) {
881 if (!m_pFormFillEnv)
883 return CJS_Result::Success(
884 pRuntime->NewString(m_pFormFillEnv->JS_docGetFilePath().AsStringView()));
885}
886
887CJS_Result CJS_Document::set_URL(CJS_Runtime* pRuntime,
888 v8::Local<v8::Value> vp) {
890}
891
892CJS_Result CJS_Document::get_base_URL(CJS_Runtime* pRuntime) {
893 return CJS_Result::Success(pRuntime->NewString(m_cwBaseURL.AsStringView()));
894}
895
896CJS_Result CJS_Document::set_base_URL(CJS_Runtime* pRuntime,
897 v8::Local<v8::Value> vp) {
898 m_cwBaseURL = pRuntime->ToWideString(vp);
900}
901
902CJS_Result CJS_Document::get_calculate(CJS_Runtime* pRuntime) {
903 if (!m_pFormFillEnv)
905
906 CPDFSDK_InteractiveForm* pInteractiveForm = GetSDKInteractiveForm();
908 pRuntime->NewBoolean(!!pInteractiveForm->IsCalculateEnabled()));
909}
910
911CJS_Result CJS_Document::set_calculate(CJS_Runtime* pRuntime,
912 v8::Local<v8::Value> vp) {
913 if (!m_pFormFillEnv)
915
916 CPDFSDK_InteractiveForm* pInteractiveForm = GetSDKInteractiveForm();
917 pInteractiveForm->EnableCalculate(pRuntime->ToBoolean(vp));
919}
920
921CJS_Result CJS_Document::get_document_file_name(CJS_Runtime* pRuntime) {
922 if (!m_pFormFillEnv)
924
925 WideString wsFilePath = m_pFormFillEnv->JS_docGetFilePath();
926 size_t i = wsFilePath.GetLength();
927 for (; i > 0; i--) {
928 if (wsFilePath[i - 1] == L'\\' || wsFilePath[i - 1] == L'/')
929 break;
930 }
931 if (i > 0 && i < wsFilePath.GetLength()) {
933 pRuntime->NewString(wsFilePath.AsStringView().Substr(i)));
934 }
935 return CJS_Result::Success(pRuntime->NewString(""));
936}
937
938CJS_Result CJS_Document::set_document_file_name(CJS_Runtime* pRuntime,
939 v8::Local<v8::Value> vp) {
941}
942
943CJS_Result CJS_Document::get_path(CJS_Runtime* pRuntime) {
944 if (!m_pFormFillEnv)
946 return CJS_Result::Success(pRuntime->NewString(
947 CJS_App::SysPathToPDFPath(m_pFormFillEnv->JS_docGetFilePath())
948 .AsStringView()));
949}
950
951CJS_Result CJS_Document::set_path(CJS_Runtime* pRuntime,
952 v8::Local<v8::Value> vp) {
954}
955
956CJS_Result CJS_Document::get_page_window_rect(CJS_Runtime* pRuntime) {
958}
959
960CJS_Result CJS_Document::set_page_window_rect(CJS_Runtime* pRuntime,
961 v8::Local<v8::Value> vp) {
963}
964
965CJS_Result CJS_Document::get_layout(CJS_Runtime* pRuntime) {
967}
968
969CJS_Result CJS_Document::set_layout(CJS_Runtime* pRuntime,
970 v8::Local<v8::Value> vp) {
972}
973
974CJS_Result CJS_Document::addLink(CJS_Runtime* pRuntime,
975 pdfium::span<v8::Local<v8::Value>> params) {
977}
978
979CJS_Result CJS_Document::closeDoc(CJS_Runtime* pRuntime,
980 pdfium::span<v8::Local<v8::Value>> params) {
982}
983
984CJS_Result CJS_Document::getPageBox(CJS_Runtime* pRuntime,
985 pdfium::span<v8::Local<v8::Value>> params) {
987}
988
989CJS_Result CJS_Document::getAnnot(CJS_Runtime* pRuntime,
990 pdfium::span<v8::Local<v8::Value>> params) {
991 if (params.size() != 2)
993 if (!m_pFormFillEnv)
995
996 int nPageNo = pRuntime->ToInt32(params[0]);
997 WideString swAnnotName = pRuntime->ToWideString(params[1]);
998 CPDFSDK_PageView* pPageView = m_pFormFillEnv->GetPageViewAtIndex(nPageNo);
999 if (!pPageView)
1001
1002 CPDFSDK_AnnotIteration annot_iteration(pPageView);
1003 CPDFSDK_BAAnnot* pSDKBAAnnot = nullptr;
1004 for (const auto& pSDKAnnotCur : annot_iteration) {
1005 auto* pBAAnnot = pSDKAnnotCur->AsBAAnnot();
1006 if (pBAAnnot && pBAAnnot->GetAnnotName() == swAnnotName) {
1007 pSDKBAAnnot = pBAAnnot;
1008 break;
1009 }
1010 }
1011 if (!pSDKBAAnnot)
1013
1014 v8::Local<v8::Object> pObj = pRuntime->NewFXJSBoundObject(
1015 CJS_Annot::GetObjDefnID(), FXJSOBJTYPE_DYNAMIC);
1016 if (pObj.IsEmpty())
1018
1019 auto* pJS_Annot = static_cast<CJS_Annot*>(
1020 CFXJS_Engine::GetObjectPrivate(pRuntime->GetIsolate(), pObj));
1021 if (!pJS_Annot)
1023
1024 pJS_Annot->SetSDKAnnot(pSDKBAAnnot);
1025 return CJS_Result::Success(pJS_Annot->ToV8Object());
1026}
1027
1028CJS_Result CJS_Document::getAnnots(CJS_Runtime* pRuntime,
1029 pdfium::span<v8::Local<v8::Value>> params) {
1030 if (!m_pFormFillEnv)
1032
1033 // TODO(tonikitoo): Add support supported parameters as per
1034 // the PDF spec.
1035
1036 int nPageNo = m_pFormFillEnv->GetPageCount();
1037 v8::Local<v8::Array> annots = pRuntime->NewArray();
1038 for (int i = 0; i < nPageNo; ++i) {
1039 CPDFSDK_PageView* pPageView = m_pFormFillEnv->GetPageViewAtIndex(i);
1040 if (!pPageView)
1042
1043 CPDFSDK_AnnotIteration annot_iteration(pPageView);
1044 for (const auto& pSDKAnnotCur : annot_iteration) {
1045 if (!pSDKAnnotCur)
1046 return CJS_Result::Failure(JSMessage::kBadObjectError);
1047
1048 v8::Local<v8::Object> pObj = pRuntime->NewFXJSBoundObject(
1049 CJS_Annot::GetObjDefnID(), FXJSOBJTYPE_DYNAMIC);
1050 if (pObj.IsEmpty())
1051 return CJS_Result::Failure(JSMessage::kBadObjectError);
1052
1053 auto* pJS_Annot = static_cast<CJS_Annot*>(
1054 CFXJS_Engine::GetObjectPrivate(pRuntime->GetIsolate(), pObj));
1055 pJS_Annot->SetSDKAnnot(pSDKAnnotCur->AsBAAnnot());
1056 pRuntime->PutArrayElement(
1057 annots, i,
1058 pJS_Annot ? v8::Local<v8::Value>(pJS_Annot->ToV8Object())
1059 : v8::Local<v8::Value>());
1060 }
1061 }
1062 return CJS_Result::Success(annots);
1063}
1064
1065CJS_Result CJS_Document::getAnnot3D(CJS_Runtime* pRuntime,
1066 pdfium::span<v8::Local<v8::Value>> params) {
1067 return CJS_Result::Success(pRuntime->NewUndefined());
1068}
1069
1070CJS_Result CJS_Document::getAnnots3D(
1071 CJS_Runtime* pRuntime,
1072 pdfium::span<v8::Local<v8::Value>> params) {
1073 return CJS_Result::Success();
1074}
1075
1076CJS_Result CJS_Document::getOCGs(CJS_Runtime* pRuntime,
1077 pdfium::span<v8::Local<v8::Value>> params) {
1078 return CJS_Result::Success();
1079}
1080
1081CJS_Result CJS_Document::getLinks(CJS_Runtime* pRuntime,
1082 pdfium::span<v8::Local<v8::Value>> params) {
1083 return CJS_Result::Success();
1084}
1085
1086CJS_Result CJS_Document::addIcon(CJS_Runtime* pRuntime,
1087 pdfium::span<v8::Local<v8::Value>> params) {
1088 if (params.size() != 2)
1090
1091 if (!params[1]->IsObject())
1093
1094 v8::Local<v8::Object> pObj = pRuntime->ToObject(params[1]);
1095 if (!JSGetObject<CJS_Icon>(pRuntime->GetIsolate(), pObj))
1097
1098 WideString swIconName = pRuntime->ToWideString(params[0]);
1099 m_IconNames.push_back(swIconName);
1100 return CJS_Result::Success();
1101}
1102
1103CJS_Result CJS_Document::get_icons(CJS_Runtime* pRuntime) {
1104 // TODO(tsepez): Maybe make consistent with Acrobat Reader behavior which
1105 // is to throw an exception under the default security settings.
1106 if (m_IconNames.empty())
1107 return CJS_Result::Success(pRuntime->NewUndefined());
1108
1109 v8::Local<v8::Array> Icons = pRuntime->NewArray();
1110 int i = 0;
1111 for (const auto& name : m_IconNames) {
1112 v8::Local<v8::Object> pObj = pRuntime->NewFXJSBoundObject(
1113 CJS_Icon::GetObjDefnID(), FXJSOBJTYPE_DYNAMIC);
1114 if (pObj.IsEmpty())
1115 return CJS_Result::Failure(JSMessage::kBadObjectError);
1116
1117 auto* pJS_Icon = static_cast<CJS_Icon*>(
1118 CFXJS_Engine::GetObjectPrivate(pRuntime->GetIsolate(), pObj));
1119 pJS_Icon->SetIconName(name);
1120 pRuntime->PutArrayElement(Icons, i++,
1121 pJS_Icon
1122 ? v8::Local<v8::Value>(pJS_Icon->ToV8Object())
1123 : v8::Local<v8::Value>());
1124 }
1125 return CJS_Result::Success(Icons);
1126}
1127
1128CJS_Result CJS_Document::set_icons(CJS_Runtime* pRuntime,
1129 v8::Local<v8::Value> vp) {
1131}
1132
1133CJS_Result CJS_Document::getIcon(CJS_Runtime* pRuntime,
1134 pdfium::span<v8::Local<v8::Value>> params) {
1135 if (params.size() != 1)
1137
1138 WideString swIconName = pRuntime->ToWideString(params[0]);
1139 auto it = std::find(m_IconNames.begin(), m_IconNames.end(), swIconName);
1140 if (it == m_IconNames.end())
1142
1143 v8::Local<v8::Object> pObj = pRuntime->NewFXJSBoundObject(
1144 CJS_Icon::GetObjDefnID(), FXJSOBJTYPE_DYNAMIC);
1145 if (pObj.IsEmpty())
1147
1148 auto* pJSIcon = static_cast<CJS_Icon*>(
1149 CFXJS_Engine::GetObjectPrivate(pRuntime->GetIsolate(), pObj));
1150 if (!pJSIcon)
1152
1153 pJSIcon->SetIconName(*it);
1154 return CJS_Result::Success(pJSIcon->ToV8Object());
1155}
1156
1157CJS_Result CJS_Document::removeIcon(CJS_Runtime* pRuntime,
1158 pdfium::span<v8::Local<v8::Value>> params) {
1159 // Unsafe, no supported.
1160 return CJS_Result::Success();
1161}
1162
1163CJS_Result CJS_Document::createDataObject(
1164 CJS_Runtime* pRuntime,
1165 pdfium::span<v8::Local<v8::Value>> params) {
1166 // Unsafe, not implemented.
1167 return CJS_Result::Success();
1168}
1169
1170CJS_Result CJS_Document::get_media(CJS_Runtime* pRuntime) {
1171 return CJS_Result::Success();
1172}
1173
1174CJS_Result CJS_Document::set_media(CJS_Runtime* pRuntime,
1175 v8::Local<v8::Value> vp) {
1176 return CJS_Result::Success();
1177}
1178
1179CJS_Result CJS_Document::calculateNow(
1180 CJS_Runtime* pRuntime,
1181 pdfium::span<v8::Local<v8::Value>> params) {
1182 if (!m_pFormFillEnv)
1184
1185 if (!m_pFormFillEnv->HasPermissions(
1186 pdfium::access_permissions::kModifyContent |
1187 pdfium::access_permissions::kModifyAnnotation |
1188 pdfium::access_permissions::kFillForm)) {
1190 }
1191
1192 GetSDKInteractiveForm()->OnCalculate(nullptr);
1193 return CJS_Result::Success();
1194}
1195
1196CJS_Result CJS_Document::get_collab(CJS_Runtime* pRuntime) {
1197 return CJS_Result::Success();
1198}
1199
1200CJS_Result CJS_Document::set_collab(CJS_Runtime* pRuntime,
1201 v8::Local<v8::Value> vp) {
1202 return CJS_Result::Success();
1203}
1204
1205CJS_Result CJS_Document::getPageNthWord(
1206 CJS_Runtime* pRuntime,
1207 pdfium::span<v8::Local<v8::Value>> params) {
1208 if (!m_pFormFillEnv)
1210
1211 using pdfium::access_permissions::kExtractForAccessibility;
1212 if (!m_pFormFillEnv->HasPermissions(kExtractForAccessibility))
1214
1215 // TODO(tsepez): check maximum allowable params.
1216
1217 int nPageNo = params.size() > 0 ? pRuntime->ToInt32(params[0]) : 0;
1218 int nWordNo = params.size() > 1 ? pRuntime->ToInt32(params[1]) : 0;
1219 bool bStrip = params.size() > 2 ? pRuntime->ToBoolean(params[2]) : true;
1220
1221 CPDF_Document* pDocument = m_pFormFillEnv->GetPDFDocument();
1222 if (nPageNo < 0 || nPageNo >= pDocument->GetPageCount())
1224
1225 RetainPtr<CPDF_Dictionary> pPageDict =
1226 pDocument->GetMutablePageDictionary(nPageNo);
1227 if (!pPageDict)
1229
1230 auto page = pdfium::MakeRetain<CPDF_Page>(pDocument, std::move(pPageDict));
1231 page->AddPageImageCache();
1232 page->ParseContent();
1233
1234 int nWords = 0;
1235 WideString swRet;
1236 for (auto& pPageObj : *page) {
1237 if (pPageObj->IsText()) {
1238 CPDF_TextObject* pTextObj = pPageObj->AsText();
1239 int nObjWords = pTextObj->CountWords();
1240 if (nWords + nObjWords >= nWordNo) {
1241 swRet = pTextObj->GetWordString(nWordNo - nWords);
1242 break;
1243 }
1244 nWords += nObjWords;
1245 }
1246 }
1247
1248 if (bStrip)
1249 swRet.Trim();
1250 return CJS_Result::Success(pRuntime->NewString(swRet.AsStringView()));
1251}
1252
1253CJS_Result CJS_Document::getPageNthWordQuads(
1254 CJS_Runtime* pRuntime,
1255 pdfium::span<v8::Local<v8::Value>> params) {
1256 if (!m_pFormFillEnv)
1258
1259 using pdfium::access_permissions::kExtractForAccessibility;
1260 if (!m_pFormFillEnv->HasPermissions(kExtractForAccessibility))
1262
1264}
1265
1266CJS_Result CJS_Document::getPageNumWords(
1267 CJS_Runtime* pRuntime,
1268 pdfium::span<v8::Local<v8::Value>> params) {
1269 if (!m_pFormFillEnv)
1271
1272 using pdfium::access_permissions::kExtractForAccessibility;
1273 if (!m_pFormFillEnv->HasPermissions(kExtractForAccessibility))
1275
1276 int nPageNo = params.size() > 0 ? pRuntime->ToInt32(params[0]) : 0;
1277 CPDF_Document* pDocument = m_pFormFillEnv->GetPDFDocument();
1278 if (nPageNo < 0 || nPageNo >= pDocument->GetPageCount())
1280
1281 RetainPtr<CPDF_Dictionary> pPageDict =
1282 pDocument->GetMutablePageDictionary(nPageNo);
1283 if (!pPageDict)
1285
1286 auto page = pdfium::MakeRetain<CPDF_Page>(pDocument, std::move(pPageDict));
1287 page->AddPageImageCache();
1288 page->ParseContent();
1289
1290 int nWords = 0;
1291 for (auto& pPageObj : *page) {
1292 if (pPageObj->IsText())
1293 nWords += pPageObj->AsText()->CountWords();
1294 }
1295 return CJS_Result::Success(pRuntime->NewNumber(nWords));
1296}
1297
1298CJS_Result CJS_Document::getPrintParams(
1299 CJS_Runtime* pRuntime,
1300 pdfium::span<v8::Local<v8::Value>> params) {
1302}
1303
1304CJS_Result CJS_Document::get_zoom(CJS_Runtime* pRuntime) {
1305 return CJS_Result::Success();
1306}
1307
1308CJS_Result CJS_Document::set_zoom(CJS_Runtime* pRuntime,
1309 v8::Local<v8::Value> vp) {
1310 return CJS_Result::Success();
1311}
1312
1313CJS_Result CJS_Document::get_zoom_type(CJS_Runtime* pRuntime) {
1314 return CJS_Result::Success();
1315}
1316
1317CJS_Result CJS_Document::set_zoom_type(CJS_Runtime* pRuntime,
1318 v8::Local<v8::Value> vp) {
1319 return CJS_Result::Success();
1320}
1321
1322CJS_Result CJS_Document::deletePages(
1323 CJS_Runtime* pRuntime,
1324 pdfium::span<v8::Local<v8::Value>> params) {
1325 // Unsafe, not supported.
1326 return CJS_Result::Success();
1327}
1328
1329CJS_Result CJS_Document::extractPages(
1330 CJS_Runtime* pRuntime,
1331 pdfium::span<v8::Local<v8::Value>> params) {
1332 // Unsafe, not supported.
1333 return CJS_Result::Success();
1334}
1335
1336CJS_Result CJS_Document::insertPages(
1337 CJS_Runtime* pRuntime,
1338 pdfium::span<v8::Local<v8::Value>> params) {
1339 // Unsafe, not supported.
1340 return CJS_Result::Success();
1341}
1342
1343CJS_Result CJS_Document::replacePages(
1344 CJS_Runtime* pRuntime,
1345 pdfium::span<v8::Local<v8::Value>> params) {
1346 // Unsafe, not supported.
1347 return CJS_Result::Success();
1348}
1349
1350CJS_Result CJS_Document::getURL(CJS_Runtime* pRuntime,
1351 pdfium::span<v8::Local<v8::Value>> params) {
1352 // Unsafe, not supported.
1353 return CJS_Result::Success();
1354}
1355
1356CJS_Result CJS_Document::gotoNamedDest(
1357 CJS_Runtime* pRuntime,
1358 pdfium::span<v8::Local<v8::Value>> params) {
1359 if (params.size() != 1)
1361
1362 if (!m_pFormFillEnv)
1364
1365 CPDF_Document* pDocument = m_pFormFillEnv->GetPDFDocument();
1366 RetainPtr<const CPDF_Array> dest_array = CPDF_NameTree::LookupNamedDest(
1367 pDocument, pRuntime->ToByteString(params[0]));
1368 if (!dest_array)
1370
1371 CPDF_Dest dest(std::move(dest_array));
1372 std::vector<float> positions = dest.GetScrollPositionArray();
1373 pRuntime->BeginBlock();
1374 m_pFormFillEnv->DoGoToAction(dest.GetDestPageIndex(pDocument),
1375 dest.GetZoomMode(), positions);
1376 pRuntime->EndBlock();
1377 return CJS_Result::Success();
1378}
1379
1380void CJS_Document::AddDelayData(std::unique_ptr<CJS_DelayData> pData) {
1381 m_DelayData.push_back(std::move(pData));
1382}
1383
1384void CJS_Document::DoFieldDelay(const WideString& sFieldName,
1385 int nControlIndex) {
1386 std::vector<std::unique_ptr<CJS_DelayData>> delayed_data;
1387 auto iter = m_DelayData.begin();
1388 while (iter != m_DelayData.end()) {
1389 auto old = iter++;
1390 if ((*old)->sFieldName == sFieldName &&
1391 (*old)->nControlIndex == nControlIndex) {
1392 delayed_data.push_back(std::move(*old));
1393 m_DelayData.erase(old);
1394 }
1395 }
1396
1397 for (const auto& pData : delayed_data)
1398 CJS_Field::DoDelay(m_pFormFillEnv.Get(), pData.get());
1399}
1400
1401CPDF_InteractiveForm* CJS_Document::GetCoreInteractiveForm() {
1402 return GetSDKInteractiveForm()->GetInteractiveForm();
1403}
1404
1405CPDFSDK_InteractiveForm* CJS_Document::GetSDKInteractiveForm() {
1406 return m_pFormFillEnv->GetInteractiveForm();
1407}
@ FXJSOBJTYPE_GLOBAL
void DoFieldDelay(const WideString &sFieldName, int nControlIndex)
CJS_Document(v8::Local< v8::Object > pObject, CJS_Runtime *pRuntime)
void SetFormFillEnv(CPDFSDK_FormFillEnvironment *pFormFillEnv)
~CJS_Document() override
void AddDelayData(std::unique_ptr< CJS_DelayData > pData)
static uint32_t GetObjDefnID()
static void DefineJSObjects(CFXJS_Engine *pEngine)
static void DefineProps(CFXJS_Engine *pEngine, uint32_t nObjDefnID, pdfium::span< const JSPropertySpec > consts)
CJS_Runtime * GetRuntime() const
Definition cjs_object.h:54
static void DefineMethods(CFXJS_Engine *pEngine, uint32_t nObjDefnID, pdfium::span< const JSMethodSpec > consts)
static CJS_Result Success()
Definition cjs_result.h:27
static CJS_Result Failure(JSMessage id)
Definition cjs_result.h:34
static CJS_Result Failure(const WideString &str)
Definition cjs_result.h:31
void BeginBlock()
Definition cjs_runtime.h:47
void EndBlock()
Definition cjs_runtime.h:48
CJS_EventContext * GetCurrentEventContext() const
CPDFSDK_AnnotIteration(CPDFSDK_PageView *page_view)
bool SubmitForm(const WideString &sDestination)
CPDF_InteractiveForm * GetInteractiveForm() const
void OnCalculate(CPDF_FormField *pFormField)
int GetPageCount() const
WideString GetValue() const
WideString GetFullName() const
bool IsEmpty() const
Definition bytestring.h:119
bool IsEmpty() const
Definition widestring.h:118
JSMessage
@ kPermissionError
@ kNotSupportedError
@ kUserGestureRequiredError