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 "core/fxcrt/check.h"
24#include "core/fxcrt/span.h"
25#include "fpdfsdk/cpdfsdk_annotiteration.h"
26#include "fpdfsdk/cpdfsdk_formfillenvironment.h"
27#include "fpdfsdk/cpdfsdk_interactiveform.h"
28#include "fpdfsdk/cpdfsdk_pageview.h"
29#include "fxjs/cjs_annot.h"
30#include "fxjs/cjs_app.h"
31#include "fxjs/cjs_delaydata.h"
32#include "fxjs/cjs_event_context.h"
33#include "fxjs/cjs_field.h"
34#include "fxjs/cjs_icon.h"
35#include "fxjs/js_resources.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::GetBinding(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(WideString::FromASCII("Bad FDF format."));
368 }
369
370 v8::LocalVector<v8::Value> newParams = ExpandKeywordParams(
371 pRuntime, params, 6, "bUI", "cTo", "cCc", "cBcc", "cSubject", "cMsg");
372
373 bool bUI = true;
374 if (IsExpandedParamKnown(newParams[0]))
375 bUI = pRuntime->ToBoolean(newParams[0]);
376
377 WideString cTo;
378 if (IsExpandedParamKnown(newParams[1]))
379 cTo = pRuntime->ToWideString(newParams[1]);
380
381 WideString cCc;
382 if (IsExpandedParamKnown(newParams[2]))
383 cCc = pRuntime->ToWideString(newParams[2]);
384
385 WideString cBcc;
386 if (IsExpandedParamKnown(newParams[3]))
387 cBcc = pRuntime->ToWideString(newParams[3]);
388
389 WideString cSubject;
390 if (IsExpandedParamKnown(newParams[4]))
391 cSubject = pRuntime->ToWideString(newParams[4]);
392
393 WideString cMsg;
394 if (IsExpandedParamKnown(newParams[5]))
395 cMsg = pRuntime->ToWideString(newParams[5]);
396
397 pRuntime->BeginBlock();
398 m_pFormFillEnv->JS_docmailForm(sTextBuf.unsigned_span(), bUI, cTo, cSubject,
399 cCc, cBcc, cMsg);
400 pRuntime->EndBlock();
402}
403
404CJS_Result CJS_Document::print(CJS_Runtime* pRuntime,
405 pdfium::span<v8::Local<v8::Value>> params) {
406 v8::LocalVector<v8::Value> newParams = ExpandKeywordParams(
407 pRuntime, params, 8, "bUI", "nStart", "nEnd", "bSilent", "bShrinkToFit",
408 "bPrintAsImage", "bReverse", "bAnnotations");
409
410 bool bUI = true;
411 if (IsExpandedParamKnown(newParams[0]))
412 bUI = pRuntime->ToBoolean(newParams[0]);
413
414 int nStart = 0;
415 if (IsExpandedParamKnown(newParams[1]))
416 nStart = pRuntime->ToInt32(newParams[1]);
417
418 int nEnd = 0;
419 if (IsExpandedParamKnown(newParams[2]))
420 nEnd = pRuntime->ToInt32(newParams[2]);
421
422 bool bSilent = false;
423 if (IsExpandedParamKnown(newParams[3]))
424 bSilent = pRuntime->ToBoolean(newParams[3]);
425
426 bool bShrinkToFit = false;
427 if (IsExpandedParamKnown(newParams[4]))
428 bShrinkToFit = pRuntime->ToBoolean(newParams[4]);
429
430 bool bPrintAsImage = false;
431 if (IsExpandedParamKnown(newParams[5]))
432 bPrintAsImage = pRuntime->ToBoolean(newParams[5]);
433
434 bool bReverse = false;
435 if (IsExpandedParamKnown(newParams[6]))
436 bReverse = pRuntime->ToBoolean(newParams[6]);
437
438 bool bAnnotations = false;
439 if (IsExpandedParamKnown(newParams[7]))
440 bAnnotations = pRuntime->ToBoolean(newParams[7]);
441
442 if (!m_pFormFillEnv)
444
445 CJS_EventContext* pHandler = pRuntime->GetCurrentEventContext();
446 if (!pHandler->IsUserGesture())
448
449 m_pFormFillEnv->JS_docprint(bUI, nStart, nEnd, bSilent, bShrinkToFit,
450 bPrintAsImage, bReverse, bAnnotations);
452}
453
454// removes the specified field from the document.
455// comment:
456// note: if the filed name is not rational, adobe is dumb for it.
457CJS_Result CJS_Document::removeField(
458 CJS_Runtime* pRuntime,
459 pdfium::span<v8::Local<v8::Value>> params) {
460 if (params.size() != 1)
462 if (!m_pFormFillEnv)
464
465 if (!m_pFormFillEnv->HasPermissions(
466 pdfium::access_permissions::kModifyContent |
467 pdfium::access_permissions::kModifyAnnotation)) {
469 }
470
471 WideString sFieldName = pRuntime->ToWideString(params[0]);
472 CPDFSDK_InteractiveForm* pInteractiveForm = GetSDKInteractiveForm();
473 std::vector<ObservedPtr<CPDFSDK_Widget>> widgets;
474 pInteractiveForm->GetWidgets(sFieldName, &widgets);
475 if (widgets.empty())
477
478 for (const auto& pWidget : widgets) {
479 if (!pWidget)
480 continue;
481
482 IPDF_Page* pPage = pWidget->GetPage();
483 DCHECK(pPage);
484
485 // If there is currently no pageview associated with the page being used
486 // do not create one. We may be in the process of tearing down the document
487 // and creating a new pageview at this point will cause bad things.
488 CPDFSDK_PageView* pPageView = m_pFormFillEnv->GetPageView(pPage);
489 if (!pPageView)
490 continue;
491
492 CFX_FloatRect rcAnnot = pWidget->GetRect();
493 rcAnnot.Inflate(1.0f, 1.0f, 1.0f, 1.0f);
494
495 std::vector<CFX_FloatRect> aRefresh(1, rcAnnot);
496 pPageView->UpdateRects(aRefresh);
497 }
498 m_pFormFillEnv->SetChangeMark();
500}
501
502// reset filed values within a document.
503// comment:
504// note: if the fields names r not rational, aodbe is dumb for it.
505
506CJS_Result CJS_Document::resetForm(CJS_Runtime* pRuntime,
507 pdfium::span<v8::Local<v8::Value>> params) {
508 if (!m_pFormFillEnv)
510
511 if (!m_pFormFillEnv->HasPermissions(
512 pdfium::access_permissions::kModifyContent |
513 pdfium::access_permissions::kModifyAnnotation |
514 pdfium::access_permissions::kFillForm)) {
516 }
517
518 CPDF_InteractiveForm* pPDFForm = GetCoreInteractiveForm();
519 if (params.empty()) {
520 pPDFForm->ResetForm();
521 m_pFormFillEnv->SetChangeMark();
523 }
524
525 v8::Local<v8::Array> array;
526 if (params[0]->IsString()) {
527 array = pRuntime->NewArray();
528 pRuntime->PutArrayElement(array, 0, params[0]);
529 } else {
530 array = pRuntime->ToArray(params[0]);
531 }
532
533 std::vector<CPDF_FormField*> aFields;
534 for (size_t i = 0; i < pRuntime->GetArrayLength(array); ++i) {
535 WideString swVal =
536 pRuntime->ToWideString(pRuntime->GetArrayElement(array, i));
537 const size_t jsz = pPDFForm->CountFields(swVal);
538 for (size_t j = 0; j < jsz; ++j)
539 aFields.push_back(pPDFForm->GetField(j, swVal));
540 }
541
542 if (!aFields.empty()) {
543 pPDFForm->ResetForm(aFields, true);
544 m_pFormFillEnv->SetChangeMark();
545 }
546
548}
549
550CJS_Result CJS_Document::saveAs(CJS_Runtime* pRuntime,
551 pdfium::span<v8::Local<v8::Value>> params) {
552 // Unsafe, not supported.
554}
555
556CJS_Result CJS_Document::syncAnnotScan(
557 CJS_Runtime* pRuntime,
558 pdfium::span<v8::Local<v8::Value>> params) {
560}
561
562CJS_Result CJS_Document::submitForm(CJS_Runtime* pRuntime,
563 pdfium::span<v8::Local<v8::Value>> params) {
564 size_t nSize = params.size();
565 if (nSize < 1)
567 if (!m_pFormFillEnv)
569
570 CJS_EventContext* pHandler = pRuntime->GetCurrentEventContext();
571 if (!pHandler->IsUserGesture())
573
574 v8::Local<v8::Array> aFields;
575 WideString strURL;
576 bool bFDF = true;
577 bool bEmpty = false;
578 if (params[0]->IsString()) {
579 strURL = pRuntime->ToWideString(params[0]);
580 if (nSize > 1)
581 bFDF = pRuntime->ToBoolean(params[1]);
582 if (nSize > 2)
583 bEmpty = pRuntime->ToBoolean(params[2]);
584 if (nSize > 3)
585 aFields = pRuntime->ToArray(params[3]);
586 } else if (params[0]->IsObject()) {
587 v8::Local<v8::Object> pObj = pRuntime->ToObject(params[0]);
588 v8::Local<v8::Value> pValue = pRuntime->GetObjectProperty(pObj, "cURL");
589 if (!pValue.IsEmpty())
590 strURL = pRuntime->ToWideString(pValue);
591
592 bFDF = pRuntime->ToBoolean(pRuntime->GetObjectProperty(pObj, "bFDF"));
593 bEmpty = pRuntime->ToBoolean(pRuntime->GetObjectProperty(pObj, "bEmpty"));
594 aFields = pRuntime->ToArray(pRuntime->GetObjectProperty(pObj, "aFields"));
595 }
596
597 CPDF_InteractiveForm* pPDFForm = GetCoreInteractiveForm();
598 if (pRuntime->GetArrayLength(aFields) == 0 && bEmpty) {
599 if (pPDFForm->CheckRequiredFields(nullptr, true)) {
600 pRuntime->BeginBlock();
601 GetSDKInteractiveForm()->SubmitForm(strURL);
602 pRuntime->EndBlock();
603 }
605 }
606
607 std::vector<CPDF_FormField*> fieldObjects;
608 for (size_t i = 0; i < pRuntime->GetArrayLength(aFields); ++i) {
609 WideString sName =
610 pRuntime->ToWideString(pRuntime->GetArrayElement(aFields, i));
611 const size_t jsz = pPDFForm->CountFields(sName);
612 for (size_t j = 0; j < jsz; ++j) {
613 CPDF_FormField* pField = pPDFForm->GetField(j, sName);
614 if (!bEmpty && pField->GetValue().IsEmpty())
615 continue;
616
617 fieldObjects.push_back(pField);
618 }
619 }
620
621 if (pPDFForm->CheckRequiredFields(&fieldObjects, true)) {
622 pRuntime->BeginBlock();
623 GetSDKInteractiveForm()->SubmitFields(strURL, fieldObjects, true, !bFDF);
624 pRuntime->EndBlock();
625 }
627}
628
629void CJS_Document::SetFormFillEnv(CPDFSDK_FormFillEnvironment* pFormFillEnv) {
630 m_pFormFillEnv.Reset(pFormFillEnv);
631}
632
633CJS_Result CJS_Document::get_bookmark_root(CJS_Runtime* pRuntime) {
635}
636
637CJS_Result CJS_Document::set_bookmark_root(CJS_Runtime* pRuntime,
638 v8::Local<v8::Value> vp) {
640}
641
642CJS_Result CJS_Document::get_author(CJS_Runtime* pRuntime) {
643 return getPropertyInternal(pRuntime, "Author");
644}
645
646CJS_Result CJS_Document::set_author(CJS_Runtime* pRuntime,
647 v8::Local<v8::Value> vp) {
648 // Read-only.
650}
651
652CJS_Result CJS_Document::get_info(CJS_Runtime* pRuntime) {
653 if (!m_pFormFillEnv)
655
656 RetainPtr<const CPDF_Dictionary> pDictionary =
657 m_pFormFillEnv->GetPDFDocument()->GetInfo();
658 if (!pDictionary)
660
661 WideString cwAuthor = pDictionary->GetUnicodeTextFor("Author");
662 WideString cwTitle = pDictionary->GetUnicodeTextFor("Title");
663 WideString cwSubject = pDictionary->GetUnicodeTextFor("Subject");
664 WideString cwKeywords = pDictionary->GetUnicodeTextFor("Keywords");
665 WideString cwCreator = pDictionary->GetUnicodeTextFor("Creator");
666 WideString cwProducer = pDictionary->GetUnicodeTextFor("Producer");
667 WideString cwCreationDate = pDictionary->GetUnicodeTextFor("CreationDate");
668 WideString cwModDate = pDictionary->GetUnicodeTextFor("ModDate");
669 WideString cwTrapped = pDictionary->GetUnicodeTextFor("Trapped");
670
671 v8::Local<v8::Object> pObj = pRuntime->NewObject();
672 pRuntime->PutObjectProperty(pObj, "Author",
673 pRuntime->NewString(cwAuthor.AsStringView()));
674 pRuntime->PutObjectProperty(pObj, "Title",
675 pRuntime->NewString(cwTitle.AsStringView()));
676 pRuntime->PutObjectProperty(pObj, "Subject",
677 pRuntime->NewString(cwSubject.AsStringView()));
678 pRuntime->PutObjectProperty(pObj, "Keywords",
679 pRuntime->NewString(cwKeywords.AsStringView()));
680 pRuntime->PutObjectProperty(pObj, "Creator",
681 pRuntime->NewString(cwCreator.AsStringView()));
682 pRuntime->PutObjectProperty(pObj, "Producer",
683 pRuntime->NewString(cwProducer.AsStringView()));
684 pRuntime->PutObjectProperty(
685 pObj, "CreationDate", pRuntime->NewString(cwCreationDate.AsStringView()));
686 pRuntime->PutObjectProperty(pObj, "ModDate",
687 pRuntime->NewString(cwModDate.AsStringView()));
688 pRuntime->PutObjectProperty(pObj, "Trapped",
689 pRuntime->NewString(cwTrapped.AsStringView()));
690
691 // PutObjectProperty() calls below may re-enter JS and change info dict.
692 CPDF_DictionaryLocker locker(ToDictionary(pDictionary->Clone()));
693 for (const auto& it : locker) {
694 const ByteString& bsKey = it.first;
695 const RetainPtr<CPDF_Object>& pValueObj = it.second;
696 if (pValueObj->IsString() || pValueObj->IsName()) {
697 pRuntime->PutObjectProperty(
698 pObj, bsKey.AsStringView(),
699 pRuntime->NewString(pValueObj->GetUnicodeText().AsStringView()));
700 } else if (pValueObj->IsNumber()) {
701 pRuntime->PutObjectProperty(pObj, bsKey.AsStringView(),
702 pRuntime->NewNumber(pValueObj->GetNumber()));
703 } else if (pValueObj->IsBoolean()) {
704 pRuntime->PutObjectProperty(
705 pObj, bsKey.AsStringView(),
706 pRuntime->NewBoolean(!!pValueObj->GetInteger()));
707 }
708 }
709 return CJS_Result::Success(pObj);
710}
711
712CJS_Result CJS_Document::set_info(CJS_Runtime* pRuntime,
713 v8::Local<v8::Value> vp) {
715}
716
717CJS_Result CJS_Document::getPropertyInternal(CJS_Runtime* pRuntime,
718 const ByteString& propName) {
719 if (!m_pFormFillEnv)
721
722 RetainPtr<CPDF_Dictionary> pDictionary =
723 m_pFormFillEnv->GetPDFDocument()->GetInfo();
724 if (!pDictionary)
726
727 return CJS_Result::Success(pRuntime->NewString(
728 pDictionary->GetUnicodeTextFor(propName).AsStringView()));
729}
730
731CJS_Result CJS_Document::get_creation_date(CJS_Runtime* pRuntime) {
732 return getPropertyInternal(pRuntime, "CreationDate");
733}
734
735CJS_Result CJS_Document::set_creation_date(CJS_Runtime* pRuntime,
736 v8::Local<v8::Value> vp) {
737 // Read-only.
739}
740
741CJS_Result CJS_Document::get_creator(CJS_Runtime* pRuntime) {
742 return getPropertyInternal(pRuntime, "Creator");
743}
744
745CJS_Result CJS_Document::set_creator(CJS_Runtime* pRuntime,
746 v8::Local<v8::Value> vp) {
747 // Read-only.
749}
750
751CJS_Result CJS_Document::get_delay(CJS_Runtime* pRuntime) {
752 if (!m_pFormFillEnv)
754 return CJS_Result::Success(pRuntime->NewBoolean(m_bDelay));
755}
756
757CJS_Result CJS_Document::set_delay(CJS_Runtime* pRuntime,
758 v8::Local<v8::Value> vp) {
759 if (!m_pFormFillEnv)
761
762 using pdfium::access_permissions::kModifyContent;
763 if (!m_pFormFillEnv->HasPermissions(kModifyContent))
765
766 m_bDelay = pRuntime->ToBoolean(vp);
767 if (m_bDelay) {
768 m_DelayData.clear();
770 }
771
772 std::list<std::unique_ptr<CJS_DelayData>> DelayDataToProcess;
773 DelayDataToProcess.swap(m_DelayData);
774 for (const auto& pData : DelayDataToProcess)
775 CJS_Field::DoDelay(m_pFormFillEnv.Get(), pData.get());
776
778}
779
780CJS_Result CJS_Document::get_keywords(CJS_Runtime* pRuntime) {
781 return getPropertyInternal(pRuntime, "Keywords");
782}
783
784CJS_Result CJS_Document::set_keywords(CJS_Runtime* pRuntime,
785 v8::Local<v8::Value> vp) {
786 // Read-only.
788}
789
790CJS_Result CJS_Document::get_mod_date(CJS_Runtime* pRuntime) {
791 return getPropertyInternal(pRuntime, "ModDate");
792}
793
794CJS_Result CJS_Document::set_mod_date(CJS_Runtime* pRuntime,
795 v8::Local<v8::Value> vp) {
796 // Read-only.
798}
799
800CJS_Result CJS_Document::get_producer(CJS_Runtime* pRuntime) {
801 return getPropertyInternal(pRuntime, "Producer");
802}
803
804CJS_Result CJS_Document::set_producer(CJS_Runtime* pRuntime,
805 v8::Local<v8::Value> vp) {
806 // Read-only.
808}
809
810CJS_Result CJS_Document::get_subject(CJS_Runtime* pRuntime) {
811 return getPropertyInternal(pRuntime, "Subject");
812}
813
814CJS_Result CJS_Document::set_subject(CJS_Runtime* pRuntime,
815 v8::Local<v8::Value> vp) {
816 // Read-only.
818}
819
820CJS_Result CJS_Document::get_title(CJS_Runtime* pRuntime) {
821 if (!m_pFormFillEnv)
823 return getPropertyInternal(pRuntime, "Title");
824}
825
826CJS_Result CJS_Document::set_title(CJS_Runtime* pRuntime,
827 v8::Local<v8::Value> vp) {
828 // Read-only.
830}
831
832CJS_Result CJS_Document::get_num_pages(CJS_Runtime* pRuntime) {
833 if (!m_pFormFillEnv)
835 return CJS_Result::Success(
836 pRuntime->NewNumber(m_pFormFillEnv->GetPageCount()));
837}
838
839CJS_Result CJS_Document::set_num_pages(CJS_Runtime* pRuntime,
840 v8::Local<v8::Value> vp) {
842}
843
844CJS_Result CJS_Document::get_external(CJS_Runtime* pRuntime) {
845 // In Chrome case, should always return true.
846 return CJS_Result::Success(pRuntime->NewBoolean(true));
847}
848
849CJS_Result CJS_Document::set_external(CJS_Runtime* pRuntime,
850 v8::Local<v8::Value> vp) {
852}
853
854CJS_Result CJS_Document::get_filesize(CJS_Runtime* pRuntime) {
855 return CJS_Result::Success(pRuntime->NewNumber(0));
856}
857
858CJS_Result CJS_Document::set_filesize(CJS_Runtime* pRuntime,
859 v8::Local<v8::Value> vp) {
861}
862
863CJS_Result CJS_Document::get_mouse_x(CJS_Runtime* pRuntime) {
865}
866
867CJS_Result CJS_Document::set_mouse_x(CJS_Runtime* pRuntime,
868 v8::Local<v8::Value> vp) {
870}
871
872CJS_Result CJS_Document::get_mouse_y(CJS_Runtime* pRuntime) {
874}
875
876CJS_Result CJS_Document::set_mouse_y(CJS_Runtime* pRuntime,
877 v8::Local<v8::Value> vp) {
879}
880
881CJS_Result CJS_Document::get_URL(CJS_Runtime* pRuntime) {
882 if (!m_pFormFillEnv)
884 return CJS_Result::Success(
885 pRuntime->NewString(m_pFormFillEnv->JS_docGetFilePath().AsStringView()));
886}
887
888CJS_Result CJS_Document::set_URL(CJS_Runtime* pRuntime,
889 v8::Local<v8::Value> vp) {
891}
892
893CJS_Result CJS_Document::get_base_URL(CJS_Runtime* pRuntime) {
894 return CJS_Result::Success(pRuntime->NewString(m_cwBaseURL.AsStringView()));
895}
896
897CJS_Result CJS_Document::set_base_URL(CJS_Runtime* pRuntime,
898 v8::Local<v8::Value> vp) {
899 m_cwBaseURL = pRuntime->ToWideString(vp);
901}
902
903CJS_Result CJS_Document::get_calculate(CJS_Runtime* pRuntime) {
904 if (!m_pFormFillEnv)
906
907 CPDFSDK_InteractiveForm* pInteractiveForm = GetSDKInteractiveForm();
909 pRuntime->NewBoolean(!!pInteractiveForm->IsCalculateEnabled()));
910}
911
912CJS_Result CJS_Document::set_calculate(CJS_Runtime* pRuntime,
913 v8::Local<v8::Value> vp) {
914 if (!m_pFormFillEnv)
916
917 CPDFSDK_InteractiveForm* pInteractiveForm = GetSDKInteractiveForm();
918 pInteractiveForm->EnableCalculate(pRuntime->ToBoolean(vp));
920}
921
922CJS_Result CJS_Document::get_document_file_name(CJS_Runtime* pRuntime) {
923 if (!m_pFormFillEnv)
925
926 WideString wsFilePath = m_pFormFillEnv->JS_docGetFilePath();
927 size_t i = wsFilePath.GetLength();
928 for (; i > 0; i--) {
929 if (wsFilePath[i - 1] == L'\\' || wsFilePath[i - 1] == L'/')
930 break;
931 }
932 if (i > 0 && i < wsFilePath.GetLength()) {
934 pRuntime->NewString(wsFilePath.AsStringView().Substr(i)));
935 }
936 return CJS_Result::Success(pRuntime->NewString(""));
937}
938
939CJS_Result CJS_Document::set_document_file_name(CJS_Runtime* pRuntime,
940 v8::Local<v8::Value> vp) {
942}
943
944CJS_Result CJS_Document::get_path(CJS_Runtime* pRuntime) {
945 if (!m_pFormFillEnv)
947 return CJS_Result::Success(pRuntime->NewString(
948 CJS_App::SysPathToPDFPath(m_pFormFillEnv->JS_docGetFilePath())
949 .AsStringView()));
950}
951
952CJS_Result CJS_Document::set_path(CJS_Runtime* pRuntime,
953 v8::Local<v8::Value> vp) {
955}
956
957CJS_Result CJS_Document::get_page_window_rect(CJS_Runtime* pRuntime) {
959}
960
961CJS_Result CJS_Document::set_page_window_rect(CJS_Runtime* pRuntime,
962 v8::Local<v8::Value> vp) {
964}
965
966CJS_Result CJS_Document::get_layout(CJS_Runtime* pRuntime) {
968}
969
970CJS_Result CJS_Document::set_layout(CJS_Runtime* pRuntime,
971 v8::Local<v8::Value> vp) {
973}
974
975CJS_Result CJS_Document::addLink(CJS_Runtime* pRuntime,
976 pdfium::span<v8::Local<v8::Value>> params) {
978}
979
980CJS_Result CJS_Document::closeDoc(CJS_Runtime* pRuntime,
981 pdfium::span<v8::Local<v8::Value>> params) {
983}
984
985CJS_Result CJS_Document::getPageBox(CJS_Runtime* pRuntime,
986 pdfium::span<v8::Local<v8::Value>> params) {
988}
989
990CJS_Result CJS_Document::getAnnot(CJS_Runtime* pRuntime,
991 pdfium::span<v8::Local<v8::Value>> params) {
992 if (params.size() != 2)
994 if (!m_pFormFillEnv)
996
997 int nPageNo = pRuntime->ToInt32(params[0]);
998 WideString swAnnotName = pRuntime->ToWideString(params[1]);
999 CPDFSDK_PageView* pPageView = m_pFormFillEnv->GetPageViewAtIndex(nPageNo);
1000 if (!pPageView)
1002
1003 CPDFSDK_AnnotIteration annot_iteration(pPageView);
1004 CPDFSDK_BAAnnot* pSDKBAAnnot = nullptr;
1005 for (const auto& pSDKAnnotCur : annot_iteration) {
1006 auto* pBAAnnot = pSDKAnnotCur->AsBAAnnot();
1007 if (pBAAnnot && pBAAnnot->GetAnnotName() == swAnnotName) {
1008 pSDKBAAnnot = pBAAnnot;
1009 break;
1010 }
1011 }
1012 if (!pSDKBAAnnot)
1014
1015 v8::Local<v8::Object> pObj = pRuntime->NewFXJSBoundObject(
1016 CJS_Annot::GetObjDefnID(), FXJSOBJTYPE_DYNAMIC);
1017 if (pObj.IsEmpty())
1019
1020 auto* pJS_Annot = static_cast<CJS_Annot*>(
1021 CFXJS_Engine::GetBinding(pRuntime->GetIsolate(), pObj));
1022 if (!pJS_Annot)
1024
1025 pJS_Annot->SetSDKAnnot(pSDKBAAnnot);
1026 return CJS_Result::Success(pJS_Annot->ToV8Object());
1027}
1028
1029CJS_Result CJS_Document::getAnnots(CJS_Runtime* pRuntime,
1030 pdfium::span<v8::Local<v8::Value>> params) {
1031 if (!m_pFormFillEnv)
1033
1034 // TODO(tonikitoo): Add support supported parameters as per
1035 // the PDF spec.
1036
1037 int nPageNo = m_pFormFillEnv->GetPageCount();
1038 v8::Local<v8::Array> annots = pRuntime->NewArray();
1039 for (int i = 0; i < nPageNo; ++i) {
1040 CPDFSDK_PageView* pPageView = m_pFormFillEnv->GetPageViewAtIndex(i);
1041 if (!pPageView)
1043
1044 CPDFSDK_AnnotIteration annot_iteration(pPageView);
1045 for (const auto& pSDKAnnotCur : annot_iteration) {
1046 if (!pSDKAnnotCur)
1047 return CJS_Result::Failure(JSMessage::kBadObjectError);
1048
1049 v8::Local<v8::Object> pObj = pRuntime->NewFXJSBoundObject(
1050 CJS_Annot::GetObjDefnID(), FXJSOBJTYPE_DYNAMIC);
1051 if (pObj.IsEmpty())
1052 return CJS_Result::Failure(JSMessage::kBadObjectError);
1053
1054 auto* pJS_Annot = static_cast<CJS_Annot*>(
1055 CFXJS_Engine::GetBinding(pRuntime->GetIsolate(), pObj));
1056 pJS_Annot->SetSDKAnnot(pSDKAnnotCur->AsBAAnnot());
1057 pRuntime->PutArrayElement(
1058 annots, i,
1059 pJS_Annot ? v8::Local<v8::Value>(pJS_Annot->ToV8Object())
1060 : v8::Local<v8::Value>());
1061 }
1062 }
1063 return CJS_Result::Success(annots);
1064}
1065
1066CJS_Result CJS_Document::getAnnot3D(CJS_Runtime* pRuntime,
1067 pdfium::span<v8::Local<v8::Value>> params) {
1068 return CJS_Result::Success(pRuntime->NewUndefined());
1069}
1070
1071CJS_Result CJS_Document::getAnnots3D(
1072 CJS_Runtime* pRuntime,
1073 pdfium::span<v8::Local<v8::Value>> params) {
1074 return CJS_Result::Success();
1075}
1076
1077CJS_Result CJS_Document::getOCGs(CJS_Runtime* pRuntime,
1078 pdfium::span<v8::Local<v8::Value>> params) {
1079 return CJS_Result::Success();
1080}
1081
1082CJS_Result CJS_Document::getLinks(CJS_Runtime* pRuntime,
1083 pdfium::span<v8::Local<v8::Value>> params) {
1084 return CJS_Result::Success();
1085}
1086
1087CJS_Result CJS_Document::addIcon(CJS_Runtime* pRuntime,
1088 pdfium::span<v8::Local<v8::Value>> params) {
1089 if (params.size() != 2)
1091
1092 if (!params[1]->IsObject())
1094
1095 v8::Local<v8::Object> pObj = pRuntime->ToObject(params[1]);
1096 if (!JSGetObject<CJS_Icon>(pRuntime->GetIsolate(), pObj))
1098
1099 WideString swIconName = pRuntime->ToWideString(params[0]);
1100 m_IconNames.push_back(swIconName);
1101 return CJS_Result::Success();
1102}
1103
1104CJS_Result CJS_Document::get_icons(CJS_Runtime* pRuntime) {
1105 // TODO(tsepez): Maybe make consistent with Acrobat Reader behavior which
1106 // is to throw an exception under the default security settings.
1107 if (m_IconNames.empty())
1108 return CJS_Result::Success(pRuntime->NewUndefined());
1109
1110 v8::Local<v8::Array> Icons = pRuntime->NewArray();
1111 int i = 0;
1112 for (const auto& name : m_IconNames) {
1113 v8::Local<v8::Object> pObj = pRuntime->NewFXJSBoundObject(
1114 CJS_Icon::GetObjDefnID(), FXJSOBJTYPE_DYNAMIC);
1115 if (pObj.IsEmpty())
1116 return CJS_Result::Failure(JSMessage::kBadObjectError);
1117
1118 auto* pJS_Icon = static_cast<CJS_Icon*>(
1119 CFXJS_Engine::GetBinding(pRuntime->GetIsolate(), pObj));
1120 pJS_Icon->SetIconName(name);
1121 pRuntime->PutArrayElement(Icons, i++,
1122 pJS_Icon
1123 ? v8::Local<v8::Value>(pJS_Icon->ToV8Object())
1124 : v8::Local<v8::Value>());
1125 }
1126 return CJS_Result::Success(Icons);
1127}
1128
1129CJS_Result CJS_Document::set_icons(CJS_Runtime* pRuntime,
1130 v8::Local<v8::Value> vp) {
1132}
1133
1134CJS_Result CJS_Document::getIcon(CJS_Runtime* pRuntime,
1135 pdfium::span<v8::Local<v8::Value>> params) {
1136 if (params.size() != 1)
1138
1139 WideString swIconName = pRuntime->ToWideString(params[0]);
1140 auto it = std::find(m_IconNames.begin(), m_IconNames.end(), swIconName);
1141 if (it == m_IconNames.end())
1143
1144 v8::Local<v8::Object> pObj = pRuntime->NewFXJSBoundObject(
1145 CJS_Icon::GetObjDefnID(), FXJSOBJTYPE_DYNAMIC);
1146 if (pObj.IsEmpty())
1148
1149 auto* pJSIcon = static_cast<CJS_Icon*>(
1150 CFXJS_Engine::GetBinding(pRuntime->GetIsolate(), pObj));
1151 if (!pJSIcon)
1153
1154 pJSIcon->SetIconName(*it);
1155 return CJS_Result::Success(pJSIcon->ToV8Object());
1156}
1157
1158CJS_Result CJS_Document::removeIcon(CJS_Runtime* pRuntime,
1159 pdfium::span<v8::Local<v8::Value>> params) {
1160 // Unsafe, no supported.
1161 return CJS_Result::Success();
1162}
1163
1164CJS_Result CJS_Document::createDataObject(
1165 CJS_Runtime* pRuntime,
1166 pdfium::span<v8::Local<v8::Value>> params) {
1167 // Unsafe, not implemented.
1168 return CJS_Result::Success();
1169}
1170
1171CJS_Result CJS_Document::get_media(CJS_Runtime* pRuntime) {
1172 return CJS_Result::Success();
1173}
1174
1175CJS_Result CJS_Document::set_media(CJS_Runtime* pRuntime,
1176 v8::Local<v8::Value> vp) {
1177 return CJS_Result::Success();
1178}
1179
1180CJS_Result CJS_Document::calculateNow(
1181 CJS_Runtime* pRuntime,
1182 pdfium::span<v8::Local<v8::Value>> params) {
1183 if (!m_pFormFillEnv)
1185
1186 if (!m_pFormFillEnv->HasPermissions(
1187 pdfium::access_permissions::kModifyContent |
1188 pdfium::access_permissions::kModifyAnnotation |
1189 pdfium::access_permissions::kFillForm)) {
1191 }
1192
1193 GetSDKInteractiveForm()->OnCalculate(nullptr);
1194 return CJS_Result::Success();
1195}
1196
1197CJS_Result CJS_Document::get_collab(CJS_Runtime* pRuntime) {
1198 return CJS_Result::Success();
1199}
1200
1201CJS_Result CJS_Document::set_collab(CJS_Runtime* pRuntime,
1202 v8::Local<v8::Value> vp) {
1203 return CJS_Result::Success();
1204}
1205
1206CJS_Result CJS_Document::getPageNthWord(
1207 CJS_Runtime* pRuntime,
1208 pdfium::span<v8::Local<v8::Value>> params) {
1209 if (!m_pFormFillEnv)
1211
1212 using pdfium::access_permissions::kExtractForAccessibility;
1213 if (!m_pFormFillEnv->HasPermissions(kExtractForAccessibility))
1215
1216 // TODO(tsepez): check maximum allowable params.
1217
1218 int nPageNo = params.size() > 0 ? pRuntime->ToInt32(params[0]) : 0;
1219 int nWordNo = params.size() > 1 ? pRuntime->ToInt32(params[1]) : 0;
1220 bool bStrip = params.size() > 2 ? pRuntime->ToBoolean(params[2]) : true;
1221
1222 CPDF_Document* pDocument = m_pFormFillEnv->GetPDFDocument();
1223 if (nPageNo < 0 || nPageNo >= pDocument->GetPageCount())
1225
1226 RetainPtr<CPDF_Dictionary> pPageDict =
1227 pDocument->GetMutablePageDictionary(nPageNo);
1228 if (!pPageDict)
1230
1231 auto page = pdfium::MakeRetain<CPDF_Page>(pDocument, std::move(pPageDict));
1232 page->AddPageImageCache();
1233 page->ParseContent();
1234
1235 int nWords = 0;
1236 WideString swRet;
1237 for (auto& pPageObj : *page) {
1238 if (pPageObj->IsText()) {
1239 CPDF_TextObject* pTextObj = pPageObj->AsText();
1240 int nObjWords = pTextObj->CountWords();
1241 if (nWords + nObjWords >= nWordNo) {
1242 swRet = pTextObj->GetWordString(nWordNo - nWords);
1243 break;
1244 }
1245 nWords += nObjWords;
1246 }
1247 }
1248
1249 if (bStrip)
1250 swRet.TrimWhitespace();
1251 return CJS_Result::Success(pRuntime->NewString(swRet.AsStringView()));
1252}
1253
1254CJS_Result CJS_Document::getPageNthWordQuads(
1255 CJS_Runtime* pRuntime,
1256 pdfium::span<v8::Local<v8::Value>> params) {
1257 if (!m_pFormFillEnv)
1259
1260 using pdfium::access_permissions::kExtractForAccessibility;
1261 if (!m_pFormFillEnv->HasPermissions(kExtractForAccessibility))
1263
1265}
1266
1267CJS_Result CJS_Document::getPageNumWords(
1268 CJS_Runtime* pRuntime,
1269 pdfium::span<v8::Local<v8::Value>> params) {
1270 if (!m_pFormFillEnv)
1272
1273 using pdfium::access_permissions::kExtractForAccessibility;
1274 if (!m_pFormFillEnv->HasPermissions(kExtractForAccessibility))
1276
1277 int nPageNo = params.size() > 0 ? pRuntime->ToInt32(params[0]) : 0;
1278 CPDF_Document* pDocument = m_pFormFillEnv->GetPDFDocument();
1279 if (nPageNo < 0 || nPageNo >= pDocument->GetPageCount())
1281
1282 RetainPtr<CPDF_Dictionary> pPageDict =
1283 pDocument->GetMutablePageDictionary(nPageNo);
1284 if (!pPageDict)
1286
1287 auto page = pdfium::MakeRetain<CPDF_Page>(pDocument, std::move(pPageDict));
1288 page->AddPageImageCache();
1289 page->ParseContent();
1290
1291 int nWords = 0;
1292 for (auto& pPageObj : *page) {
1293 if (pPageObj->IsText())
1294 nWords += pPageObj->AsText()->CountWords();
1295 }
1296 return CJS_Result::Success(pRuntime->NewNumber(nWords));
1297}
1298
1299CJS_Result CJS_Document::getPrintParams(
1300 CJS_Runtime* pRuntime,
1301 pdfium::span<v8::Local<v8::Value>> params) {
1303}
1304
1305CJS_Result CJS_Document::get_zoom(CJS_Runtime* pRuntime) {
1306 return CJS_Result::Success();
1307}
1308
1309CJS_Result CJS_Document::set_zoom(CJS_Runtime* pRuntime,
1310 v8::Local<v8::Value> vp) {
1311 return CJS_Result::Success();
1312}
1313
1314CJS_Result CJS_Document::get_zoom_type(CJS_Runtime* pRuntime) {
1315 return CJS_Result::Success();
1316}
1317
1318CJS_Result CJS_Document::set_zoom_type(CJS_Runtime* pRuntime,
1319 v8::Local<v8::Value> vp) {
1320 return CJS_Result::Success();
1321}
1322
1323CJS_Result CJS_Document::deletePages(
1324 CJS_Runtime* pRuntime,
1325 pdfium::span<v8::Local<v8::Value>> params) {
1326 // Unsafe, not supported.
1327 return CJS_Result::Success();
1328}
1329
1330CJS_Result CJS_Document::extractPages(
1331 CJS_Runtime* pRuntime,
1332 pdfium::span<v8::Local<v8::Value>> params) {
1333 // Unsafe, not supported.
1334 return CJS_Result::Success();
1335}
1336
1337CJS_Result CJS_Document::insertPages(
1338 CJS_Runtime* pRuntime,
1339 pdfium::span<v8::Local<v8::Value>> params) {
1340 // Unsafe, not supported.
1341 return CJS_Result::Success();
1342}
1343
1344CJS_Result CJS_Document::replacePages(
1345 CJS_Runtime* pRuntime,
1346 pdfium::span<v8::Local<v8::Value>> params) {
1347 // Unsafe, not supported.
1348 return CJS_Result::Success();
1349}
1350
1351CJS_Result CJS_Document::getURL(CJS_Runtime* pRuntime,
1352 pdfium::span<v8::Local<v8::Value>> params) {
1353 // Unsafe, not supported.
1354 return CJS_Result::Success();
1355}
1356
1357CJS_Result CJS_Document::gotoNamedDest(
1358 CJS_Runtime* pRuntime,
1359 pdfium::span<v8::Local<v8::Value>> params) {
1360 if (params.size() != 1)
1362
1363 if (!m_pFormFillEnv)
1365
1366 CPDF_Document* pDocument = m_pFormFillEnv->GetPDFDocument();
1367 RetainPtr<const CPDF_Array> dest_array = CPDF_NameTree::LookupNamedDest(
1368 pDocument, pRuntime->ToByteString(params[0]));
1369 if (!dest_array)
1371
1372 CPDF_Dest dest(std::move(dest_array));
1373 std::vector<float> positions = dest.GetScrollPositionArray();
1374 pRuntime->BeginBlock();
1375 m_pFormFillEnv->DoGoToAction(dest.GetDestPageIndex(pDocument),
1376 dest.GetZoomMode(), positions);
1377 pRuntime->EndBlock();
1378 return CJS_Result::Success();
1379}
1380
1381void CJS_Document::AddDelayData(std::unique_ptr<CJS_DelayData> pData) {
1382 m_DelayData.push_back(std::move(pData));
1383}
1384
1385void CJS_Document::DoFieldDelay(const WideString& sFieldName,
1386 int nControlIndex) {
1387 std::vector<std::unique_ptr<CJS_DelayData>> delayed_data;
1388 auto iter = m_DelayData.begin();
1389 while (iter != m_DelayData.end()) {
1390 auto old = iter++;
1391 if ((*old)->sFieldName == sFieldName &&
1392 (*old)->nControlIndex == nControlIndex) {
1393 delayed_data.push_back(std::move(*old));
1394 m_DelayData.erase(old);
1395 }
1396 }
1397
1398 for (const auto& pData : delayed_data)
1399 CJS_Field::DoDelay(m_pFormFillEnv.Get(), pData.get());
1400}
1401
1402CPDF_InteractiveForm* CJS_Document::GetCoreInteractiveForm() {
1403 return GetSDKInteractiveForm()->GetInteractiveForm();
1404}
1405
1406CPDFSDK_InteractiveForm* CJS_Document::GetSDKInteractiveForm() {
1407 return m_pFormFillEnv->GetInteractiveForm();
1408}
fxcrt::ByteString ByteString
Definition bytestring.h:180
@ FXJSOBJTYPE_GLOBAL
#define DCHECK
Definition check.h:33
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)
CJS_Runtime * GetRuntime() const
Definition cjs_object.h:53
static CJS_Result Success()
Definition cjs_result.h:28
static CJS_Result Failure(JSMessage id)
Definition cjs_result.h:35
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)
std::vector< RetainPtr< CPDF_Object > >::const_iterator const_iterator
Definition cpdf_array.h:29
std::map< ByteString, RetainPtr< CPDF_Object >, std::less<> > DictMap
int GetPageCount() const
WideString GetValue() const
WideString GetFullName() const
WideString()=default
static WideString FromASCII(ByteStringView str)
JSMessage
@ kNotSupportedError
@ kUserGestureRequiredError
fxcrt::WideString WideString
Definition widestring.h:207