7#include "fxjs/cjs_document.h"
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"
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}};
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}};
118uint32_t CJS_Document::ObjDefnID = 0;
119const char CJS_Document::kName[] =
"Document";
129 JSConstructor<CJS_Document>, JSDestructor);
130 DefineProps(pEngine, ObjDefnID, PropertySpecs);
131 DefineMethods(pEngine, ObjDefnID, MethodSpecs);
134CJS_Document::
CJS_Document(v8::Local<v8::Object> pObject, CJS_Runtime* pRuntime)
142CJS_Result CJS_Document::get_num_fields(CJS_Runtime* pRuntime) {
151CJS_Result CJS_Document::set_num_fields(CJS_Runtime* pRuntime,
152 v8::Local<v8::Value> vp) {
156CJS_Result CJS_Document::get_dirty(CJS_Runtime* pRuntime) {
160 return CJS_Result::Success(
161 pRuntime->NewBoolean(!!m_pFormFillEnv->GetChangeMark()));
164CJS_Result CJS_Document::set_dirty(CJS_Runtime* pRuntime,
165 v8::Local<v8::Value> vp) {
169 pRuntime->ToBoolean(vp) ? m_pFormFillEnv->SetChangeMark()
170 : m_pFormFillEnv->ClearChangeMark();
174CJS_Result CJS_Document::get_ADBE(CJS_Runtime* pRuntime) {
178CJS_Result CJS_Document::set_ADBE(CJS_Runtime* pRuntime,
179 v8::Local<v8::Value> vp) {
183CJS_Result CJS_Document::get_page_num(CJS_Runtime* pRuntime) {
187 CPDFSDK_PageView* pPageView = m_pFormFillEnv->GetCurrentView();
194CJS_Result CJS_Document::set_page_num(CJS_Runtime* pRuntime,
195 v8::Local<v8::Value> vp) {
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);
211CJS_Result CJS_Document::addAnnot(CJS_Runtime* pRuntime,
212 pdfium::span<v8::Local<v8::Value>> params) {
217CJS_Result CJS_Document::addField(CJS_Runtime* pRuntime,
218 pdfium::span<v8::Local<v8::Value>> params) {
224 CJS_Runtime* pRuntime,
225 pdfium::span<v8::Local<v8::Value>> params) {
231 CJS_Runtime* pRuntime,
232 pdfium::span<v8::Local<v8::Value>> params) {
238 CJS_Runtime* pRuntime,
239 pdfium::span<v8::Local<v8::Value>> params) {
244CJS_Result CJS_Document::getField(CJS_Runtime* pRuntime,
245 pdfium::span<v8::Local<v8::Value>> params) {
252 WideString wideName = pRuntime->ToWideString(params[0]);
254 if (pPDFForm->CountFields(wideName) <= 0)
257 v8::Local<v8::Object> pFieldObj = pRuntime->NewFXJSBoundObject(
258 CJS_Field::GetObjDefnID(), FXJSOBJTYPE_DYNAMIC);
259 if (pFieldObj.IsEmpty())
262 auto* pJSField =
static_cast<CJS_Field*>(
263 CFXJS_Engine::GetBinding(pRuntime->GetIsolate(), pFieldObj));
267 pJSField->AttachField(
this, wideName);
273 CJS_Runtime* pRuntime,
274 pdfium::span<v8::Local<v8::Value>> params) {
275 if (params.size() != 1)
280 int nIndex = pRuntime->ToInt32(params[0]);
293 CJS_Runtime* pRuntime,
294 pdfium::span<v8::Local<v8::Value>> params) {
300 CJS_Runtime* pRuntime,
301 pdfium::span<v8::Local<v8::Value>> params) {
307 CJS_Runtime* pRuntime,
308 pdfium::span<v8::Local<v8::Value>> params) {
313CJS_Result CJS_Document::mailDoc(CJS_Runtime* pRuntime,
314 pdfium::span<v8::Local<v8::Value>> params) {
318 v8::LocalVector<v8::Value> newParams = ExpandKeywordParams(
319 pRuntime, params, 6,
"bUI",
"cTo",
"cCc",
"cBcc",
"cSubject",
"cMsg");
322 if (IsExpandedParamKnown(newParams[0]))
323 bUI = pRuntime->ToBoolean(newParams[0]);
326 if (IsExpandedParamKnown(newParams[1]))
327 cTo = pRuntime->ToWideString(newParams[1]);
330 if (IsExpandedParamKnown(newParams[2]))
331 cCc = pRuntime->ToWideString(newParams[2]);
334 if (IsExpandedParamKnown(newParams[3]))
335 cBcc = pRuntime->ToWideString(newParams[3]);
338 if (IsExpandedParamKnown(newParams[4]))
339 cSubject = pRuntime->ToWideString(newParams[4]);
342 if (IsExpandedParamKnown(newParams[5]))
343 cMsg = pRuntime->ToWideString(newParams[5]);
346 m_pFormFillEnv->JS_docmailForm(pdfium::span<
const uint8_t>(), bUI, cTo,
347 cSubject, cCc, cBcc, cMsg);
355CJS_Result CJS_Document::mailForm(CJS_Runtime* pRuntime,
356 pdfium::span<v8::Local<v8::Value>> params) {
361 if (!m_pFormFillEnv->HasPermissions(kExtractForAccessibility))
364 CPDFSDK_InteractiveForm* pInteractiveForm = GetSDKInteractiveForm();
366 if (sTextBuf.IsEmpty()) {
370 v8::LocalVector<v8::Value> newParams = ExpandKeywordParams(
371 pRuntime, params, 6,
"bUI",
"cTo",
"cCc",
"cBcc",
"cSubject",
"cMsg");
374 if (IsExpandedParamKnown(newParams[0]))
375 bUI = pRuntime->ToBoolean(newParams[0]);
378 if (IsExpandedParamKnown(newParams[1]))
379 cTo = pRuntime->ToWideString(newParams[1]);
382 if (IsExpandedParamKnown(newParams[2]))
383 cCc = pRuntime->ToWideString(newParams[2]);
386 if (IsExpandedParamKnown(newParams[3]))
387 cBcc = pRuntime->ToWideString(newParams[3]);
390 if (IsExpandedParamKnown(newParams[4]))
391 cSubject = pRuntime->ToWideString(newParams[4]);
394 if (IsExpandedParamKnown(newParams[5]))
395 cMsg = pRuntime->ToWideString(newParams[5]);
398 m_pFormFillEnv->JS_docmailForm(sTextBuf.unsigned_span(), bUI, cTo, cSubject,
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");
411 if (IsExpandedParamKnown(newParams[0]))
412 bUI = pRuntime->ToBoolean(newParams[0]);
415 if (IsExpandedParamKnown(newParams[1]))
416 nStart = pRuntime->ToInt32(newParams[1]);
419 if (IsExpandedParamKnown(newParams[2]))
420 nEnd = pRuntime->ToInt32(newParams[2]);
422 bool bSilent =
false;
423 if (IsExpandedParamKnown(newParams[3]))
424 bSilent = pRuntime->ToBoolean(newParams[3]);
426 bool bShrinkToFit =
false;
427 if (IsExpandedParamKnown(newParams[4]))
428 bShrinkToFit = pRuntime->ToBoolean(newParams[4]);
430 bool bPrintAsImage =
false;
431 if (IsExpandedParamKnown(newParams[5]))
432 bPrintAsImage = pRuntime->ToBoolean(newParams[5]);
434 bool bReverse =
false;
435 if (IsExpandedParamKnown(newParams[6]))
436 bReverse = pRuntime->ToBoolean(newParams[6]);
438 bool bAnnotations =
false;
439 if (IsExpandedParamKnown(newParams[7]))
440 bAnnotations = pRuntime->ToBoolean(newParams[7]);
449 m_pFormFillEnv->JS_docprint(bUI, nStart, nEnd, bSilent, bShrinkToFit,
450 bPrintAsImage, bReverse, bAnnotations);
458 CJS_Runtime* pRuntime,
459 pdfium::span<v8::Local<v8::Value>> params) {
460 if (params.size() != 1)
465 if (!m_pFormFillEnv->HasPermissions(
466 pdfium::access_permissions::kModifyContent |
467 pdfium::access_permissions::kModifyAnnotation)) {
471 WideString sFieldName = pRuntime->ToWideString(params[0]);
472 CPDFSDK_InteractiveForm* pInteractiveForm = GetSDKInteractiveForm();
473 std::vector<ObservedPtr<CPDFSDK_Widget>> widgets;
474 pInteractiveForm->GetWidgets(sFieldName, &widgets);
478 for (
const auto& pWidget : widgets) {
482 IPDF_Page* pPage = pWidget->GetPage();
488 CPDFSDK_PageView* pPageView = m_pFormFillEnv->GetPageView(pPage);
492 CFX_FloatRect rcAnnot = pWidget->GetRect();
493 rcAnnot.Inflate(1.0f, 1.0f, 1.0f, 1.0f);
495 std::vector<CFX_FloatRect> aRefresh(1, rcAnnot);
496 pPageView->UpdateRects(aRefresh);
498 m_pFormFillEnv->SetChangeMark();
506CJS_Result CJS_Document::resetForm(CJS_Runtime* pRuntime,
507 pdfium::span<v8::Local<v8::Value>> params) {
511 if (!m_pFormFillEnv->HasPermissions(
512 pdfium::access_permissions::kModifyContent |
513 pdfium::access_permissions::kModifyAnnotation |
514 pdfium::access_permissions::kFillForm)) {
519 if (params.empty()) {
521 m_pFormFillEnv->SetChangeMark();
525 v8::Local<v8::Array> array;
526 if (params[0]->IsString()) {
527 array = pRuntime->NewArray();
528 pRuntime->PutArrayElement(array, 0, params[0]);
530 array = pRuntime->ToArray(params[0]);
533 std::vector<CPDF_FormField*> aFields;
534 for (size_t i = 0; i < pRuntime->GetArrayLength(array); ++i) {
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));
542 if (!aFields.empty()) {
544 m_pFormFillEnv->SetChangeMark();
550CJS_Result CJS_Document::saveAs(CJS_Runtime* pRuntime,
551 pdfium::span<v8::Local<v8::Value>> params) {
557 CJS_Runtime* pRuntime,
558 pdfium::span<v8::Local<v8::Value>> params) {
562CJS_Result CJS_Document::submitForm(CJS_Runtime* pRuntime,
563 pdfium::span<v8::Local<v8::Value>> params) {
564 size_t nSize = params.size();
574 v8::Local<v8::Array> aFields;
578 if (params[0]->IsString()) {
579 strURL = pRuntime->ToWideString(params[0]);
581 bFDF = pRuntime->ToBoolean(params[1]);
583 bEmpty = pRuntime->ToBoolean(params[2]);
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);
592 bFDF = pRuntime->ToBoolean(pRuntime->GetObjectProperty(pObj,
"bFDF"));
593 bEmpty = pRuntime->ToBoolean(pRuntime->GetObjectProperty(pObj,
"bEmpty"));
594 aFields = pRuntime->ToArray(pRuntime->GetObjectProperty(pObj,
"aFields"));
598 if (pRuntime->GetArrayLength(aFields) == 0 && bEmpty) {
599 if (pPDFForm->CheckRequiredFields(
nullptr,
true)) {
607 std::vector<CPDF_FormField*> fieldObjects;
608 for (size_t i = 0; i < pRuntime->GetArrayLength(aFields); ++i) {
610 pRuntime->ToWideString(pRuntime->GetArrayElement(aFields, i));
611 const size_t jsz = pPDFForm->CountFields(sName);
612 for (size_t j = 0; j < jsz; ++j) {
617 fieldObjects.push_back(pField);
621 if (pPDFForm->CheckRequiredFields(&fieldObjects,
true)) {
623 GetSDKInteractiveForm()->SubmitFields(strURL, fieldObjects,
true, !bFDF);
630 m_pFormFillEnv.Reset(pFormFillEnv);
633CJS_Result CJS_Document::get_bookmark_root(CJS_Runtime* pRuntime) {
637CJS_Result CJS_Document::set_bookmark_root(CJS_Runtime* pRuntime,
638 v8::Local<v8::Value> vp) {
642CJS_Result CJS_Document::get_author(CJS_Runtime* pRuntime) {
643 return getPropertyInternal(pRuntime,
"Author");
646CJS_Result CJS_Document::set_author(CJS_Runtime* pRuntime,
647 v8::Local<v8::Value> vp) {
652CJS_Result CJS_Document::get_info(CJS_Runtime* pRuntime) {
657 m_pFormFillEnv->GetPDFDocument()->GetInfo();
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");
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()));
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()));
709 return CJS_Result::Success(pObj);
712CJS_Result CJS_Document::set_info(CJS_Runtime* pRuntime,
713 v8::Local<v8::Value> vp) {
717CJS_Result CJS_Document::getPropertyInternal(CJS_Runtime* pRuntime,
723 m_pFormFillEnv->GetPDFDocument()->GetInfo();
728 pDictionary->GetUnicodeTextFor(propName).AsStringView())
);
731CJS_Result CJS_Document::get_creation_date(CJS_Runtime* pRuntime) {
732 return getPropertyInternal(pRuntime,
"CreationDate");
735CJS_Result CJS_Document::set_creation_date(CJS_Runtime* pRuntime,
736 v8::Local<v8::Value> vp) {
741CJS_Result CJS_Document::get_creator(CJS_Runtime* pRuntime) {
742 return getPropertyInternal(pRuntime,
"Creator");
745CJS_Result CJS_Document::set_creator(CJS_Runtime* pRuntime,
746 v8::Local<v8::Value> vp) {
751CJS_Result CJS_Document::get_delay(CJS_Runtime* pRuntime) {
757CJS_Result CJS_Document::set_delay(CJS_Runtime* pRuntime,
758 v8::Local<v8::Value> vp) {
763 if (!m_pFormFillEnv->HasPermissions(kModifyContent))
766 m_bDelay = pRuntime->ToBoolean(vp);
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());
780CJS_Result CJS_Document::get_keywords(CJS_Runtime* pRuntime) {
781 return getPropertyInternal(pRuntime,
"Keywords");
784CJS_Result CJS_Document::set_keywords(CJS_Runtime* pRuntime,
785 v8::Local<v8::Value> vp) {
790CJS_Result CJS_Document::get_mod_date(CJS_Runtime* pRuntime) {
791 return getPropertyInternal(pRuntime,
"ModDate");
794CJS_Result CJS_Document::set_mod_date(CJS_Runtime* pRuntime,
795 v8::Local<v8::Value> vp) {
800CJS_Result CJS_Document::get_producer(CJS_Runtime* pRuntime) {
801 return getPropertyInternal(pRuntime,
"Producer");
804CJS_Result CJS_Document::set_producer(CJS_Runtime* pRuntime,
805 v8::Local<v8::Value> vp) {
810CJS_Result CJS_Document::get_subject(CJS_Runtime* pRuntime) {
811 return getPropertyInternal(pRuntime,
"Subject");
814CJS_Result CJS_Document::set_subject(CJS_Runtime* pRuntime,
815 v8::Local<v8::Value> vp) {
820CJS_Result CJS_Document::get_title(CJS_Runtime* pRuntime) {
823 return getPropertyInternal(pRuntime,
"Title");
826CJS_Result CJS_Document::set_title(CJS_Runtime* pRuntime,
827 v8::Local<v8::Value> vp) {
832CJS_Result CJS_Document::get_num_pages(CJS_Runtime* pRuntime) {
835 return CJS_Result::Success(
836 pRuntime->NewNumber(m_pFormFillEnv->GetPageCount()));
839CJS_Result CJS_Document::set_num_pages(CJS_Runtime* pRuntime,
840 v8::Local<v8::Value> vp) {
844CJS_Result CJS_Document::get_external(CJS_Runtime* pRuntime) {
849CJS_Result CJS_Document::set_external(CJS_Runtime* pRuntime,
850 v8::Local<v8::Value> vp) {
854CJS_Result CJS_Document::get_filesize(CJS_Runtime* pRuntime) {
858CJS_Result CJS_Document::set_filesize(CJS_Runtime* pRuntime,
859 v8::Local<v8::Value> vp) {
863CJS_Result CJS_Document::get_mouse_x(CJS_Runtime* pRuntime) {
867CJS_Result CJS_Document::set_mouse_x(CJS_Runtime* pRuntime,
868 v8::Local<v8::Value> vp) {
872CJS_Result CJS_Document::get_mouse_y(CJS_Runtime* pRuntime) {
876CJS_Result CJS_Document::set_mouse_y(CJS_Runtime* pRuntime,
877 v8::Local<v8::Value> vp) {
881CJS_Result CJS_Document::get_URL(CJS_Runtime* pRuntime) {
884 return CJS_Result::Success(
885 pRuntime->NewString(m_pFormFillEnv->JS_docGetFilePath().AsStringView()));
888CJS_Result CJS_Document::set_URL(CJS_Runtime* pRuntime,
889 v8::Local<v8::Value> vp) {
893CJS_Result CJS_Document::get_base_URL(CJS_Runtime* pRuntime) {
894 return CJS_Result::Success(pRuntime->NewString(m_cwBaseURL.AsStringView()));
897CJS_Result CJS_Document::set_base_URL(CJS_Runtime* pRuntime,
898 v8::Local<v8::Value> vp) {
899 m_cwBaseURL = pRuntime->ToWideString(vp);
903CJS_Result CJS_Document::get_calculate(CJS_Runtime* pRuntime) {
907 CPDFSDK_InteractiveForm* pInteractiveForm = GetSDKInteractiveForm();
912CJS_Result CJS_Document::set_calculate(CJS_Runtime* pRuntime,
913 v8::Local<v8::Value> vp) {
917 CPDFSDK_InteractiveForm* pInteractiveForm = GetSDKInteractiveForm();
922CJS_Result CJS_Document::get_document_file_name(CJS_Runtime* pRuntime) {
926 WideString wsFilePath = m_pFormFillEnv->JS_docGetFilePath();
927 size_t i = wsFilePath.GetLength();
929 if (wsFilePath[i - 1] == L'\\' || wsFilePath[i - 1] == L'/')
932 if (i > 0 && i < wsFilePath.GetLength()) {
934 pRuntime->NewString(wsFilePath.AsStringView().Substr(i))
);
939CJS_Result CJS_Document::set_document_file_name(CJS_Runtime* pRuntime,
940 v8::Local<v8::Value> vp) {
944CJS_Result CJS_Document::get_path(CJS_Runtime* pRuntime) {
947 return CJS_Result::Success(pRuntime->NewString(
948 CJS_App::SysPathToPDFPath(m_pFormFillEnv->JS_docGetFilePath())
952CJS_Result CJS_Document::set_path(CJS_Runtime* pRuntime,
953 v8::Local<v8::Value> vp) {
957CJS_Result CJS_Document::get_page_window_rect(CJS_Runtime* pRuntime) {
961CJS_Result CJS_Document::set_page_window_rect(CJS_Runtime* pRuntime,
962 v8::Local<v8::Value> vp) {
966CJS_Result CJS_Document::get_layout(CJS_Runtime* pRuntime) {
970CJS_Result CJS_Document::set_layout(CJS_Runtime* pRuntime,
971 v8::Local<v8::Value> vp) {
975CJS_Result CJS_Document::addLink(CJS_Runtime* pRuntime,
976 pdfium::span<v8::Local<v8::Value>> params) {
980CJS_Result CJS_Document::closeDoc(CJS_Runtime* pRuntime,
981 pdfium::span<v8::Local<v8::Value>> params) {
985CJS_Result CJS_Document::getPageBox(CJS_Runtime* pRuntime,
986 pdfium::span<v8::Local<v8::Value>> params) {
990CJS_Result CJS_Document::getAnnot(CJS_Runtime* pRuntime,
991 pdfium::span<v8::Local<v8::Value>> params) {
992 if (params.size() != 2)
997 int nPageNo = pRuntime->ToInt32(params[0]);
998 WideString swAnnotName = pRuntime->ToWideString(params[1]);
999 CPDFSDK_PageView* pPageView = m_pFormFillEnv->GetPageViewAtIndex(nPageNo);
1005 for (
const auto& pSDKAnnotCur : annot_iteration) {
1006 auto* pBAAnnot = pSDKAnnotCur->AsBAAnnot();
1007 if (pBAAnnot && pBAAnnot->GetAnnotName() == swAnnotName) {
1008 pSDKBAAnnot = pBAAnnot;
1015 v8::Local<v8::Object> pObj = pRuntime->NewFXJSBoundObject(
1016 CJS_Annot::GetObjDefnID(), FXJSOBJTYPE_DYNAMIC);
1020 auto* pJS_Annot =
static_cast<CJS_Annot*>(
1021 CFXJS_Engine::GetBinding(pRuntime->GetIsolate(), pObj));
1025 pJS_Annot->SetSDKAnnot(pSDKBAAnnot);
1029CJS_Result CJS_Document::getAnnots(CJS_Runtime* pRuntime,
1030 pdfium::span<v8::Local<v8::Value>> params) {
1031 if (!m_pFormFillEnv)
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);
1045 for (
const auto& pSDKAnnotCur : annot_iteration) {
1047 return CJS_Result::Failure(JSMessage::kBadObjectError);
1049 v8::Local<v8::Object> pObj = pRuntime->NewFXJSBoundObject(
1050 CJS_Annot::GetObjDefnID(), FXJSOBJTYPE_DYNAMIC);
1052 return CJS_Result::Failure(JSMessage::kBadObjectError);
1054 auto* pJS_Annot =
static_cast<CJS_Annot*>(
1055 CFXJS_Engine::GetBinding(pRuntime->GetIsolate(), pObj));
1056 pJS_Annot->SetSDKAnnot(pSDKAnnotCur->AsBAAnnot());
1057 pRuntime->PutArrayElement(
1059 pJS_Annot ? v8::Local<v8::Value>(pJS_Annot->ToV8Object())
1060 : v8::Local<v8::Value>());
1063 return CJS_Result::Success(annots);
1066CJS_Result CJS_Document::getAnnot3D(CJS_Runtime* pRuntime,
1067 pdfium::span<v8::Local<v8::Value>> params) {
1072 CJS_Runtime* pRuntime,
1073 pdfium::span<v8::Local<v8::Value>> params) {
1077CJS_Result CJS_Document::getOCGs(CJS_Runtime* pRuntime,
1078 pdfium::span<v8::Local<v8::Value>> params) {
1082CJS_Result CJS_Document::getLinks(CJS_Runtime* pRuntime,
1083 pdfium::span<v8::Local<v8::Value>> params) {
1087CJS_Result CJS_Document::addIcon(CJS_Runtime* pRuntime,
1088 pdfium::span<v8::Local<v8::Value>> params) {
1089 if (params.size() != 2)
1092 if (!params[1]->IsObject())
1095 v8::Local<v8::Object> pObj = pRuntime->ToObject(params[1]);
1096 if (!JSGetObject<CJS_Icon>(pRuntime->GetIsolate(), pObj))
1099 WideString swIconName = pRuntime->ToWideString(params[0]);
1100 m_IconNames.push_back(swIconName);
1104CJS_Result CJS_Document::get_icons(CJS_Runtime* pRuntime) {
1107 if (m_IconNames.empty())
1110 v8::Local<v8::Array> Icons = pRuntime->NewArray();
1112 for (
const auto& name : m_IconNames) {
1113 v8::Local<v8::Object> pObj = pRuntime->NewFXJSBoundObject(
1114 CJS_Icon::GetObjDefnID(), FXJSOBJTYPE_DYNAMIC);
1116 return CJS_Result::Failure(JSMessage::kBadObjectError);
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++,
1123 ? v8::Local<v8::Value>(pJS_Icon->ToV8Object())
1124 : v8::Local<v8::Value>());
1126 return CJS_Result::Success(Icons);
1129CJS_Result CJS_Document::set_icons(CJS_Runtime* pRuntime,
1130 v8::Local<v8::Value> vp) {
1134CJS_Result CJS_Document::getIcon(CJS_Runtime* pRuntime,
1135 pdfium::span<v8::Local<v8::Value>> params) {
1136 if (params.size() != 1)
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())
1144 v8::Local<v8::Object> pObj = pRuntime->NewFXJSBoundObject(
1145 CJS_Icon::GetObjDefnID(), FXJSOBJTYPE_DYNAMIC);
1149 auto* pJSIcon =
static_cast<CJS_Icon*>(
1150 CFXJS_Engine::GetBinding(pRuntime->GetIsolate(), pObj));
1154 pJSIcon->SetIconName(*it);
1158CJS_Result CJS_Document::removeIcon(CJS_Runtime* pRuntime,
1159 pdfium::span<v8::Local<v8::Value>> params) {
1165 CJS_Runtime* pRuntime,
1166 pdfium::span<v8::Local<v8::Value>> params) {
1171CJS_Result CJS_Document::get_media(CJS_Runtime* pRuntime) {
1175CJS_Result CJS_Document::set_media(CJS_Runtime* pRuntime,
1176 v8::Local<v8::Value> vp) {
1181 CJS_Runtime* pRuntime,
1182 pdfium::span<v8::Local<v8::Value>> params) {
1183 if (!m_pFormFillEnv)
1186 if (!m_pFormFillEnv->HasPermissions(
1187 pdfium::access_permissions::kModifyContent |
1188 pdfium::access_permissions::kModifyAnnotation |
1189 pdfium::access_permissions::kFillForm)) {
1197CJS_Result CJS_Document::get_collab(CJS_Runtime* pRuntime) {
1201CJS_Result CJS_Document::set_collab(CJS_Runtime* pRuntime,
1202 v8::Local<v8::Value> vp) {
1207 CJS_Runtime* pRuntime,
1208 pdfium::span<v8::Local<v8::Value>> params) {
1209 if (!m_pFormFillEnv)
1213 if (!m_pFormFillEnv->HasPermissions(kExtractForAccessibility))
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;
1222 CPDF_Document* pDocument = m_pFormFillEnv->GetPDFDocument();
1227 pDocument->GetMutablePageDictionary(nPageNo);
1231 auto page =
pdfium::MakeRetain<CPDF_Page>(pDocument,
std::move(pPageDict));
1232 page->AddPageImageCache();
1233 page->ParseContent();
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);
1245 nWords += nObjWords;
1255 CJS_Runtime* pRuntime,
1256 pdfium::span<v8::Local<v8::Value>> params) {
1257 if (!m_pFormFillEnv)
1261 if (!m_pFormFillEnv->HasPermissions(kExtractForAccessibility))
1268 CJS_Runtime* pRuntime,
1269 pdfium::span<v8::Local<v8::Value>> params) {
1270 if (!m_pFormFillEnv)
1274 if (!m_pFormFillEnv->HasPermissions(kExtractForAccessibility))
1277 int nPageNo = params.size() > 0 ? pRuntime->ToInt32(params[0]) : 0;
1278 CPDF_Document* pDocument = m_pFormFillEnv->GetPDFDocument();
1283 pDocument->GetMutablePageDictionary(nPageNo);
1287 auto page =
pdfium::MakeRetain<CPDF_Page>(pDocument,
std::move(pPageDict));
1288 page->AddPageImageCache();
1289 page->ParseContent();
1292 for (
auto& pPageObj : *page) {
1293 if (pPageObj->IsText())
1294 nWords += pPageObj->AsText()->CountWords();
1300 CJS_Runtime* pRuntime,
1301 pdfium::span<v8::Local<v8::Value>> params) {
1305CJS_Result CJS_Document::get_zoom(CJS_Runtime* pRuntime) {
1309CJS_Result CJS_Document::set_zoom(CJS_Runtime* pRuntime,
1310 v8::Local<v8::Value> vp) {
1314CJS_Result CJS_Document::get_zoom_type(CJS_Runtime* pRuntime) {
1318CJS_Result CJS_Document::set_zoom_type(CJS_Runtime* pRuntime,
1319 v8::Local<v8::Value> vp) {
1324 CJS_Runtime* pRuntime,
1325 pdfium::span<v8::Local<v8::Value>> params) {
1331 CJS_Runtime* pRuntime,
1332 pdfium::span<v8::Local<v8::Value>> params) {
1338 CJS_Runtime* pRuntime,
1339 pdfium::span<v8::Local<v8::Value>> params) {
1345 CJS_Runtime* pRuntime,
1346 pdfium::span<v8::Local<v8::Value>> params) {
1351CJS_Result CJS_Document::getURL(CJS_Runtime* pRuntime,
1352 pdfium::span<v8::Local<v8::Value>> params) {
1358 CJS_Runtime* pRuntime,
1359 pdfium::span<v8::Local<v8::Value>> params) {
1360 if (params.size() != 1)
1363 if (!m_pFormFillEnv)
1366 CPDF_Document* pDocument = m_pFormFillEnv->GetPDFDocument();
1368 pDocument, pRuntime->ToByteString(params[0]));
1373 std::vector<
float> positions = dest.GetScrollPositionArray();
1375 m_pFormFillEnv->DoGoToAction(dest.GetDestPageIndex(pDocument),
1376 dest.GetZoomMode(), positions);
1382 m_DelayData.push_back(std::move(pData));
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()) {
1391 if ((*old)->sFieldName == sFieldName &&
1392 (*old)->nControlIndex == nControlIndex) {
1393 delayed_data.push_back(
std::move(*old));
1394 m_DelayData.erase(old);
1398 for (
const auto& pData : delayed_data)
1399 CJS_Field::DoDelay(m_pFormFillEnv.Get(), pData.get());
1406CPDFSDK_InteractiveForm* CJS_Document::GetSDKInteractiveForm() {
1407 return m_pFormFillEnv->GetInteractiveForm();
fxcrt::ByteString ByteString
void DoFieldDelay(const WideString &sFieldName, int nControlIndex)
CJS_Document(v8::Local< v8::Object > pObject, CJS_Runtime *pRuntime)
void SetFormFillEnv(CPDFSDK_FormFillEnvironment *pFormFillEnv)
void AddDelayData(std::unique_ptr< CJS_DelayData > pData)
static uint32_t GetObjDefnID()
static void DefineJSObjects(CFXJS_Engine *pEngine)
bool IsUserGesture() const
CJS_Runtime * GetRuntime() const
static CJS_Result Success()
static CJS_Result Failure(JSMessage id)
CJS_EventContext * GetCurrentEventContext() const
CPDFSDK_AnnotIteration(CPDFSDK_PageView *page_view)
std::vector< RetainPtr< CPDF_Object > >::const_iterator const_iterator
std::map< ByteString, RetainPtr< CPDF_Object >, std::less<> > DictMap
static WideString FromASCII(ByteStringView str)
@ kUserGestureRequiredError
fxcrt::WideString WideString