Merge CPDFSDK_Document into CPDFSDK_FormFillEnvironment

This CL merges the CPDFSDK_Document and CPDFSDK_FormFillEnvironment classes
as they always existed and always pointed at each other.

Review-Url: https://codereview.chromium.org/2410893002
diff --git a/BUILD.gn b/BUILD.gn
index a2a79d7..5e1e4b5 100644
--- a/BUILD.gn
+++ b/BUILD.gn
@@ -78,8 +78,6 @@
     "fpdfsdk/cpdfsdk_baannothandler.h",
     "fpdfsdk/cpdfsdk_datetime.cpp",
     "fpdfsdk/cpdfsdk_datetime.h",
-    "fpdfsdk/cpdfsdk_document.cpp",
-    "fpdfsdk/cpdfsdk_document.h",
     "fpdfsdk/cpdfsdk_formfillenvironment.cpp",
     "fpdfsdk/cpdfsdk_formfillenvironment.h",
     "fpdfsdk/cpdfsdk_interform.cpp",
diff --git a/fpdfsdk/cfx_systemhandler.cpp b/fpdfsdk/cfx_systemhandler.cpp
index 72cb9f5..62bad46 100644
--- a/fpdfsdk/cfx_systemhandler.cpp
+++ b/fpdfsdk/cfx_systemhandler.cpp
@@ -13,7 +13,6 @@
 #include "core/fxge/cfx_fontmgr.h"
 #include "core/fxge/cfx_gemodule.h"
 #include "fpdfsdk/cpdfsdk_annot.h"
-#include "fpdfsdk/cpdfsdk_document.h"
 #include "fpdfsdk/cpdfsdk_formfillenvironment.h"
 #include "fpdfsdk/cpdfsdk_pageview.h"
 #include "fpdfsdk/cpdfsdk_widget.h"
diff --git a/fpdfsdk/cpdfsdk_annothandlermgr.cpp b/fpdfsdk/cpdfsdk_annothandlermgr.cpp
index b7bb760..5324c3f 100644
--- a/fpdfsdk/cpdfsdk_annothandlermgr.cpp
+++ b/fpdfsdk/cpdfsdk_annothandlermgr.cpp
@@ -12,7 +12,6 @@
 #include "fpdfsdk/cpdfsdk_baannot.h"
 #include "fpdfsdk/cpdfsdk_baannothandler.h"
 #include "fpdfsdk/cpdfsdk_datetime.h"
-#include "fpdfsdk/cpdfsdk_document.h"
 #include "fpdfsdk/cpdfsdk_formfillenvironment.h"
 #include "fpdfsdk/cpdfsdk_pageview.h"
 #include "fpdfsdk/cpdfsdk_widgethandler.h"
@@ -205,7 +204,7 @@
     CPDFSDK_Annot::ObservedPtr pNext(
         GetNextAnnot(pFocusAnnot, !m_pEnv->IsSHIFTKeyDown(nFlag)));
     if (pNext && pNext.Get() != pFocusAnnot) {
-      pPage->GetFormFillEnv()->GetSDKDocument()->SetFocusAnnot(&pNext);
+      pPage->GetFormFillEnv()->SetFocusAnnot(&pNext);
       return TRUE;
     }
   }
diff --git a/fpdfsdk/cpdfsdk_baannot.cpp b/fpdfsdk/cpdfsdk_baannot.cpp
index 7d2a92a..72468e1 100644
--- a/fpdfsdk/cpdfsdk_baannot.cpp
+++ b/fpdfsdk/cpdfsdk_baannot.cpp
@@ -12,7 +12,6 @@
 #include "core/fpdfapi/parser/cpdf_stream.h"
 #include "core/fpdfapi/parser/fpdf_parser_decode.h"
 #include "fpdfsdk/cpdfsdk_datetime.h"
-#include "fpdfsdk/cpdfsdk_document.h"
 #include "fpdfsdk/cpdfsdk_pageview.h"
 
 CPDFSDK_BAAnnot::CPDFSDK_BAAnnot(CPDF_Annot* pAnnot,
diff --git a/fpdfsdk/cpdfsdk_document.cpp b/fpdfsdk/cpdfsdk_document.cpp
deleted file mode 100644
index 0aeb919..0000000
--- a/fpdfsdk/cpdfsdk_document.cpp
+++ /dev/null
@@ -1,247 +0,0 @@
-// Copyright 2016 PDFium Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-
-// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
-
-#include "fpdfsdk/cpdfsdk_document.h"
-
-#include "core/fpdfapi/parser/cpdf_array.h"
-#include "core/fpdfapi/parser/cpdf_dictionary.h"
-#include "core/fpdfapi/parser/cpdf_document.h"
-#include "core/fpdfapi/parser/cpdf_object.h"
-#include "core/fpdfdoc/cpdf_action.h"
-#include "core/fpdfdoc/cpdf_docjsactions.h"
-#include "core/fpdfdoc/cpdf_occontext.h"
-#include "fpdfsdk/cpdfsdk_annot.h"
-#include "fpdfsdk/cpdfsdk_annothandlermgr.h"
-#include "fpdfsdk/cpdfsdk_formfillenvironment.h"
-#include "fpdfsdk/cpdfsdk_interform.h"
-#include "fpdfsdk/cpdfsdk_pageview.h"
-#include "fpdfsdk/cpdfsdk_widget.h"
-#include "fpdfsdk/fsdk_actionhandler.h"
-#include "third_party/base/ptr_util.h"
-
-CPDFSDK_Document::CPDFSDK_Document(UnderlyingDocumentType* pDoc,
-                                   CPDFSDK_FormFillEnvironment* pEnv)
-    : m_pDoc(pDoc),
-      m_pEnv(pEnv),
-      m_bChangeMask(FALSE),
-      m_bBeingDestroyed(FALSE) {}
-
-CPDFSDK_Document::~CPDFSDK_Document() {
-  m_bBeingDestroyed = TRUE;
-
-  ClearAllFocusedAnnots();
-  for (auto& it : m_pageMap)
-    delete it.second;
-  m_pageMap.clear();
-}
-
-void CPDFSDK_Document::ClearAllFocusedAnnots() {
-  for (auto& it : m_pageMap) {
-    if (it.second->IsValidSDKAnnot(GetFocusAnnot()))
-      KillFocusAnnot(0);
-  }
-}
-
-CPDFSDK_PageView* CPDFSDK_Document::GetPageView(
-    UnderlyingPageType* pUnderlyingPage,
-    bool ReNew) {
-  auto it = m_pageMap.find(pUnderlyingPage);
-  if (it != m_pageMap.end())
-    return it->second;
-
-  if (!ReNew)
-    return nullptr;
-
-  CPDFSDK_PageView* pPageView = new CPDFSDK_PageView(GetEnv(), pUnderlyingPage);
-  m_pageMap[pUnderlyingPage] = pPageView;
-  // Delay to load all the annotations, to avoid endless loop.
-  pPageView->LoadFXAnnots();
-  return pPageView;
-}
-
-CPDFSDK_PageView* CPDFSDK_Document::GetCurrentView() {
-  UnderlyingPageType* pPage =
-      UnderlyingFromFPDFPage(m_pEnv->GetCurrentPage(m_pDoc));
-  return pPage ? GetPageView(pPage, true) : nullptr;
-}
-
-CPDFSDK_PageView* CPDFSDK_Document::GetPageView(int nIndex) {
-  UnderlyingPageType* pTempPage =
-      UnderlyingFromFPDFPage(m_pEnv->GetPage(m_pDoc, nIndex));
-  if (!pTempPage)
-    return nullptr;
-
-  auto it = m_pageMap.find(pTempPage);
-  return it != m_pageMap.end() ? it->second : nullptr;
-}
-
-void CPDFSDK_Document::ProcJavascriptFun() {
-  CPDF_Document* pPDFDoc = GetPDFDocument();
-  CPDF_DocJSActions docJS(pPDFDoc);
-  int iCount = docJS.CountJSActions();
-  if (iCount < 1)
-    return;
-  for (int i = 0; i < iCount; i++) {
-    CFX_ByteString csJSName;
-    CPDF_Action jsAction = docJS.GetJSAction(i, csJSName);
-    if (m_pEnv->GetActionHander())
-      m_pEnv->GetActionHander()->DoAction_JavaScript(
-          jsAction, CFX_WideString::FromLocal(csJSName.AsStringC()), GetEnv());
-  }
-}
-
-FX_BOOL CPDFSDK_Document::ProcOpenAction() {
-  if (!m_pDoc)
-    return FALSE;
-
-  CPDF_Dictionary* pRoot = GetPDFDocument()->GetRoot();
-  if (!pRoot)
-    return FALSE;
-
-  CPDF_Object* pOpenAction = pRoot->GetDictFor("OpenAction");
-  if (!pOpenAction)
-    pOpenAction = pRoot->GetArrayFor("OpenAction");
-
-  if (!pOpenAction)
-    return FALSE;
-
-  if (pOpenAction->IsArray())
-    return TRUE;
-
-  if (CPDF_Dictionary* pDict = pOpenAction->AsDictionary()) {
-    CPDF_Action action(pDict);
-    if (m_pEnv->GetActionHander())
-      m_pEnv->GetActionHander()->DoAction_DocOpen(action, GetEnv());
-    return TRUE;
-  }
-  return FALSE;
-}
-
-void CPDFSDK_Document::RemovePageView(UnderlyingPageType* pUnderlyingPage) {
-  auto it = m_pageMap.find(pUnderlyingPage);
-  if (it == m_pageMap.end())
-    return;
-
-  CPDFSDK_PageView* pPageView = it->second;
-  if (pPageView->IsLocked() || pPageView->IsBeingDestroyed())
-    return;
-
-  // Mark the page view so we do not come into |RemovePageView| a second
-  // time while we're in the process of removing.
-  pPageView->SetBeingDestroyed();
-
-  // This must happen before we remove |pPageView| from the map because
-  // |KillFocusAnnot| can call into the |GetPage| method which will
-  // look for this page view in the map, if it doesn't find it a new one will
-  // be created. We then have two page views pointing to the same page and
-  // bad things happen.
-  if (pPageView->IsValidSDKAnnot(GetFocusAnnot()))
-    KillFocusAnnot(0);
-
-  // Remove the page from the map to make sure we don't accidentally attempt
-  // to use the |pPageView| while we're cleaning it up.
-  m_pageMap.erase(it);
-
-  delete pPageView;
-}
-
-UnderlyingPageType* CPDFSDK_Document::GetPage(int nIndex) {
-  return UnderlyingFromFPDFPage(m_pEnv->GetPage(m_pDoc, nIndex));
-}
-
-CPDFSDK_InterForm* CPDFSDK_Document::GetInterForm() {
-  if (!m_pInterForm)
-    m_pInterForm = pdfium::MakeUnique<CPDFSDK_InterForm>(GetEnv());
-  return m_pInterForm.get();
-}
-
-void CPDFSDK_Document::UpdateAllViews(CPDFSDK_PageView* pSender,
-                                      CPDFSDK_Annot* pAnnot) {
-  for (const auto& it : m_pageMap) {
-    CPDFSDK_PageView* pPageView = it.second;
-    if (pPageView != pSender)
-      pPageView->UpdateView(pAnnot);
-  }
-}
-
-FX_BOOL CPDFSDK_Document::SetFocusAnnot(CPDFSDK_Annot::ObservedPtr* pAnnot) {
-  if (m_bBeingDestroyed)
-    return FALSE;
-  if (m_pFocusAnnot == *pAnnot)
-    return TRUE;
-  if (m_pFocusAnnot && !KillFocusAnnot(0))
-    return FALSE;
-  if (!*pAnnot)
-    return FALSE;
-
-#ifdef PDF_ENABLE_XFA
-  CPDFSDK_Annot::ObservedPtr pLastFocusAnnot(m_pFocusAnnot.Get());
-#endif  // PDF_ENABLE_XFA
-  CPDFSDK_PageView* pPageView = (*pAnnot)->GetPageView();
-  if (pPageView && pPageView->IsValid()) {
-    CPDFSDK_AnnotHandlerMgr* pAnnotHandler = m_pEnv->GetAnnotHandlerMgr();
-    if (!m_pFocusAnnot) {
-#ifdef PDF_ENABLE_XFA
-      if (!pAnnotHandler->Annot_OnChangeFocus(pAnnot, &pLastFocusAnnot))
-        return FALSE;
-#endif  // PDF_ENABLE_XFA
-      if (!pAnnotHandler->Annot_OnSetFocus(pAnnot, 0))
-        return FALSE;
-      if (!m_pFocusAnnot) {
-        m_pFocusAnnot.Reset(pAnnot->Get());
-        return TRUE;
-      }
-    }
-  }
-  return FALSE;
-}
-
-FX_BOOL CPDFSDK_Document::KillFocusAnnot(uint32_t nFlag) {
-  if (m_pFocusAnnot) {
-    CPDFSDK_AnnotHandlerMgr* pAnnotHandler = m_pEnv->GetAnnotHandlerMgr();
-    CPDFSDK_Annot::ObservedPtr pFocusAnnot(m_pFocusAnnot.Get());
-    m_pFocusAnnot.Reset();
-
-#ifdef PDF_ENABLE_XFA
-    CPDFSDK_Annot::ObservedPtr pNull;
-    if (!pAnnotHandler->Annot_OnChangeFocus(&pNull, &pFocusAnnot))
-      return FALSE;
-#endif  // PDF_ENABLE_XFA
-
-    if (pAnnotHandler->Annot_OnKillFocus(&pFocusAnnot, nFlag)) {
-      if (pFocusAnnot->GetAnnotSubtype() == CPDF_Annot::Subtype::WIDGET) {
-        CPDFSDK_Widget* pWidget =
-            static_cast<CPDFSDK_Widget*>(pFocusAnnot.Get());
-        int nFieldType = pWidget->GetFieldType();
-        if (FIELDTYPE_TEXTFIELD == nFieldType ||
-            FIELDTYPE_COMBOBOX == nFieldType) {
-          m_pEnv->OnSetFieldInputFocus(nullptr, 0, FALSE);
-        }
-      }
-      if (!m_pFocusAnnot)
-        return TRUE;
-    } else {
-      m_pFocusAnnot.Reset(pFocusAnnot.Get());
-    }
-  }
-  return FALSE;
-}
-
-void CPDFSDK_Document::OnCloseDocument() {
-  KillFocusAnnot(0);
-}
-
-FX_BOOL CPDFSDK_Document::GetPermissions(int nFlag) {
-  return GetPDFDocument()->GetUserPermissions() & nFlag;
-}
-
-IJS_Runtime* CPDFSDK_Document::GetJsRuntime() {
-  return m_pEnv->GetJSRuntime();
-}
-
-CFX_WideString CPDFSDK_Document::GetPath() {
-  return m_pEnv->JS_docGetFilePath();
-}
diff --git a/fpdfsdk/cpdfsdk_document.h b/fpdfsdk/cpdfsdk_document.h
deleted file mode 100644
index b765ed5..0000000
--- a/fpdfsdk/cpdfsdk_document.h
+++ /dev/null
@@ -1,105 +0,0 @@
-// Copyright 2016 PDFium Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-
-// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
-
-#ifndef FPDFSDK_CPDFSDK_DOCUMENT_H_
-#define FPDFSDK_CPDFSDK_DOCUMENT_H_
-
-#include <map>
-#include <memory>
-
-#include "core/fpdfapi/parser/cpdf_document.h"
-#include "core/fxcrt/cfx_observable.h"
-#include "fpdfsdk/cpdfsdk_annot.h"
-#include "fpdfsdk/fsdk_define.h"
-#include "public/fpdf_formfill.h"
-
-class CPDFSDK_FormFillEnvironment;
-class CPDFSDK_InterForm;
-class CPDFSDK_PageView;
-class IJS_Runtime;
-
-class CPDFSDK_Document : public CFX_Observable<CPDFSDK_Document> {
- public:
-  CPDFSDK_Document(UnderlyingDocumentType* pDoc,
-                   CPDFSDK_FormFillEnvironment* pEnv);
-  ~CPDFSDK_Document();
-
-  CPDFSDK_InterForm* GetInterForm();
-
-  // Gets the document object for the next layer down; for master this is
-  // a CPDF_Document, but for XFA it is a CPDFXFA_Document.
-  UnderlyingDocumentType* GetUnderlyingDocument() const {
-#ifdef PDF_ENABLE_XFA
-    return GetXFADocument();
-#else   // PDF_ENABLE_XFA
-    return GetPDFDocument();
-#endif  // PDF_ENABLE_XFA
-  }
-
-  // Gets the CPDF_Document, either directly in master, or from the
-  // CPDFXFA_Document for XFA.
-  CPDF_Document* GetPDFDocument() const {
-#ifdef PDF_ENABLE_XFA
-    return m_pDoc ? m_pDoc->GetPDFDoc() : nullptr;
-#else   // PDF_ENABLE_XFA
-    return m_pDoc;
-#endif  // PDF_ENABLE_XFA
-  }
-
-#ifdef PDF_ENABLE_XFA
-  // Gets the XFA document directly (XFA-only).
-  CPDFXFA_Document* GetXFADocument() const { return m_pDoc; }
-  void ResetXFADocument() { m_pDoc = nullptr; }
-
-  int GetPageViewCount() const { return m_pageMap.size(); }
-#endif  // PDF_ENABLE_XFA
-
-  CPDFSDK_PageView* GetPageView(UnderlyingPageType* pPage, bool ReNew);
-  CPDFSDK_PageView* GetPageView(int nIndex);
-  CPDFSDK_PageView* GetCurrentView();
-  void RemovePageView(UnderlyingPageType* pPage);
-  void UpdateAllViews(CPDFSDK_PageView* pSender, CPDFSDK_Annot* pAnnot);
-
-  IJS_Runtime* GetJsRuntime();
-
-  CPDFSDK_Annot* GetFocusAnnot() { return m_pFocusAnnot.Get(); }
-  FX_BOOL SetFocusAnnot(CPDFSDK_Annot::ObservedPtr* pAnnot);
-  FX_BOOL KillFocusAnnot(uint32_t nFlag);
-  void ClearAllFocusedAnnots();
-
-  FX_BOOL ExtractPages(const std::vector<uint16_t>& arrExtraPages,
-                       CPDF_Document* pDstDoc);
-  FX_BOOL InsertPages(int nInsertAt,
-                      const CPDF_Document* pSrcDoc,
-                      const std::vector<uint16_t>& arrSrcPages);
-  FX_BOOL ReplacePages(int nPage,
-                       const CPDF_Document* pSrcDoc,
-                       const std::vector<uint16_t>& arrSrcPages);
-
-  void OnCloseDocument();
-
-  int GetPageCount() { return m_pDoc->GetPageCount(); }
-  FX_BOOL GetPermissions(int nFlag);
-  FX_BOOL GetChangeMark() { return m_bChangeMask; }
-  void SetChangeMark() { m_bChangeMask = TRUE; }
-  void ClearChangeMark() { m_bChangeMask = FALSE; }
-  CFX_WideString GetPath();
-  UnderlyingPageType* GetPage(int nIndex);
-  CPDFSDK_FormFillEnvironment* GetEnv() { return m_pEnv; }
-  void ProcJavascriptFun();
-  FX_BOOL ProcOpenAction();
-
- private:
-  std::map<UnderlyingPageType*, CPDFSDK_PageView*> m_pageMap;
-  UnderlyingDocumentType* m_pDoc;
-  std::unique_ptr<CPDFSDK_InterForm> m_pInterForm;
-  CPDFSDK_Annot::ObservedPtr m_pFocusAnnot;
-  CPDFSDK_FormFillEnvironment* m_pEnv;
-  FX_BOOL m_bChangeMask;
-  FX_BOOL m_bBeingDestroyed;
-};
-
-#endif  // FPDFSDK_CPDFSDK_DOCUMENT_H_
diff --git a/fpdfsdk/cpdfsdk_formfillenvironment.cpp b/fpdfsdk/cpdfsdk_formfillenvironment.cpp
index 7b41ea1..2beaade 100644
--- a/fpdfsdk/cpdfsdk_formfillenvironment.cpp
+++ b/fpdfsdk/cpdfsdk_formfillenvironment.cpp
@@ -8,8 +8,12 @@
 
 #include <memory>
 
+#include "core/fpdfapi/parser/cpdf_array.h"
+#include "core/fpdfdoc/cpdf_docjsactions.h"
 #include "fpdfsdk/cpdfsdk_annothandlermgr.h"
-#include "fpdfsdk/cpdfsdk_document.h"
+#include "fpdfsdk/cpdfsdk_interform.h"
+#include "fpdfsdk/cpdfsdk_pageview.h"
+#include "fpdfsdk/cpdfsdk_widget.h"
 #include "fpdfsdk/formfiller/cffl_interactiveformfiller.h"
 #include "fpdfsdk/fsdk_actionhandler.h"
 #include "fpdfsdk/javascript/ijs_runtime.h"
@@ -34,11 +38,19 @@
     UnderlyingDocumentType* pDoc,
     FPDF_FORMFILLINFO* pFFinfo)
     : m_pInfo(pFFinfo),
-      m_pSDKDoc(new CPDFSDK_Document(pDoc, this)),
       m_pUnderlyingDoc(pDoc),
-      m_pSysHandler(new CFX_SystemHandler(this)) {}
+      m_pSysHandler(new CFX_SystemHandler(this)),
+      m_bChangeMask(FALSE),
+      m_bBeingDestroyed(FALSE) {}
 
 CPDFSDK_FormFillEnvironment::~CPDFSDK_FormFillEnvironment() {
+  m_bBeingDestroyed = TRUE;
+
+  ClearAllFocusedAnnots();
+  for (auto& it : m_pageMap)
+    delete it.second;
+  m_pageMap.clear();
+
   // |m_pAnnotHandlerMgr| will try to access |m_pFormFiller|
   // when it cleans up. So, we must make sure it is cleaned up before
   // |m_pFormFiller|.
@@ -562,3 +574,202 @@
     m_pInfo->FFI_PageEvent(m_pInfo, iPageCount, dwEventType);
 }
 #endif  // PDF_ENABLE_XFA
+
+void CPDFSDK_FormFillEnvironment::ClearAllFocusedAnnots() {
+  for (auto& it : m_pageMap) {
+    if (it.second->IsValidSDKAnnot(GetFocusAnnot()))
+      KillFocusAnnot(0);
+  }
+}
+
+CPDFSDK_PageView* CPDFSDK_FormFillEnvironment::GetPageView(
+    UnderlyingPageType* pUnderlyingPage,
+    bool ReNew) {
+  auto it = m_pageMap.find(pUnderlyingPage);
+  if (it != m_pageMap.end())
+    return it->second;
+
+  if (!ReNew)
+    return nullptr;
+
+  CPDFSDK_PageView* pPageView = new CPDFSDK_PageView(this, pUnderlyingPage);
+  m_pageMap[pUnderlyingPage] = pPageView;
+  // Delay to load all the annotations, to avoid endless loop.
+  pPageView->LoadFXAnnots();
+  return pPageView;
+}
+
+CPDFSDK_PageView* CPDFSDK_FormFillEnvironment::GetCurrentView() {
+  UnderlyingPageType* pPage =
+      UnderlyingFromFPDFPage(GetCurrentPage(m_pUnderlyingDoc));
+  return pPage ? GetPageView(pPage, true) : nullptr;
+}
+
+CPDFSDK_PageView* CPDFSDK_FormFillEnvironment::GetPageView(int nIndex) {
+  UnderlyingPageType* pTempPage =
+      UnderlyingFromFPDFPage(GetPage(m_pUnderlyingDoc, nIndex));
+  if (!pTempPage)
+    return nullptr;
+
+  auto it = m_pageMap.find(pTempPage);
+  return it != m_pageMap.end() ? it->second : nullptr;
+}
+
+void CPDFSDK_FormFillEnvironment::ProcJavascriptFun() {
+  CPDF_Document* pPDFDoc = GetPDFDocument();
+  CPDF_DocJSActions docJS(pPDFDoc);
+  int iCount = docJS.CountJSActions();
+  if (iCount < 1)
+    return;
+  for (int i = 0; i < iCount; i++) {
+    CFX_ByteString csJSName;
+    CPDF_Action jsAction = docJS.GetJSAction(i, csJSName);
+    if (GetActionHander()) {
+      GetActionHander()->DoAction_JavaScript(
+          jsAction, CFX_WideString::FromLocal(csJSName.AsStringC()), this);
+    }
+  }
+}
+
+FX_BOOL CPDFSDK_FormFillEnvironment::ProcOpenAction() {
+  if (!m_pUnderlyingDoc)
+    return FALSE;
+
+  CPDF_Dictionary* pRoot = GetPDFDocument()->GetRoot();
+  if (!pRoot)
+    return FALSE;
+
+  CPDF_Object* pOpenAction = pRoot->GetDictFor("OpenAction");
+  if (!pOpenAction)
+    pOpenAction = pRoot->GetArrayFor("OpenAction");
+
+  if (!pOpenAction)
+    return FALSE;
+
+  if (pOpenAction->IsArray())
+    return TRUE;
+
+  if (CPDF_Dictionary* pDict = pOpenAction->AsDictionary()) {
+    CPDF_Action action(pDict);
+    if (GetActionHander())
+      GetActionHander()->DoAction_DocOpen(action, this);
+    return TRUE;
+  }
+  return FALSE;
+}
+
+void CPDFSDK_FormFillEnvironment::RemovePageView(
+    UnderlyingPageType* pUnderlyingPage) {
+  auto it = m_pageMap.find(pUnderlyingPage);
+  if (it == m_pageMap.end())
+    return;
+
+  CPDFSDK_PageView* pPageView = it->second;
+  if (pPageView->IsLocked() || pPageView->IsBeingDestroyed())
+    return;
+
+  // Mark the page view so we do not come into |RemovePageView| a second
+  // time while we're in the process of removing.
+  pPageView->SetBeingDestroyed();
+
+  // This must happen before we remove |pPageView| from the map because
+  // |KillFocusAnnot| can call into the |GetPage| method which will
+  // look for this page view in the map, if it doesn't find it a new one will
+  // be created. We then have two page views pointing to the same page and
+  // bad things happen.
+  if (pPageView->IsValidSDKAnnot(GetFocusAnnot()))
+    KillFocusAnnot(0);
+
+  // Remove the page from the map to make sure we don't accidentally attempt
+  // to use the |pPageView| while we're cleaning it up.
+  m_pageMap.erase(it);
+
+  delete pPageView;
+}
+
+UnderlyingPageType* CPDFSDK_FormFillEnvironment::GetPage(int nIndex) {
+  return UnderlyingFromFPDFPage(GetPage(m_pUnderlyingDoc, nIndex));
+}
+
+CPDFSDK_InterForm* CPDFSDK_FormFillEnvironment::GetInterForm() {
+  if (!m_pInterForm)
+    m_pInterForm = pdfium::MakeUnique<CPDFSDK_InterForm>(this);
+  return m_pInterForm.get();
+}
+
+void CPDFSDK_FormFillEnvironment::UpdateAllViews(CPDFSDK_PageView* pSender,
+                                                 CPDFSDK_Annot* pAnnot) {
+  for (const auto& it : m_pageMap) {
+    CPDFSDK_PageView* pPageView = it.second;
+    if (pPageView != pSender)
+      pPageView->UpdateView(pAnnot);
+  }
+}
+
+FX_BOOL CPDFSDK_FormFillEnvironment::SetFocusAnnot(
+    CPDFSDK_Annot::ObservedPtr* pAnnot) {
+  if (m_bBeingDestroyed)
+    return FALSE;
+  if (m_pFocusAnnot == *pAnnot)
+    return TRUE;
+  if (m_pFocusAnnot && !KillFocusAnnot(0))
+    return FALSE;
+  if (!*pAnnot)
+    return FALSE;
+
+#ifdef PDF_ENABLE_XFA
+  CPDFSDK_Annot::ObservedPtr pLastFocusAnnot(m_pFocusAnnot.Get());
+#endif  // PDF_ENABLE_XFA
+  CPDFSDK_PageView* pPageView = (*pAnnot)->GetPageView();
+  if (pPageView && pPageView->IsValid()) {
+    CPDFSDK_AnnotHandlerMgr* pAnnotHandler = GetAnnotHandlerMgr();
+    if (!m_pFocusAnnot) {
+#ifdef PDF_ENABLE_XFA
+      if (!pAnnotHandler->Annot_OnChangeFocus(pAnnot, &pLastFocusAnnot))
+        return FALSE;
+#endif  // PDF_ENABLE_XFA
+      if (!pAnnotHandler->Annot_OnSetFocus(pAnnot, 0))
+        return FALSE;
+      if (!m_pFocusAnnot) {
+        m_pFocusAnnot.Reset(pAnnot->Get());
+        return TRUE;
+      }
+    }
+  }
+  return FALSE;
+}
+
+FX_BOOL CPDFSDK_FormFillEnvironment::KillFocusAnnot(uint32_t nFlag) {
+  if (m_pFocusAnnot) {
+    CPDFSDK_AnnotHandlerMgr* pAnnotHandler = GetAnnotHandlerMgr();
+    CPDFSDK_Annot::ObservedPtr pFocusAnnot(m_pFocusAnnot.Get());
+    m_pFocusAnnot.Reset();
+
+#ifdef PDF_ENABLE_XFA
+    CPDFSDK_Annot::ObservedPtr pNull;
+    if (!pAnnotHandler->Annot_OnChangeFocus(&pNull, &pFocusAnnot))
+      return FALSE;
+#endif  // PDF_ENABLE_XFA
+
+    if (pAnnotHandler->Annot_OnKillFocus(&pFocusAnnot, nFlag)) {
+      if (pFocusAnnot->GetAnnotSubtype() == CPDF_Annot::Subtype::WIDGET) {
+        CPDFSDK_Widget* pWidget =
+            static_cast<CPDFSDK_Widget*>(pFocusAnnot.Get());
+        int nFieldType = pWidget->GetFieldType();
+        if (FIELDTYPE_TEXTFIELD == nFieldType ||
+            FIELDTYPE_COMBOBOX == nFieldType) {
+          OnSetFieldInputFocus(nullptr, 0, FALSE);
+        }
+      }
+      if (!m_pFocusAnnot)
+        return TRUE;
+    } else {
+      m_pFocusAnnot.Reset(pFocusAnnot.Get());
+    }
+  }
+  return FALSE;
+}
+
+FX_BOOL CPDFSDK_FormFillEnvironment::GetPermissions(int nFlag) {
+  return GetPDFDocument()->GetUserPermissions() & nFlag;
+}
diff --git a/fpdfsdk/cpdfsdk_formfillenvironment.h b/fpdfsdk/cpdfsdk_formfillenvironment.h
index e5a6932..dd0e6bf 100644
--- a/fpdfsdk/cpdfsdk_formfillenvironment.h
+++ b/fpdfsdk/cpdfsdk_formfillenvironment.h
@@ -14,6 +14,7 @@
 #include "core/fpdfdoc/cpdf_occontext.h"
 #include "core/fxcrt/cfx_observable.h"
 #include "fpdfsdk/cfx_systemhandler.h"
+#include "fpdfsdk/cpdfsdk_annot.h"
 #include "fpdfsdk/fsdk_define.h"
 #include "public/fpdf_formfill.h"
 #include "public/fpdf_fwlevent.h"
@@ -22,7 +23,8 @@
 class CFX_SystemHandler;
 class CPDFSDK_ActionHandler;
 class CPDFSDK_AnnotHandlerMgr;
-class CPDFSDK_Document;
+class CPDFSDK_InterForm;
+class CPDFSDK_PageView;
 class IJS_Runtime;
 
 class CPDFSDK_FormFillEnvironment
@@ -32,6 +34,68 @@
                               FPDF_FORMFILLINFO* pFFinfo);
   ~CPDFSDK_FormFillEnvironment();
 
+  CPDFSDK_InterForm* GetInterForm();
+
+  // Gets the document object for the next layer down; for master this is
+  // a CPDF_Document, but for XFA it is a CPDFXFA_Document.
+  UnderlyingDocumentType* GetUnderlyingDocument() const {
+#ifdef PDF_ENABLE_XFA
+    return GetXFADocument();
+#else   // PDF_ENABLE_XFA
+    return GetPDFDocument();
+#endif  // PDF_ENABLE_XFA
+  }
+
+  // Gets the CPDF_Document, either directly in master, or from the
+  // CPDFXFA_Document for XFA.
+  CPDF_Document* GetPDFDocument() const {
+#ifdef PDF_ENABLE_XFA
+    return m_pUnderlyingDoc ? m_pUnderlyingDoc->GetPDFDoc() : nullptr;
+#else   // PDF_ENABLE_XFA
+    return m_pUnderlyingDoc;
+#endif  // PDF_ENABLE_XFA
+  }
+
+#ifdef PDF_ENABLE_XFA
+  // Gets the XFA document directly (XFA-only).
+  CPDFXFA_Document* GetXFADocument() const { return m_pUnderlyingDoc; }
+  void ResetXFADocument() { m_pUnderlyingDoc = nullptr; }
+
+  int GetPageViewCount() const { return m_pageMap.size(); }
+#endif  // PDF_ENABLE_XFA
+
+  CPDFSDK_PageView* GetPageView(UnderlyingPageType* pPage, bool ReNew);
+  CPDFSDK_PageView* GetPageView(int nIndex);
+  CPDFSDK_PageView* GetCurrentView();
+  void RemovePageView(UnderlyingPageType* pPage);
+  void UpdateAllViews(CPDFSDK_PageView* pSender, CPDFSDK_Annot* pAnnot);
+
+  CPDFSDK_Annot* GetFocusAnnot() { return m_pFocusAnnot.Get(); }
+  FX_BOOL SetFocusAnnot(CPDFSDK_Annot::ObservedPtr* pAnnot);
+  FX_BOOL KillFocusAnnot(uint32_t nFlag);
+  void ClearAllFocusedAnnots();
+
+  FX_BOOL ExtractPages(const std::vector<uint16_t>& arrExtraPages,
+                       CPDF_Document* pDstDoc);
+  FX_BOOL InsertPages(int nInsertAt,
+                      const CPDF_Document* pSrcDoc,
+                      const std::vector<uint16_t>& arrSrcPages);
+  FX_BOOL ReplacePages(int nPage,
+                       const CPDF_Document* pSrcDoc,
+                       const std::vector<uint16_t>& arrSrcPages);
+
+  int GetPageCount() { return m_pUnderlyingDoc->GetPageCount(); }
+  FX_BOOL GetPermissions(int nFlag);
+
+  FX_BOOL GetChangeMark() { return m_bChangeMask; }
+  void SetChangeMark() { m_bChangeMask = TRUE; }
+  void ClearChangeMark() { m_bChangeMask = FALSE; }
+
+  UnderlyingPageType* GetPage(int nIndex);
+
+  void ProcJavascriptFun();
+  FX_BOOL ProcOpenAction();
+
   void Invalidate(FPDF_PAGE page,
                   double left,
                   double top,
@@ -148,10 +212,6 @@
   void JS_docgotoPage(int nPageNum);
 
   FX_BOOL IsJSInitiated() const { return m_pInfo && m_pInfo->m_pJsPlatform; }
-  CPDFSDK_Document* GetSDKDocument() const { return m_pSDKDoc.get(); }
-  UnderlyingDocumentType* GetUnderlyingDocument() const {
-    return m_pUnderlyingDoc;
-  }
   CFX_ByteString GetAppName() const { return ""; }
   CFX_SystemHandler* GetSysHandler() const { return m_pSysHandler.get(); }
   FPDF_FORMFILLINFO* GetFormFillInfo() const { return m_pInfo; }
@@ -167,10 +227,14 @@
   std::unique_ptr<CPDFSDK_ActionHandler> m_pActionHandler;
   std::unique_ptr<IJS_Runtime> m_pJSRuntime;
   FPDF_FORMFILLINFO* const m_pInfo;
-  std::unique_ptr<CPDFSDK_Document> m_pSDKDoc;
-  UnderlyingDocumentType* const m_pUnderlyingDoc;
+  std::map<UnderlyingPageType*, CPDFSDK_PageView*> m_pageMap;
+  std::unique_ptr<CPDFSDK_InterForm> m_pInterForm;
+  CPDFSDK_Annot::ObservedPtr m_pFocusAnnot;
+  UnderlyingDocumentType* m_pUnderlyingDoc;
   std::unique_ptr<CFFL_InteractiveFormFiller> m_pFormFiller;
   std::unique_ptr<CFX_SystemHandler> m_pSysHandler;
+  FX_BOOL m_bChangeMask;
+  FX_BOOL m_bBeingDestroyed;
 };
 
 #endif  // FPDFSDK_CPDFSDK_FORMFILLENVIRONMENT_H_
diff --git a/fpdfsdk/cpdfsdk_interform.cpp b/fpdfsdk/cpdfsdk_interform.cpp
index 005079d..81836f7 100644
--- a/fpdfsdk/cpdfsdk_interform.cpp
+++ b/fpdfsdk/cpdfsdk_interform.cpp
@@ -22,7 +22,6 @@
 #include "core/fxge/cfx_renderdevice.h"
 #include "fpdfsdk/cba_annotiterator.h"
 #include "fpdfsdk/cpdfsdk_annot.h"
-#include "fpdfsdk/cpdfsdk_document.h"
 #include "fpdfsdk/cpdfsdk_formfillenvironment.h"
 #include "fpdfsdk/cpdfsdk_pageview.h"
 #include "fpdfsdk/cpdfsdk_widget.h"
@@ -48,8 +47,7 @@
 
 CPDFSDK_InterForm::CPDFSDK_InterForm(CPDFSDK_FormFillEnvironment* pFormFillEnv)
     : m_pFormFillEnv(pFormFillEnv),
-      m_pInterForm(new CPDF_InterForm(
-          m_pFormFillEnv->GetSDKDocument()->GetPDFDocument())),
+      m_pInterForm(new CPDF_InterForm(m_pFormFillEnv->GetPDFDocument())),
 #ifdef PDF_ENABLE_XFA
       m_bXfaCalculate(TRUE),
       m_bXfaValidationsEnabled(TRUE),
@@ -99,19 +97,19 @@
     return nullptr;
 
   CPDF_Dictionary* pControlDict = pControl->GetWidget();
-  CPDF_Document* pDocument = m_pFormFillEnv->GetSDKDocument()->GetPDFDocument();
+  CPDF_Document* pDocument = m_pFormFillEnv->GetPDFDocument();
   CPDFSDK_PageView* pPage = nullptr;
 
   if (CPDF_Dictionary* pPageDict = pControlDict->GetDictFor("P")) {
     int nPageIndex = pDocument->GetPageIndex(pPageDict->GetObjNum());
     if (nPageIndex >= 0)
-      pPage = m_pFormFillEnv->GetSDKDocument()->GetPageView(nPageIndex);
+      pPage = m_pFormFillEnv->GetPageView(nPageIndex);
   }
 
   if (!pPage) {
     int nPageIndex = GetPageIndexByAnnotDict(pDocument, pControlDict);
     if (nPageIndex >= 0)
-      pPage = m_pFormFillEnv->GetSDKDocument()->GetPageView(nPageIndex);
+      pPage = m_pFormFillEnv->GetPageView(nPageIndex);
   }
 
   if (!pPage)
@@ -336,8 +334,7 @@
 
     if (CPDFSDK_Widget* pWidget = GetWidget(pFormCtrl, false)) {
       UnderlyingPageType* pPage = pWidget->GetUnderlyingPage();
-      CPDFSDK_PageView* pPageView =
-          m_pFormFillEnv->GetSDKDocument()->GetPageView(pPage, false);
+      CPDFSDK_PageView* pPageView = m_pFormFillEnv->GetPageView(pPage, false);
       FX_RECT rcBBox = m_pFormFillEnv->GetInteractiveFormFiller()->GetViewBBox(
           pPageView, pWidget);
 
@@ -513,9 +510,9 @@
     const std::vector<CPDF_FormField*>& fields,
     bool bIncludeOrExclude,
     CFX_ByteTextBuf& textBuf) {
-  std::unique_ptr<CFDF_Document> pFDF(m_pInterForm->ExportToFDF(
-      m_pFormFillEnv->GetSDKDocument()->GetPath().AsStringC(), fields,
-      bIncludeOrExclude, false));
+  std::unique_ptr<CFDF_Document> pFDF(
+      m_pInterForm->ExportToFDF(m_pFormFillEnv->JS_docGetFilePath().AsStringC(),
+                                fields, bIncludeOrExclude, false));
   return pFDF ? pFDF->WriteBuf(textBuf) : FALSE;
 }
 
@@ -529,10 +526,10 @@
   if (sDestination.IsEmpty())
     return FALSE;
 
-  if (!m_pFormFillEnv || !m_pFormFillEnv->GetSDKDocument() || !m_pInterForm)
+  if (!m_pFormFillEnv || !m_pInterForm)
     return FALSE;
 
-  CFX_WideString wsPDFFilePath = m_pFormFillEnv->GetSDKDocument()->GetPath();
+  CFX_WideString wsPDFFilePath = m_pFormFillEnv->JS_docGetFilePath();
   CFDF_Document* pFDFDoc =
       m_pInterForm->ExportToFDF(wsPDFFilePath.AsStringC(), false);
   if (!pFDFDoc)
@@ -560,7 +557,7 @@
 
 FX_BOOL CPDFSDK_InterForm::ExportFormToFDFTextBuf(CFX_ByteTextBuf& textBuf) {
   CFDF_Document* pFDF = m_pInterForm->ExportToFDF(
-      m_pFormFillEnv->GetSDKDocument()->GetPath().AsStringC(), false);
+      m_pFormFillEnv->JS_docGetFilePath().AsStringC(), false);
   if (!pFDF)
     return FALSE;
 
diff --git a/fpdfsdk/cpdfsdk_pageview.cpp b/fpdfsdk/cpdfsdk_pageview.cpp
index a5c60d6..348f2d1 100644
--- a/fpdfsdk/cpdfsdk_pageview.cpp
+++ b/fpdfsdk/cpdfsdk_pageview.cpp
@@ -42,8 +42,7 @@
       m_bValid(FALSE),
       m_bLocked(FALSE),
       m_bBeingDestroyed(false) {
-  CPDFSDK_InterForm* pInterForm =
-      pFormFillEnv->GetSDKDocument()->GetInterForm();
+  CPDFSDK_InterForm* pInterForm = pFormFillEnv->GetInterForm();
   if (pInterForm) {
     CPDF_InterForm* pPDFInterForm = pInterForm->GetInterForm();
 #ifdef PDF_ENABLE_XFA
@@ -197,7 +196,7 @@
     return FALSE;
 
   if (GetFocusAnnot() == pAnnot)
-    m_pFormFillEnv->GetSDKDocument()->KillFocusAnnot(0);
+    m_pFormFillEnv->KillFocusAnnot(0);
   CPDFSDK_AnnotHandlerMgr* pAnnotHandler = m_pFormFillEnv->GetAnnotHandlerMgr();
   if (pAnnotHandler)
     pAnnotHandler->ReleaseAnnot(pAnnot);
@@ -256,7 +255,7 @@
                                         uint32_t nFlag) {
   CPDFSDK_Annot::ObservedPtr pAnnot(GetFXWidgetAtPoint(point.x, point.y));
   if (!pAnnot) {
-    m_pFormFillEnv->GetSDKDocument()->KillFocusAnnot(nFlag);
+    m_pFormFillEnv->KillFocusAnnot(nFlag);
     return FALSE;
   }
 
@@ -268,7 +267,7 @@
   if (!pAnnot)
     return FALSE;
 
-  m_pFormFillEnv->GetSDKDocument()->SetFocusAnnot(&pAnnot);
+  m_pFormFillEnv->SetFocusAnnot(&pAnnot);
   return TRUE;
 }
 
@@ -287,7 +286,7 @@
     return FALSE;
 
   if (ok)
-    m_pFormFillEnv->GetSDKDocument()->SetFocusAnnot(&pAnnot);
+    m_pFormFillEnv->SetFocusAnnot(&pAnnot);
 
   return TRUE;
 }
@@ -301,7 +300,7 @@
     return FALSE;
 
   if (pAnnotHandlerMgr->Annot_OnRButtonUp(this, &pFXAnnot, nFlag, point))
-    m_pFormFillEnv->GetSDKDocument()->SetFocusAnnot(&pFXAnnot);
+    m_pFormFillEnv->SetFocusAnnot(&pFXAnnot);
 
   return TRUE;
 }
@@ -399,8 +398,7 @@
 
 #ifdef PDF_ENABLE_XFA
   CFX_RetainPtr<CPDFXFA_Page> protector(m_page);
-  if (m_pFormFillEnv->GetSDKDocument()->GetXFADocument()->GetDocType() ==
-      DOCTYPE_DYNAMIC_XFA) {
+  if (m_pFormFillEnv->GetXFADocument()->GetDocType() == DOCTYPE_DYNAMIC_XFA) {
     CXFA_FFPageView* pageView = m_page->GetXFAPageView();
     std::unique_ptr<IXFA_WidgetIterator> pWidgetHander(
         pageView->CreateWidgetIterator(
@@ -498,13 +496,12 @@
 }
 
 CPDFSDK_Annot* CPDFSDK_PageView::GetFocusAnnot() {
-  CPDFSDK_Annot* pFocusAnnot =
-      m_pFormFillEnv->GetSDKDocument()->GetFocusAnnot();
+  CPDFSDK_Annot* pFocusAnnot = m_pFormFillEnv->GetFocusAnnot();
   return IsValidSDKAnnot(pFocusAnnot) ? pFocusAnnot : nullptr;
 }
 
 int CPDFSDK_PageView::GetPageIndexForStaticPDF() const {
   CPDF_Dictionary* pDict = GetPDFPage()->m_pFormDict;
-  CPDF_Document* pDoc = m_pFormFillEnv->GetSDKDocument()->GetPDFDocument();
+  CPDF_Document* pDoc = m_pFormFillEnv->GetPDFDocument();
   return (pDoc && pDict) ? pDoc->GetPageIndex(pDict->GetObjNum()) : -1;
 }
diff --git a/fpdfsdk/cpdfsdk_pageview.h b/fpdfsdk/cpdfsdk_pageview.h
index a265436..6878324 100644
--- a/fpdfsdk/cpdfsdk_pageview.h
+++ b/fpdfsdk/cpdfsdk_pageview.h
@@ -13,7 +13,6 @@
 #include "core/fpdfapi/page/cpdf_page.h"
 #include "core/fxcrt/fx_system.h"
 #include "fpdfsdk/cpdfsdk_annot.h"
-#include "fpdfsdk/cpdfsdk_document.h"
 
 class CFX_RenderDevice;
 class CPDF_AnnotList;
diff --git a/fpdfsdk/cpdfsdk_widget.cpp b/fpdfsdk/cpdfsdk_widget.cpp
index 4ce7d9a..e1624a0 100644
--- a/fpdfsdk/cpdfsdk_widget.cpp
+++ b/fpdfsdk/cpdfsdk_widget.cpp
@@ -19,7 +19,6 @@
 #include "core/fxge/cfx_graphstatedata.h"
 #include "core/fxge/cfx_pathdata.h"
 #include "core/fxge/cfx_renderdevice.h"
-#include "fpdfsdk/cpdfsdk_document.h"
 #include "fpdfsdk/cpdfsdk_formfillenvironment.h"
 #include "fpdfsdk/cpdfsdk_interform.h"
 #include "fpdfsdk/cpdfsdk_pageview.h"
@@ -69,8 +68,7 @@
 
 #ifdef PDF_ENABLE_XFA
 CXFA_FFWidget* CPDFSDK_Widget::GetMixXFAWidget() const {
-  CPDFXFA_Document* pDoc =
-      m_pPageView->GetFormFillEnv()->GetSDKDocument()->GetXFADocument();
+  CPDFXFA_Document* pDoc = m_pPageView->GetFormFillEnv()->GetXFADocument();
   if (pDoc->GetDocType() == DOCTYPE_STATIC_XFA) {
     if (!m_hMixXFAWidget) {
       if (CXFA_FFDocView* pDocView = pDoc->GetXFADocView()) {
@@ -94,8 +92,7 @@
 }
 
 CXFA_FFWidget* CPDFSDK_Widget::GetGroupMixXFAWidget() {
-  CPDFXFA_Document* pDoc =
-      m_pPageView->GetFormFillEnv()->GetSDKDocument()->GetXFADocument();
+  CPDFXFA_Document* pDoc = m_pPageView->GetFormFillEnv()->GetXFADocument();
   if (pDoc->GetDocType() == DOCTYPE_STATIC_XFA) {
     if (CXFA_FFDocView* pDocView = pDoc->GetXFADocView()) {
       CFX_WideString sName = GetName();
@@ -108,8 +105,7 @@
 }
 
 CXFA_FFWidgetHandler* CPDFSDK_Widget::GetXFAWidgetHandler() const {
-  CPDFXFA_Document* pDoc =
-      m_pPageView->GetFormFillEnv()->GetSDKDocument()->GetXFADocument();
+  CPDFXFA_Document* pDoc = m_pPageView->GetFormFillEnv()->GetXFADocument();
   if (pDoc->GetDocType() == DOCTYPE_STATIC_XFA) {
     if (!m_pWidgetHandler) {
       if (CXFA_FFDocView* pDocView = pDoc->GetXFADocView())
@@ -223,8 +219,7 @@
 FX_BOOL CPDFSDK_Widget::OnXFAAAction(PDFSDK_XFAAActionType eXFAAAT,
                                      PDFSDK_FieldAction& data,
                                      CPDFSDK_PageView* pPageView) {
-  CPDFXFA_Document* pDoc =
-      m_pPageView->GetFormFillEnv()->GetSDKDocument()->GetXFADocument();
+  CPDFXFA_Document* pDoc = m_pPageView->GetFormFillEnv()->GetXFADocument();
 
   CXFA_FFWidget* hWidget = GetMixXFAWidget();
   if (!hWidget)
@@ -327,8 +322,7 @@
 }
 
 void CPDFSDK_Widget::SynchronizeXFAValue() {
-  CPDFXFA_Document* pDoc =
-      m_pPageView->GetFormFillEnv()->GetSDKDocument()->GetXFADocument();
+  CPDFXFA_Document* pDoc = m_pPageView->GetFormFillEnv()->GetXFADocument();
   CXFA_FFDocView* pXFADocView = pDoc->GetXFADocView();
   if (!pXFADocView)
     return;
@@ -342,8 +336,7 @@
 }
 
 void CPDFSDK_Widget::SynchronizeXFAItems() {
-  CPDFXFA_Document* pDoc =
-      m_pPageView->GetFormFillEnv()->GetSDKDocument()->GetXFADocument();
+  CPDFXFA_Document* pDoc = m_pPageView->GetFormFillEnv()->GetXFADocument();
   CXFA_FFDocView* pXFADocView = pDoc->GetXFADocView();
   if (!pXFADocView)
     return;
@@ -509,8 +502,7 @@
 
 FX_BOOL CPDFSDK_Widget::IsAppearanceValid() {
 #ifdef PDF_ENABLE_XFA
-  CPDFXFA_Document* pDoc =
-      m_pPageView->GetFormFillEnv()->GetSDKDocument()->GetXFADocument();
+  CPDFXFA_Document* pDoc = m_pPageView->GetFormFillEnv()->GetXFADocument();
   int nDocType = pDoc->GetDocType();
   if (nDocType != DOCTYPE_PDF && nDocType != DOCTYPE_STATIC_XFA)
     return TRUE;
@@ -1834,7 +1826,7 @@
   CPDFSDK_FormFillEnvironment* pFormFillEnv = pPageView->GetFormFillEnv();
 
 #ifdef PDF_ENABLE_XFA
-  CPDFXFA_Document* pDoc = pFormFillEnv->GetSDKDocument()->GetXFADocument();
+  CPDFXFA_Document* pDoc = pFormFillEnv->GetXFADocument();
   if (CXFA_FFWidget* hWidget = GetMixXFAWidget()) {
     XFA_EVENTTYPE eEventType = GetXFAEventType(type, data.bWillCommit);
 
diff --git a/fpdfsdk/cpdfsdk_widgethandler.cpp b/fpdfsdk/cpdfsdk_widgethandler.cpp
index 2509eb6..a75e6c7 100644
--- a/fpdfsdk/cpdfsdk_widgethandler.cpp
+++ b/fpdfsdk/cpdfsdk_widgethandler.cpp
@@ -13,7 +13,6 @@
 #include "core/fpdfapi/parser/cpdf_document.h"
 #include "core/fpdfdoc/cpdf_interform.h"
 #include "fpdfsdk/cpdfsdk_annot.h"
-#include "fpdfsdk/cpdfsdk_document.h"
 #include "fpdfsdk/cpdfsdk_formfillenvironment.h"
 #include "fpdfsdk/cpdfsdk_interform.h"
 #include "fpdfsdk/cpdfsdk_pageview.h"
@@ -54,7 +53,7 @@
 
 CPDFSDK_Annot* CPDFSDK_WidgetHandler::NewAnnot(CPDF_Annot* pAnnot,
                                                CPDFSDK_PageView* pPage) {
-  CPDFSDK_InterForm* pInterForm = m_pEnv->GetSDKDocument()->GetInterForm();
+  CPDFSDK_InterForm* pInterForm = m_pEnv->GetInterForm();
   CPDF_FormControl* pCtrl = CPDFSDK_Widget::GetFormControl(
       pInterForm->GetInterForm(), pAnnot->GetAnnotDict());
   if (!pCtrl)
@@ -239,8 +238,7 @@
 
 #ifdef PDF_ENABLE_XFA
   CPDFSDK_PageView* pPageView = pAnnot->GetPageView();
-  CPDFXFA_Document* pDoc =
-      pPageView->GetFormFillEnv()->GetSDKDocument()->GetXFADocument();
+  CPDFXFA_Document* pDoc = pPageView->GetFormFillEnv()->GetXFADocument();
   if (pDoc->GetDocType() == DOCTYPE_STATIC_XFA) {
     if (!pWidget->IsAppearanceValid() && !pWidget->GetValue().IsEmpty())
       pWidget->ResetAppearance(FALSE);
diff --git a/fpdfsdk/cpdfsdk_xfawidgethandler.cpp b/fpdfsdk/cpdfsdk_xfawidgethandler.cpp
index 48fda86..a7310a7 100644
--- a/fpdfsdk/cpdfsdk_xfawidgethandler.cpp
+++ b/fpdfsdk/cpdfsdk_xfawidgethandler.cpp
@@ -8,7 +8,6 @@
 
 #include "core/fpdfdoc/cpdf_interform.h"
 #include "fpdfsdk/cpdfsdk_annot.h"
-#include "fpdfsdk/cpdfsdk_document.h"
 #include "fpdfsdk/cpdfsdk_formfillenvironment.h"
 #include "fpdfsdk/cpdfsdk_interform.h"
 #include "fpdfsdk/cpdfsdk_pageview.h"
@@ -39,7 +38,7 @@
 
 CPDFSDK_Annot* CPDFSDK_XFAWidgetHandler::NewAnnot(CXFA_FFWidget* pAnnot,
                                                   CPDFSDK_PageView* pPage) {
-  CPDFSDK_InterForm* pInterForm = m_pEnv->GetSDKDocument()->GetInterForm();
+  CPDFSDK_InterForm* pInterForm = m_pEnv->GetInterForm();
   CPDFSDK_XFAWidget* pWidget = new CPDFSDK_XFAWidget(pAnnot, pPage, pInterForm);
   pInterForm->AddXFAMap(pAnnot, pWidget);
   return pWidget;
@@ -60,7 +59,7 @@
   mt = *pUser2Device;
 
   FX_BOOL bIsHighlight = FALSE;
-  if (pPageView->GetFormFillEnv()->GetSDKDocument()->GetFocusAnnot() != pAnnot)
+  if (pPageView->GetFormFillEnv()->GetFocusAnnot() != pAnnot)
     bIsHighlight = TRUE;
 
   GetXFAWidgetHandler(pAnnot)->RenderWidget(pAnnot->GetXFAWidget(), &gs, &mt,
@@ -107,10 +106,10 @@
     return FALSE;
 
   CPDFSDK_FormFillEnvironment* pFormFillEnv = pPageView->GetFormFillEnv();
-  if (!pFormFillEnv->GetSDKDocument())
+  if (!pFormFillEnv)
     return FALSE;
 
-  CPDFXFA_Document* pDoc = pFormFillEnv->GetSDKDocument()->GetXFADocument();
+  CPDFXFA_Document* pDoc = pFormFillEnv->GetXFADocument();
   if (!pDoc)
     return FALSE;
 
@@ -332,10 +331,10 @@
     return nullptr;
 
   CPDFSDK_FormFillEnvironment* pFormFillEnv = pPageView->GetFormFillEnv();
-  if (!pFormFillEnv->GetSDKDocument())
+  if (!pFormFillEnv)
     return nullptr;
 
-  CPDFXFA_Document* pDoc = pFormFillEnv->GetSDKDocument()->GetXFADocument();
+  CPDFXFA_Document* pDoc = pFormFillEnv->GetXFADocument();
   if (!pDoc)
     return nullptr;
 
diff --git a/fpdfsdk/formfiller/cffl_formfiller.cpp b/fpdfsdk/formfiller/cffl_formfiller.cpp
index 86818b1..30609fd 100644
--- a/fpdfsdk/formfiller/cffl_formfiller.cpp
+++ b/fpdfsdk/formfiller/cffl_formfiller.cpp
@@ -8,7 +8,6 @@
 
 #include "core/fpdfapi/page/cpdf_page.h"
 #include "core/fxge/cfx_renderdevice.h"
-#include "fpdfsdk/cpdfsdk_document.h"
 #include "fpdfsdk/cpdfsdk_formfillenvironment.h"
 #include "fpdfsdk/cpdfsdk_pageview.h"
 #include "fpdfsdk/cpdfsdk_widget.h"
@@ -245,8 +244,7 @@
 void CFFL_FormFiller::SetFocusForAnnot(CPDFSDK_Annot* pAnnot, uint32_t nFlag) {
   CPDFSDK_Widget* pWidget = (CPDFSDK_Widget*)pAnnot;
   UnderlyingPageType* pPage = pWidget->GetUnderlyingPage();
-  CPDFSDK_PageView* pPageView =
-      m_pEnv->GetSDKDocument()->GetPageView(pPage, true);
+  CPDFSDK_PageView* pPageView = m_pEnv->GetPageView(pPage, true);
   if (CPWL_Wnd* pWnd = GetPDFWindow(pPageView, TRUE))
     pWnd->SetFocus();
 
@@ -452,7 +450,7 @@
 
 CPDFSDK_PageView* CFFL_FormFiller::GetCurPageView(bool renew) {
   UnderlyingPageType* pPage = m_pAnnot->GetUnderlyingPage();
-  return m_pEnv ? m_pEnv->GetSDKDocument()->GetPageView(pPage, renew) : nullptr;
+  return m_pEnv ? m_pEnv->GetPageView(pPage, renew) : nullptr;
 }
 
 CFX_FloatRect CFFL_FormFiller::GetFocusBox(CPDFSDK_PageView* pPageView) {
diff --git a/fpdfsdk/formfiller/cffl_interactiveformfiller.cpp b/fpdfsdk/formfiller/cffl_interactiveformfiller.cpp
index e3a136e..b06dfdb 100644
--- a/fpdfsdk/formfiller/cffl_interactiveformfiller.cpp
+++ b/fpdfsdk/formfiller/cffl_interactiveformfiller.cpp
@@ -11,7 +11,6 @@
 #include "core/fxge/cfx_graphstatedata.h"
 #include "core/fxge/cfx_pathdata.h"
 #include "core/fxge/cfx_renderdevice.h"
-#include "fpdfsdk/cpdfsdk_document.h"
 #include "fpdfsdk/cpdfsdk_formfillenvironment.h"
 #include "fpdfsdk/cpdfsdk_interform.h"
 #include "fpdfsdk/cpdfsdk_pageview.h"
@@ -67,7 +66,7 @@
       pFormFiller->OnDraw(pPageView, pAnnot, pDevice, pUser2Device);
       pAnnot->GetPDFPage();
 
-      if (m_pEnv->GetSDKDocument()->GetFocusAnnot() == pAnnot) {
+      if (m_pEnv->GetFocusAnnot() == pAnnot) {
         CFX_FloatRect rcFocus = pFormFiller->GetFocusBox(pPageView);
         if (!rcFocus.IsEmpty()) {
           CFX_PathData path;
@@ -229,10 +228,10 @@
     case FIELDTYPE_RADIOBUTTON:
       if (GetViewBBox(pPageView, pAnnot->Get())
               .Contains((int)point.x, (int)point.y))
-        m_pEnv->GetSDKDocument()->SetFocusAnnot(pAnnot);
+        m_pEnv->SetFocusAnnot(pAnnot);
       break;
     default:
-      m_pEnv->GetSDKDocument()->SetFocusAnnot(pAnnot);
+      m_pEnv->SetFocusAnnot(pAnnot);
       break;
   }
 
@@ -240,7 +239,7 @@
   if (CFFL_FormFiller* pFormFiller = GetFormFiller(pAnnot->Get(), FALSE))
     bRet = pFormFiller->OnLButtonUp(pPageView, pAnnot->Get(), nFlags, point);
 
-  if (m_pEnv->GetSDKDocument()->GetFocusAnnot() == pAnnot->Get()) {
+  if (m_pEnv->GetFocusAnnot() == pAnnot->Get()) {
     FX_BOOL bExit = FALSE;
     FX_BOOL bReset = FALSE;
     OnButtonUp(pAnnot, pPageView, bReset, bExit, nFlags);
@@ -672,8 +671,7 @@
                                              uint32_t nFlag) {
   if (!m_bNotifying) {
     ASSERT(pWidget);
-    CPDFSDK_InterForm* pInterForm =
-        pPageView->GetFormFillEnv()->GetSDKDocument()->GetInterForm();
+    CPDFSDK_InterForm* pInterForm = pPageView->GetFormFillEnv()->GetInterForm();
     pInterForm->OnCalculate(pWidget->GetFormField());
     m_bNotifying = FALSE;
   }
@@ -685,8 +683,7 @@
                                           uint32_t nFlag) {
   if (!m_bNotifying) {
     ASSERT(pWidget);
-    CPDFSDK_InterForm* pInterForm =
-        pPageView->GetFormFillEnv()->GetSDKDocument()->GetInterForm();
+    CPDFSDK_InterForm* pInterForm = pPageView->GetFormFillEnv()->GetInterForm();
 
     FX_BOOL bFormatted = FALSE;
     CFX_WideString sValue =
@@ -955,7 +952,7 @@
           bRC = FALSE;
         }
 
-        if (pFormFillEnv->GetSDKDocument()->GetFocusAnnot() != pData->pWidget) {
+        if (pFormFillEnv->GetFocusAnnot() != pData->pWidget) {
           pFormFiller->CommitData(pData->pPageView, nFlag);
           bExit = TRUE;
         }
diff --git a/fpdfsdk/fpdfformfill.cpp b/fpdfsdk/fpdfformfill.cpp
index 8146e1d..de43273 100644
--- a/fpdfsdk/fpdfformfill.cpp
+++ b/fpdfsdk/fpdfformfill.cpp
@@ -17,7 +17,6 @@
 #include "core/fpdfdoc/cpdf_interform.h"
 #include "core/fpdfdoc/cpdf_occontext.h"
 #include "core/fxge/cfx_fxgedevice.h"
-#include "fpdfsdk/cpdfsdk_document.h"
 #include "fpdfsdk/cpdfsdk_formfillenvironment.h"
 #include "fpdfsdk/cpdfsdk_interform.h"
 #include "fpdfsdk/cpdfsdk_pageview.h"
@@ -46,8 +45,7 @@
 CPDFSDK_InterForm* FormHandleToInterForm(FPDF_FORMHANDLE hHandle) {
   CPDFSDK_FormFillEnvironment* pFormFillEnv =
       HandleToCPDFSDKEnvironment(hHandle);
-  return pFormFillEnv ? pFormFillEnv->GetSDKDocument()->GetInterForm()
-                      : nullptr;
+  return pFormFillEnv ? pFormFillEnv->GetInterForm() : nullptr;
 }
 
 CPDFSDK_PageView* FormHandleToPageView(FPDF_FORMHANDLE hHandle,
@@ -58,8 +56,7 @@
 
   CPDFSDK_FormFillEnvironment* pFormFillEnv =
       HandleToCPDFSDKEnvironment(hHandle);
-  return pFormFillEnv ? pFormFillEnv->GetSDKDocument()->GetPageView(pPage, true)
-                      : nullptr;
+  return pFormFillEnv ? pFormFillEnv->GetPageView(pPage, true) : nullptr;
 }
 
 #ifdef PDF_ENABLE_XFA
@@ -132,8 +129,7 @@
 
 #ifdef PDF_ENABLE_XFA
   options.m_pOCContext = new CPDF_OCContext(pPDFDoc, CPDF_OCContext::View);
-  if (CPDFSDK_PageView* pPageView =
-          pFormFillEnv->GetSDKDocument()->GetPageView(pPage, true))
+  if (CPDFSDK_PageView* pPageView = pFormFillEnv->GetPageView(pPage, true))
     pPageView->PageView_OnDraw(pDevice.get(), &matrix, &options, clip);
 #else   // PDF_ENABLE_XFA
   options.m_pOCContext =
@@ -281,11 +277,11 @@
 
   // Reset the focused annotations and remove the SDK document from the
   // XFA document.
-  pFormFillEnv->GetSDKDocument()->ClearAllFocusedAnnots();
+  pFormFillEnv->ClearAllFocusedAnnots();
   // If the document was closed first, it's possible the XFA document
   // is now a nullptr.
-  if (pFormFillEnv->GetSDKDocument()->GetXFADocument())
-    pFormFillEnv->GetSDKDocument()->GetXFADocument()->SetFormFillEnv(nullptr);
+  if (pFormFillEnv->GetXFADocument())
+    pFormFillEnv->GetXFADocument()->SetFormFillEnv(nullptr);
 #endif  // PDF_ENABLE_XFA
 
   delete pFormFillEnv;
@@ -396,7 +392,7 @@
       HandleToCPDFSDKEnvironment(hHandle);
   if (!pFormFillEnv)
     return FALSE;
-  return pFormFillEnv->GetSDKDocument()->KillFocusAnnot(0);
+  return pFormFillEnv->KillFocusAnnot(0);
 }
 
 DLLEXPORT void STDCALL FPDF_FFLDraw(FPDF_FORMHANDLE hHandle,
@@ -675,12 +671,11 @@
   if (!pPage)
     return;
 
-  CPDFSDK_PageView* pPageView =
-      pFormFillEnv->GetSDKDocument()->GetPageView(pPage, false);
+  CPDFSDK_PageView* pPageView = pFormFillEnv->GetPageView(pPage, false);
   if (pPageView) {
     pPageView->SetValid(FALSE);
     // RemovePageView() takes care of the delete for us.
-    pFormFillEnv->GetSDKDocument()->RemovePageView(pPage);
+    pFormFillEnv->RemovePageView(pPage);
   }
 }
 
@@ -688,14 +683,14 @@
   CPDFSDK_FormFillEnvironment* pFormFillEnv =
       HandleToCPDFSDKEnvironment(hHandle);
   if (pFormFillEnv && pFormFillEnv->IsJSInitiated())
-    pFormFillEnv->GetSDKDocument()->ProcJavascriptFun();
+    pFormFillEnv->ProcJavascriptFun();
 }
 
 DLLEXPORT void STDCALL FORM_DoDocumentOpenAction(FPDF_FORMHANDLE hHandle) {
   CPDFSDK_FormFillEnvironment* pFormFillEnv =
       HandleToCPDFSDKEnvironment(hHandle);
   if (pFormFillEnv && pFormFillEnv->IsJSInitiated())
-    pFormFillEnv->GetSDKDocument()->ProcOpenAction();
+    pFormFillEnv->ProcOpenAction();
 }
 
 DLLEXPORT void STDCALL FORM_DoDocumentAAction(FPDF_FORMHANDLE hHandle,
@@ -705,7 +700,7 @@
   if (!pFormFillEnv)
     return;
 
-  CPDF_Document* pDoc = pFormFillEnv->GetSDKDocument()->GetPDFDocument();
+  CPDF_Document* pDoc = pFormFillEnv->GetPDFDocument();
   CPDF_Dictionary* pDic = pDoc->GetRoot();
   if (!pDic)
     return;
@@ -736,7 +731,7 @@
   if (!pPDFPage)
     return;
 
-  if (!pFormFillEnv->GetSDKDocument()->GetPageView(pPage, false))
+  if (!pFormFillEnv->GetPageView(pPage, false))
     return;
 
   CPDFSDK_ActionHandler* pActionHandler = pFormFillEnv->GetActionHander();
diff --git a/fpdfsdk/fpdfview.cpp b/fpdfsdk/fpdfview.cpp
index 41fdc64..3ab81fe 100644
--- a/fpdfsdk/fpdfview.cpp
+++ b/fpdfsdk/fpdfview.cpp
@@ -685,7 +685,7 @@
     // This will delete the |pPageView| object. We must cleanup the PageView
     // first because it will attempt to reset the View on the |pPage| during
     // destruction.
-    pPageView->GetFormFillEnv()->GetSDKDocument()->RemovePageView(pPage);
+    pPageView->GetFormFillEnv()->RemovePageView(pPage);
     // If the page was owned then the pageview will have deleted the page.
     if (owned)
       return;
diff --git a/fpdfsdk/fpdfxfa/cpdfxfa_docenvironment.cpp b/fpdfsdk/fpdfxfa/cpdfxfa_docenvironment.cpp
index 3d40fb2..7e7ee50 100644
--- a/fpdfsdk/fpdfxfa/cpdfxfa_docenvironment.cpp
+++ b/fpdfsdk/fpdfxfa/cpdfxfa_docenvironment.cpp
@@ -9,7 +9,6 @@
 #include "core/fpdfapi/parser/cpdf_array.h"
 #include "core/fpdfapi/parser/cpdf_stream_acc.h"
 #include "core/fpdfapi/parser/cpdf_string.h"
-#include "fpdfsdk/cpdfsdk_document.h"
 #include "fpdfsdk/cpdfsdk_formfillenvironment.h"
 #include "fpdfsdk/cpdfsdk_interform.h"
 #include "fpdfsdk/cpdfsdk_pageview.h"
@@ -47,7 +46,7 @@
 
 void CPDFXFA_DocEnvironment::SetChangeMark(CXFA_FFDoc* hDoc) {
   if (hDoc == m_pDocument->GetXFADoc() && m_pDocument->GetFormFillEnv())
-    m_pDocument->GetFormFillEnv()->GetSDKDocument()->SetChangeMark();
+    m_pDocument->GetFormFillEnv()->SetChangeMark();
 }
 
 void CPDFXFA_DocEnvironment::InvalidateRect(CXFA_FFPageView* pPageView,
@@ -279,7 +278,7 @@
     if (!pPage)
       continue;
 
-    m_pDocument->GetFormFillEnv()->GetSDKDocument()->RemovePageView(pPage);
+    m_pDocument->GetFormFillEnv()->RemovePageView(pPage);
     pPage->SetXFAPageView(pXFADocView->GetPageView(iPageIter));
   }
 
@@ -305,7 +304,6 @@
     return;
 
   m_pDocument->GetFormFillEnv()
-      ->GetSDKDocument()
       ->GetPageView(pXFAPage, true)
       ->AddAnnot(hWidget);
 }
@@ -324,8 +322,7 @@
     return;
 
   CPDFSDK_PageView* pSdkPageView =
-      m_pDocument->GetFormFillEnv()->GetSDKDocument()->GetPageView(pXFAPage,
-                                                                   true);
+      m_pDocument->GetFormFillEnv()->GetPageView(pXFAPage, true);
   if (CPDFSDK_Annot* pAnnot = pSdkPageView->GetAnnotByXFAWidget(hWidget))
     pSdkPageView->DeleteAnnot(pAnnot);
 }
@@ -353,8 +350,7 @@
                                             int32_t iCurPage) {
   if (hDoc != m_pDocument->GetXFADoc() || !m_pDocument->GetFormFillEnv() ||
       m_pDocument->GetDocType() != DOCTYPE_DYNAMIC_XFA || iCurPage < 0 ||
-      iCurPage >=
-          m_pDocument->GetFormFillEnv()->GetSDKDocument()->GetPageCount()) {
+      iCurPage >= m_pDocument->GetFormFillEnv()->GetPageCount()) {
     return;
   }
 
@@ -367,11 +363,11 @@
 FX_BOOL CPDFXFA_DocEnvironment::IsCalculationsEnabled(CXFA_FFDoc* hDoc) {
   if (hDoc != m_pDocument->GetXFADoc() || !m_pDocument->GetFormFillEnv())
     return FALSE;
-  if (m_pDocument->GetFormFillEnv()->GetSDKDocument()->GetInterForm())
+  if (m_pDocument->GetFormFillEnv()->GetInterForm()) {
     return m_pDocument->GetFormFillEnv()
-        ->GetSDKDocument()
         ->GetInterForm()
         ->IsXfaCalculateEnabled();
+  }
   return FALSE;
 }
 
@@ -379,11 +375,11 @@
                                                     FX_BOOL bEnabled) {
   if (hDoc != m_pDocument->GetXFADoc() || !m_pDocument->GetFormFillEnv())
     return;
-  if (m_pDocument->GetFormFillEnv()->GetSDKDocument()->GetInterForm())
+  if (m_pDocument->GetFormFillEnv()->GetInterForm()) {
     m_pDocument->GetFormFillEnv()
-        ->GetSDKDocument()
         ->GetInterForm()
         ->XfaEnableCalculate(bEnabled);
+  }
 }
 
 void CPDFXFA_DocEnvironment::GetTitle(CXFA_FFDoc* hDoc,
@@ -416,8 +412,9 @@
     return;
 
   if (m_pDocument->GetDocType() != DOCTYPE_DYNAMIC_XFA &&
-      m_pDocument->GetDocType() != DOCTYPE_STATIC_XFA)
+      m_pDocument->GetDocType() != DOCTYPE_STATIC_XFA) {
     return;
+  }
 
   CPDFSDK_FormFillEnvironment* pFormFillEnv = m_pDocument->GetFormFillEnv();
   if (!pFormFillEnv)
@@ -427,8 +424,9 @@
   CFX_ByteString bs = wsFilePath.UTF16LE_Encode();
   if (wsFilePath.IsEmpty()) {
     if (!pFormFillEnv->GetFormFillInfo() ||
-        !pFormFillEnv->GetFormFillInfo()->m_pJsPlatform)
+        !pFormFillEnv->GetFormFillInfo()->m_pJsPlatform) {
       return;
+    }
 
     CFX_WideString filepath = pFormFillEnv->JS_fieldBrowse();
     bs = filepath.UTF16LE_Encode();
@@ -528,11 +526,11 @@
 FX_BOOL CPDFXFA_DocEnvironment::IsValidationsEnabled(CXFA_FFDoc* hDoc) {
   if (hDoc != m_pDocument->GetXFADoc() || !m_pDocument->GetFormFillEnv())
     return FALSE;
-  if (m_pDocument->GetFormFillEnv()->GetSDKDocument()->GetInterForm())
+  if (m_pDocument->GetFormFillEnv()->GetInterForm()) {
     return m_pDocument->GetFormFillEnv()
-        ->GetSDKDocument()
         ->GetInterForm()
         ->IsXfaValidationsEnabled();
+  }
   return TRUE;
 }
 
@@ -540,11 +538,11 @@
                                                    FX_BOOL bEnabled) {
   if (hDoc != m_pDocument->GetXFADoc() || !m_pDocument->GetFormFillEnv())
     return;
-  if (m_pDocument->GetFormFillEnv()->GetSDKDocument()->GetInterForm())
+  if (m_pDocument->GetFormFillEnv()->GetInterForm()) {
     m_pDocument->GetFormFillEnv()
-        ->GetSDKDocument()
         ->GetInterForm()
         ->XfaSetValidationsEnabled(bEnabled);
+  }
 }
 
 void CPDFXFA_DocEnvironment::SetFocusWidget(CXFA_FFDoc* hDoc,
@@ -554,21 +552,19 @@
 
   if (!hWidget) {
     CPDFSDK_Annot::ObservedPtr pNull;
-    m_pDocument->GetFormFillEnv()->GetSDKDocument()->SetFocusAnnot(&pNull);
+    m_pDocument->GetFormFillEnv()->SetFocusAnnot(&pNull);
     return;
   }
 
-  int pageViewCount =
-      m_pDocument->GetFormFillEnv()->GetSDKDocument()->GetPageViewCount();
+  int pageViewCount = m_pDocument->GetFormFillEnv()->GetPageViewCount();
   for (int i = 0; i < pageViewCount; i++) {
-    CPDFSDK_PageView* pPageView =
-        m_pDocument->GetFormFillEnv()->GetSDKDocument()->GetPageView(i);
+    CPDFSDK_PageView* pPageView = m_pDocument->GetFormFillEnv()->GetPageView(i);
     if (!pPageView)
       continue;
 
     CPDFSDK_Annot::ObservedPtr pAnnot(pPageView->GetAnnotByXFAWidget(hWidget));
     if (pAnnot) {
-      m_pDocument->GetFormFillEnv()->GetSDKDocument()->SetFocusAnnot(&pAnnot);
+      m_pDocument->GetFormFillEnv()->SetFocusAnnot(&pAnnot);
       break;
     }
   }
@@ -600,8 +596,7 @@
   if (hDoc != m_pDocument->GetXFADoc() || !m_pDocument->GetFormFillEnv())
     return 0;
 
-  CPDFSDK_InterForm* pInterForm =
-      m_pDocument->GetFormFillEnv()->GetSDKDocument()->GetInterForm();
+  CPDFSDK_InterForm* pInterForm = m_pDocument->GetFormFillEnv()->GetInterForm();
   if (!pInterForm)
     return 0;
 
@@ -619,8 +614,9 @@
 
 FX_BOOL CPDFXFA_DocEnvironment::OnBeforeNotifySubmit() {
   if (m_pDocument->GetDocType() != DOCTYPE_DYNAMIC_XFA &&
-      m_pDocument->GetDocType() != DOCTYPE_STATIC_XFA)
+      m_pDocument->GetDocType() != DOCTYPE_STATIC_XFA) {
     return TRUE;
+  }
 
   if (!m_pDocument->GetXFADocView())
     return TRUE;
diff --git a/fpdfsdk/fpdfxfa/cpdfxfa_document.cpp b/fpdfsdk/fpdfxfa/cpdfxfa_document.cpp
index 472779a..c768b59 100644
--- a/fpdfsdk/fpdfxfa/cpdfxfa_document.cpp
+++ b/fpdfsdk/fpdfxfa/cpdfxfa_document.cpp
@@ -7,7 +7,6 @@
 #include "fpdfsdk/fpdfxfa/cpdfxfa_document.h"
 
 #include "core/fpdfapi/parser/cpdf_document.h"
-#include "fpdfsdk/cpdfsdk_document.h"
 #include "fpdfsdk/cpdfsdk_formfillenvironment.h"
 #include "fpdfsdk/cpdfsdk_interform.h"
 #include "fpdfsdk/cpdfsdk_pageview.h"
@@ -44,10 +43,10 @@
   m_nLoadStatus = FXFA_LOADSTATUS_CLOSING;
 
   if (m_pFormFillEnv) {
-    m_pFormFillEnv->GetSDKDocument()->ClearAllFocusedAnnots();
+    m_pFormFillEnv->ClearAllFocusedAnnots();
     // Once we're deleted the SDKDocument will point at a bad underlying
     // doc so we need to reset it ...
-    m_pFormFillEnv->GetSDKDocument()->ResetXFADocument();
+    m_pFormFillEnv->ResetXFADocument();
     m_pFormFillEnv = nullptr;
   }
 
@@ -202,5 +201,5 @@
 
 void CPDFXFA_Document::ClearChangeMark() {
   if (m_pFormFillEnv)
-    m_pFormFillEnv->GetSDKDocument()->ClearChangeMark();
+    m_pFormFillEnv->ClearChangeMark();
 }
diff --git a/fpdfsdk/fsdk_actionhandler.cpp b/fpdfsdk/fsdk_actionhandler.cpp
index a7a7940..c324406 100644
--- a/fpdfsdk/fsdk_actionhandler.cpp
+++ b/fpdfsdk/fsdk_actionhandler.cpp
@@ -11,7 +11,6 @@
 #include "core/fpdfapi/parser/cpdf_array.h"
 #include "core/fpdfdoc/cpdf_formfield.h"
 #include "core/fpdfdoc/cpdf_interform.h"
-#include "fpdfsdk/cpdfsdk_document.h"
 #include "fpdfsdk/cpdfsdk_formfillenvironment.h"
 #include "fpdfsdk/cpdfsdk_interform.h"
 #include "fpdfsdk/fsdk_define.h"
@@ -223,8 +222,7 @@
     CPDF_Dictionary* pFieldDict) {
   ASSERT(pFieldDict);
 
-  CPDFSDK_InterForm* pInterForm =
-      pFormFillEnv->GetSDKDocument()->GetInterForm();
+  CPDFSDK_InterForm* pInterForm = pFormFillEnv->GetInterForm();
   CPDF_InterForm* pPDFInterForm = pInterForm->GetInterForm();
   return !!pPDFInterForm->GetFieldByDict(pFieldDict);
 }
@@ -418,8 +416,7 @@
     const CPDF_Action& action) {
   ASSERT(action.GetDict());
 
-  CPDF_Document* pPDFDocument =
-      pFormFillEnv->GetSDKDocument()->GetPDFDocument();
+  CPDF_Document* pPDFDocument = pFormFillEnv->GetPDFDocument();
   ASSERT(pPDFDocument);
 
   CPDF_Dest MyDest = action.GetDest(pPDFDocument);
@@ -454,8 +451,7 @@
     const CPDF_Action& action) {
   ASSERT(action.GetDict());
 
-  CFX_ByteString sURI =
-      action.GetURI(pFormFillEnv->GetSDKDocument()->GetPDFDocument());
+  CFX_ByteString sURI = action.GetURI(pFormFillEnv->GetPDFDocument());
   pFormFillEnv->DoURIAction(sURI.c_str());
 }
 
@@ -597,10 +593,9 @@
 FX_BOOL CPDFSDK_ActionHandler::DoAction_Hide(
     const CPDF_Action& action,
     CPDFSDK_FormFillEnvironment* pFormFillEnv) {
-  CPDFSDK_InterForm* pInterForm =
-      pFormFillEnv->GetSDKDocument()->GetInterForm();
+  CPDFSDK_InterForm* pInterForm = pFormFillEnv->GetInterForm();
   if (pInterForm->DoAction_Hide(action)) {
-    pFormFillEnv->GetSDKDocument()->SetChangeMark();
+    pFormFillEnv->SetChangeMark();
     return TRUE;
   }
 
@@ -610,26 +605,23 @@
 FX_BOOL CPDFSDK_ActionHandler::DoAction_SubmitForm(
     const CPDF_Action& action,
     CPDFSDK_FormFillEnvironment* pFormFillEnv) {
-  CPDFSDK_InterForm* pInterForm =
-      pFormFillEnv->GetSDKDocument()->GetInterForm();
+  CPDFSDK_InterForm* pInterForm = pFormFillEnv->GetInterForm();
   return pInterForm->DoAction_SubmitForm(action);
 }
 
 FX_BOOL CPDFSDK_ActionHandler::DoAction_ResetForm(
     const CPDF_Action& action,
     CPDFSDK_FormFillEnvironment* pFormFillEnv) {
-  CPDFSDK_InterForm* pInterForm =
-      pFormFillEnv->GetSDKDocument()->GetInterForm();
+  CPDFSDK_InterForm* pInterForm = pFormFillEnv->GetInterForm();
   return pInterForm->DoAction_ResetForm(action);
 }
 
 FX_BOOL CPDFSDK_ActionHandler::DoAction_ImportData(
     const CPDF_Action& action,
     CPDFSDK_FormFillEnvironment* pFormFillEnv) {
-  CPDFSDK_InterForm* pInterForm =
-      pFormFillEnv->GetSDKDocument()->GetInterForm();
+  CPDFSDK_InterForm* pInterForm = pFormFillEnv->GetInterForm();
   if (pInterForm->DoAction_ImportData(action)) {
-    pFormFillEnv->GetSDKDocument()->SetChangeMark();
+    pFormFillEnv->SetChangeMark();
     return TRUE;
   }
 
diff --git a/fpdfsdk/fsdk_baseform_embeddertest.cpp b/fpdfsdk/fsdk_baseform_embeddertest.cpp
index a11453b..13efe86 100644
--- a/fpdfsdk/fsdk_baseform_embeddertest.cpp
+++ b/fpdfsdk/fsdk_baseform_embeddertest.cpp
@@ -4,7 +4,6 @@
 
 #include "fpdfsdk/cba_annotiterator.h"
 #include "fpdfsdk/cpdfsdk_annot.h"
-#include "fpdfsdk/cpdfsdk_document.h"
 #include "fpdfsdk/cpdfsdk_formfillenvironment.h"
 #include "fpdfsdk/fsdk_define.h"
 #include "testing/embedder_test.h"
@@ -45,7 +44,7 @@
 
   {
     // Page 0 specifies "row order".
-    CBA_AnnotIterator iter(pFormFillEnv->GetSDKDocument()->GetPageView(0),
+    CBA_AnnotIterator iter(pFormFillEnv->GetPageView(0),
                            CPDF_Annot::Subtype::WIDGET);
     CPDFSDK_Annot* pAnnot = iter.GetFirstAnnot();
     CheckRect(pAnnot->GetRect(), RightTop);
@@ -71,7 +70,7 @@
   }
   {
     // Page 1 specifies "column order"
-    CBA_AnnotIterator iter(pFormFillEnv->GetSDKDocument()->GetPageView(1),
+    CBA_AnnotIterator iter(pFormFillEnv->GetPageView(1),
                            CPDF_Annot::Subtype::WIDGET);
     CPDFSDK_Annot* pAnnot = iter.GetFirstAnnot();
     CheckRect(pAnnot->GetRect(), RightTop);
@@ -97,7 +96,7 @@
   }
   {
     // Page 2 specifies "struct order"
-    CBA_AnnotIterator iter(pFormFillEnv->GetSDKDocument()->GetPageView(2),
+    CBA_AnnotIterator iter(pFormFillEnv->GetPageView(2),
                            CPDF_Annot::Subtype::WIDGET);
     CPDFSDK_Annot* pAnnot = iter.GetFirstAnnot();
     CheckRect(pAnnot->GetRect(), LeftBottom);
diff --git a/fpdfsdk/javascript/Document.cpp b/fpdfsdk/javascript/Document.cpp
index ed87c2d..6b795a0 100644
--- a/fpdfsdk/javascript/Document.cpp
+++ b/fpdfsdk/javascript/Document.cpp
@@ -17,7 +17,6 @@
 #include "core/fpdfdoc/cpdf_interform.h"
 #include "core/fpdfdoc/cpdf_nametree.h"
 #include "fpdfsdk/cpdfsdk_annotiterator.h"
-#include "fpdfsdk/cpdfsdk_document.h"
 #include "fpdfsdk/cpdfsdk_formfillenvironment.h"
 #include "fpdfsdk/cpdfsdk_interform.h"
 #include "fpdfsdk/cpdfsdk_pageview.h"
@@ -173,8 +172,7 @@
     sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
     return FALSE;
   }
-  CPDFSDK_InterForm* pInterForm =
-      m_pFormFillEnv->GetSDKDocument()->GetInterForm();
+  CPDFSDK_InterForm* pInterForm = m_pFormFillEnv->GetInterForm();
   CPDF_InterForm* pPDFForm = pInterForm->GetInterForm();
   vp << static_cast<int>(pPDFForm->CountFields(CFX_WideString()));
   return TRUE;
@@ -188,15 +186,15 @@
     return FALSE;
   }
   if (vp.IsGetting()) {
-    vp << !!m_pFormFillEnv->GetSDKDocument()->GetChangeMark();
+    vp << !!m_pFormFillEnv->GetChangeMark();
   } else {
     bool bChanged = false;
     vp >> bChanged;
 
     if (bChanged)
-      m_pFormFillEnv->GetSDKDocument()->SetChangeMark();
+      m_pFormFillEnv->SetChangeMark();
     else
-      m_pFormFillEnv->GetSDKDocument()->ClearChangeMark();
+      m_pFormFillEnv->ClearChangeMark();
   }
   return TRUE;
 }
@@ -218,12 +216,11 @@
     return FALSE;
   }
   if (vp.IsGetting()) {
-    if (CPDFSDK_PageView* pPageView =
-            m_pFormFillEnv->GetSDKDocument()->GetCurrentView()) {
+    if (CPDFSDK_PageView* pPageView = m_pFormFillEnv->GetCurrentView()) {
       vp << pPageView->GetPageIndex();
     }
   } else {
-    int iPageCount = m_pFormFillEnv->GetSDKDocument()->GetPageCount();
+    int iPageCount = m_pFormFillEnv->GetPageCount();
     int iPageNum = 0;
     vp >> iPageNum;
 
@@ -299,8 +296,7 @@
   CJS_Context* pContext = static_cast<CJS_Context*>(cc);
   CJS_Runtime* pRuntime = pContext->GetJSRuntime();
   CFX_WideString wideName = params[0].ToCFXWideString(pRuntime);
-  CPDFSDK_InterForm* pInterForm =
-      m_pFormFillEnv->GetSDKDocument()->GetInterForm();
+  CPDFSDK_InterForm* pInterForm = m_pFormFillEnv->GetInterForm();
   CPDF_InterForm* pPDFForm = pInterForm->GetInterForm();
   if (pPDFForm->CountFields(wideName) <= 0) {
     vRet.SetNull(pRuntime);
@@ -338,8 +334,7 @@
     sError = JSGetStringFromID(IDS_STRING_JSVALUEERROR);
     return FALSE;
   }
-  CPDFSDK_InterForm* pInterForm =
-      m_pFormFillEnv->GetSDKDocument()->GetInterForm();
+  CPDFSDK_InterForm* pInterForm = m_pFormFillEnv->GetInterForm();
   CPDF_InterForm* pPDFForm = pInterForm->GetInterForm();
   CPDF_FormField* pField = pPDFForm->GetField(nIndex, CFX_WideString());
   if (!pField)
@@ -384,8 +379,7 @@
     sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
     return FALSE;
   }
-  if (!m_pFormFillEnv->GetSDKDocument()->GetPermissions(
-          FPDFPERM_EXTRACT_ACCESS)) {
+  if (!m_pFormFillEnv->GetPermissions(FPDFPERM_EXTRACT_ACCESS)) {
     sError = JSGetStringFromID(IDS_STRING_JSNOPERMISSION);
     return FALSE;
   }
@@ -402,8 +396,7 @@
       iLength > 4 ? params[4].ToCFXWideString(pRuntime) : L"";
   CFX_WideString cMsg = iLength > 5 ? params[5].ToCFXWideString(pRuntime) : L"";
 
-  CPDFSDK_InterForm* pInterForm =
-      m_pFormFillEnv->GetSDKDocument()->GetInterForm();
+  CPDFSDK_InterForm* pInterForm = m_pFormFillEnv->GetInterForm();
   CFX_ByteTextBuf textBuf;
   if (!pInterForm->ExportFormToFDFTextBuf(textBuf))
     return FALSE;
@@ -502,17 +495,15 @@
     sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
     return FALSE;
   }
-  if (!(m_pFormFillEnv->GetSDKDocument()->GetPermissions(FPDFPERM_MODIFY) ||
-        m_pFormFillEnv->GetSDKDocument()->GetPermissions(
-            FPDFPERM_ANNOT_FORM))) {
+  if (!(m_pFormFillEnv->GetPermissions(FPDFPERM_MODIFY) ||
+        m_pFormFillEnv->GetPermissions(FPDFPERM_ANNOT_FORM))) {
     sError = JSGetStringFromID(IDS_STRING_JSNOPERMISSION);
     return FALSE;
   }
   CJS_Context* pContext = static_cast<CJS_Context*>(cc);
   CJS_Runtime* pRuntime = pContext->GetJSRuntime();
   CFX_WideString sFieldName = params[0].ToCFXWideString(pRuntime);
-  CPDFSDK_InterForm* pInterForm =
-      m_pFormFillEnv->GetSDKDocument()->GetInterForm();
+  CPDFSDK_InterForm* pInterForm = m_pFormFillEnv->GetInterForm();
   std::vector<CPDFSDK_Widget*> widgets;
   pInterForm->GetWidgets(sFieldName, &widgets);
   if (widgets.empty())
@@ -532,8 +523,7 @@
     // If there is currently no pageview associated with the page being used
     // do not create one. We may be in the process of tearing down the document
     // and creating a new pageview at this point will cause bad things.
-    CPDFSDK_PageView* pPageView =
-        m_pFormFillEnv->GetSDKDocument()->GetPageView(pPage, false);
+    CPDFSDK_PageView* pPageView = m_pFormFillEnv->GetPageView(pPage, false);
     if (pPageView) {
 #if PDF_ENABLE_XFA
       pPageView->DeleteAnnot(pWidget);
@@ -541,7 +531,7 @@
       pPageView->UpdateRects(aRefresh);
     }
   }
-  m_pFormFillEnv->GetSDKDocument()->SetChangeMark();
+  m_pFormFillEnv->SetChangeMark();
 
   return TRUE;
 }
@@ -558,21 +548,20 @@
     sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
     return FALSE;
   }
-  if (!(m_pFormFillEnv->GetSDKDocument()->GetPermissions(FPDFPERM_MODIFY) ||
-        m_pFormFillEnv->GetSDKDocument()->GetPermissions(FPDFPERM_ANNOT_FORM) ||
-        m_pFormFillEnv->GetSDKDocument()->GetPermissions(FPDFPERM_FILL_FORM))) {
+  if (!(m_pFormFillEnv->GetPermissions(FPDFPERM_MODIFY) ||
+        m_pFormFillEnv->GetPermissions(FPDFPERM_ANNOT_FORM) ||
+        m_pFormFillEnv->GetPermissions(FPDFPERM_FILL_FORM))) {
     sError = JSGetStringFromID(IDS_STRING_JSNOPERMISSION);
     return FALSE;
   }
 
-  CPDFSDK_InterForm* pInterForm =
-      m_pFormFillEnv->GetSDKDocument()->GetInterForm();
+  CPDFSDK_InterForm* pInterForm = m_pFormFillEnv->GetInterForm();
   CPDF_InterForm* pPDFForm = pInterForm->GetInterForm();
   CJS_Array aName;
 
   if (params.empty()) {
     pPDFForm->ResetForm(TRUE);
-    m_pFormFillEnv->GetSDKDocument()->SetChangeMark();
+    m_pFormFillEnv->SetChangeMark();
     return TRUE;
   }
 
@@ -599,7 +588,7 @@
 
   if (!aFields.empty()) {
     pPDFForm->ResetForm(aFields, TRUE, TRUE);
-    m_pFormFillEnv->GetSDKDocument()->SetChangeMark();
+    m_pFormFillEnv->SetChangeMark();
   }
 
   return TRUE;
@@ -664,8 +653,7 @@
     aFields.Attach(CJS_Value(pRuntime, pValue).ToV8Array(pRuntime));
   }
 
-  CPDFSDK_InterForm* pInterForm =
-      m_pFormFillEnv->GetSDKDocument()->GetInterForm();
+  CPDFSDK_InterForm* pInterForm = m_pFormFillEnv->GetInterForm();
   CPDF_InterForm* pPDFInterForm = pInterForm->GetInterForm();
   if (aFields.GetLength(pRuntime) == 0 && bEmpty) {
     if (pPDFInterForm->CheckRequiredFields(nullptr, true)) {
@@ -788,8 +776,7 @@
     sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
     return FALSE;
   }
-  CPDF_Dictionary* pDictionary =
-      m_pFormFillEnv->GetSDKDocument()->GetPDFDocument()->GetInfo();
+  CPDF_Dictionary* pDictionary = m_pFormFillEnv->GetPDFDocument()->GetInfo();
   if (!pDictionary)
     return FALSE;
 
@@ -843,22 +830,21 @@
     sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
     return FALSE;
   }
-  CPDF_Dictionary* pDictionary =
-      m_pFormFillEnv->GetSDKDocument()->GetPDFDocument()->GetInfo();
+  CPDF_Dictionary* pDictionary = m_pFormFillEnv->GetPDFDocument()->GetInfo();
   if (!pDictionary)
     return FALSE;
 
   if (vp.IsGetting()) {
     vp << pDictionary->GetUnicodeTextFor(propName);
   } else {
-    if (!m_pFormFillEnv->GetSDKDocument()->GetPermissions(FPDFPERM_MODIFY)) {
+    if (!m_pFormFillEnv->GetPermissions(FPDFPERM_MODIFY)) {
       sError = JSGetStringFromID(IDS_STRING_JSNOPERMISSION);
       return FALSE;
     }
     CFX_WideString csProperty;
     vp >> csProperty;
     pDictionary->SetStringFor(propName, PDF_EncodeText(csProperty));
-    m_pFormFillEnv->GetSDKDocument()->SetChangeMark();
+    m_pFormFillEnv->SetChangeMark();
   }
   return TRUE;
 }
@@ -885,7 +871,7 @@
   if (vp.IsGetting()) {
     vp << m_bDelay;
   } else {
-    if (!m_pFormFillEnv->GetSDKDocument()->GetPermissions(FPDFPERM_MODIFY)) {
+    if (!m_pFormFillEnv->GetPermissions(FPDFPERM_MODIFY)) {
       sError = JSGetStringFromID(IDS_STRING_JSNOPERMISSION);
       return FALSE;
     }
@@ -947,7 +933,7 @@
     sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
     return FALSE;
   }
-  vp << m_pFormFillEnv->GetSDKDocument()->GetPageCount();
+  vp << m_pFormFillEnv->GetPageCount();
   return TRUE;
 }
 
@@ -995,7 +981,7 @@
     sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
     return FALSE;
   }
-  vp << m_pFormFillEnv->GetSDKDocument()->GetPath();
+  vp << m_pFormFillEnv->JS_docGetFilePath();
   return TRUE;
 }
 
@@ -1017,8 +1003,7 @@
     sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
     return FALSE;
   }
-  CPDFSDK_InterForm* pInterForm =
-      m_pFormFillEnv->GetSDKDocument()->GetInterForm();
+  CPDFSDK_InterForm* pInterForm = m_pFormFillEnv->GetInterForm();
   if (vp.IsGetting()) {
     vp << !!pInterForm->IsCalculateEnabled();
   } else {
@@ -1040,7 +1025,7 @@
     sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
     return FALSE;
   }
-  CFX_WideString wsFilePath = m_pFormFillEnv->GetSDKDocument()->GetPath();
+  CFX_WideString wsFilePath = m_pFormFillEnv->JS_docGetFilePath();
   int32_t i = wsFilePath.GetLength() - 1;
   for (; i >= 0; i--) {
     if (wsFilePath.GetAt(i) == L'\\' || wsFilePath.GetAt(i) == L'/')
@@ -1065,7 +1050,7 @@
     sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
     return FALSE;
   }
-  vp << app::SysPathToPDFPath(m_pFormFillEnv->GetSDKDocument()->GetPath());
+  vp << app::SysPathToPDFPath(m_pFormFillEnv->JS_docGetFilePath());
   return TRUE;
 }
 
@@ -1118,8 +1103,7 @@
   CJS_Runtime* pRuntime = pContext->GetJSRuntime();
   int nPageNo = params[0].ToInt(pRuntime);
   CFX_WideString swAnnotName = params[1].ToCFXWideString(pRuntime);
-  CPDFSDK_PageView* pPageView =
-      m_pFormFillEnv->GetSDKDocument()->GetPageView(nPageNo);
+  CPDFSDK_PageView* pPageView = m_pFormFillEnv->GetPageView(nPageNo);
   if (!pPageView)
     return FALSE;
 
@@ -1170,12 +1154,11 @@
   // TODO(tonikitoo): Add support supported parameters as per
   // the PDF spec.
 
-  int nPageNo = m_pFormFillEnv->GetSDKDocument()->GetPageCount();
+  int nPageNo = m_pFormFillEnv->GetPageCount();
   CJS_Array annots;
 
   for (int i = 0; i < nPageNo; ++i) {
-    CPDFSDK_PageView* pPageView =
-        m_pFormFillEnv->GetSDKDocument()->GetPageView(i);
+    CPDFSDK_PageView* pPageView = m_pFormFillEnv->GetPageView(i);
     if (!pPageView)
       return FALSE;
 
@@ -1392,13 +1375,13 @@
     sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
     return FALSE;
   }
-  if (!(m_pFormFillEnv->GetSDKDocument()->GetPermissions(FPDFPERM_MODIFY) ||
-        m_pFormFillEnv->GetSDKDocument()->GetPermissions(FPDFPERM_ANNOT_FORM) ||
-        m_pFormFillEnv->GetSDKDocument()->GetPermissions(FPDFPERM_FILL_FORM))) {
+  if (!(m_pFormFillEnv->GetPermissions(FPDFPERM_MODIFY) ||
+        m_pFormFillEnv->GetPermissions(FPDFPERM_ANNOT_FORM) ||
+        m_pFormFillEnv->GetPermissions(FPDFPERM_FILL_FORM))) {
     sError = JSGetStringFromID(IDS_STRING_JSNOPERMISSION);
     return FALSE;
   }
-  m_pFormFillEnv->GetSDKDocument()->GetInterForm()->OnCalculate();
+  m_pFormFillEnv->GetInterForm()->OnCalculate();
   return TRUE;
 }
 
@@ -1416,8 +1399,7 @@
     sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
     return FALSE;
   }
-  if (!m_pFormFillEnv->GetSDKDocument()->GetPermissions(
-          FPDFPERM_EXTRACT_ACCESS)) {
+  if (!m_pFormFillEnv->GetPermissions(FPDFPERM_EXTRACT_ACCESS)) {
     sError = JSGetStringFromID(IDS_STRING_JSNOPERMISSION);
     return FALSE;
   }
@@ -1429,7 +1411,7 @@
   int nWordNo = params.size() > 1 ? params[1].ToInt(pRuntime) : 0;
   bool bStrip = params.size() > 2 ? params[2].ToBool(pRuntime) : true;
 
-  CPDF_Document* pDocument = m_pFormFillEnv->GetSDKDocument()->GetPDFDocument();
+  CPDF_Document* pDocument = m_pFormFillEnv->GetPDFDocument();
   if (!pDocument)
     return FALSE;
 
@@ -1476,8 +1458,7 @@
     sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
     return FALSE;
   }
-  if (!m_pFormFillEnv->GetSDKDocument()->GetPermissions(
-          FPDFPERM_EXTRACT_ACCESS)) {
+  if (!m_pFormFillEnv->GetPermissions(FPDFPERM_EXTRACT_ACCESS)) {
     sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
     return FALSE;
   }
@@ -1492,14 +1473,13 @@
     sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
     return FALSE;
   }
-  if (!m_pFormFillEnv->GetSDKDocument()->GetPermissions(
-          FPDFPERM_EXTRACT_ACCESS)) {
+  if (!m_pFormFillEnv->GetPermissions(FPDFPERM_EXTRACT_ACCESS)) {
     sError = JSGetStringFromID(IDS_STRING_JSNOPERMISSION);
     return FALSE;
   }
   CJS_Runtime* pRuntime = CJS_Runtime::FromContext(cc);
   int nPageNo = params.size() > 0 ? params[0].ToInt(pRuntime) : 0;
-  CPDF_Document* pDocument = m_pFormFillEnv->GetSDKDocument()->GetPDFDocument();
+  CPDF_Document* pDocument = m_pFormFillEnv->GetPDFDocument();
   if (nPageNo < 0 || nPageNo >= pDocument->GetPageCount()) {
     sError = JSGetStringFromID(IDS_STRING_JSVALUEERROR);
     return FALSE;
@@ -1686,7 +1666,7 @@
   CJS_Runtime* pRuntime = CJS_Runtime::FromContext(cc);
   CFX_WideString wideName = params[0].ToCFXWideString(pRuntime);
   CFX_ByteString utf8Name = wideName.UTF8Encode();
-  CPDF_Document* pDocument = m_pFormFillEnv->GetSDKDocument()->GetPDFDocument();
+  CPDF_Document* pDocument = m_pFormFillEnv->GetPDFDocument();
   if (!pDocument)
     return FALSE;
 
diff --git a/fpdfsdk/javascript/Field.cpp b/fpdfsdk/javascript/Field.cpp
index 652f1e6..a2118a7 100644
--- a/fpdfsdk/javascript/Field.cpp
+++ b/fpdfsdk/javascript/Field.cpp
@@ -15,7 +15,6 @@
 #include "core/fpdfapi/page/cpdf_page.h"
 #include "core/fpdfapi/parser/cpdf_document.h"
 #include "core/fpdfdoc/cpdf_interform.h"
-#include "fpdfsdk/cpdfsdk_document.h"
 #include "fpdfsdk/cpdfsdk_formfillenvironment.h"
 #include "fpdfsdk/cpdfsdk_interform.h"
 #include "fpdfsdk/cpdfsdk_pageview.h"
@@ -213,13 +212,11 @@
                            const CFX_WideString& csFieldName) {
   m_pJSDoc = pDocument;
   m_pFormFillEnv.Reset(pDocument->GetFormFillEnv());
-  m_bCanSet =
-      m_pFormFillEnv->GetSDKDocument()->GetPermissions(FPDFPERM_FILL_FORM) ||
-      m_pFormFillEnv->GetSDKDocument()->GetPermissions(FPDFPERM_ANNOT_FORM) ||
-      m_pFormFillEnv->GetSDKDocument()->GetPermissions(FPDFPERM_MODIFY);
+  m_bCanSet = m_pFormFillEnv->GetPermissions(FPDFPERM_FILL_FORM) ||
+              m_pFormFillEnv->GetPermissions(FPDFPERM_ANNOT_FORM) ||
+              m_pFormFillEnv->GetPermissions(FPDFPERM_MODIFY);
 
-  CPDFSDK_InterForm* pRDInterForm =
-      m_pFormFillEnv->GetSDKDocument()->GetInterForm();
+  CPDFSDK_InterForm* pRDInterForm = m_pFormFillEnv->GetInterForm();
   CPDF_InterForm* pInterForm = pRDInterForm->GetInterForm();
   CFX_WideString swFieldNameTemp = csFieldName;
   swFieldNameTemp.Replace(L"..", L".");
@@ -246,8 +243,7 @@
     CPDFSDK_FormFillEnvironment* pFormFillEnv,
     const CFX_WideString& csFieldName) {
   std::vector<CPDF_FormField*> fields;
-  CPDFSDK_InterForm* pReaderInterForm =
-      pFormFillEnv->GetSDKDocument()->GetInterForm();
+  CPDFSDK_InterForm* pReaderInterForm = pFormFillEnv->GetInterForm();
   CPDF_InterForm* pInterForm = pReaderInterForm->GetInterForm();
   for (int i = 0, sz = pInterForm->CountFields(csFieldName); i < sz; ++i) {
     if (CPDF_FormField* pFormField = pInterForm->GetField(i, csFieldName))
@@ -266,8 +262,7 @@
                             FX_BOOL bChangeMark,
                             FX_BOOL bResetAP,
                             FX_BOOL bRefresh) {
-  CPDFSDK_InterForm* pInterForm =
-      pFormFillEnv->GetSDKDocument()->GetInterForm();
+  CPDFSDK_InterForm* pInterForm = pFormFillEnv->GetInterForm();
 
   if (bResetAP) {
     std::vector<CPDFSDK_Widget*> widgets;
@@ -301,17 +296,16 @@
 
     // TODO(dsinclair): Determine if all widgets share the same
     // CPDFSDK_InterForm. If that's the case, we can move the code to
-    // |GetSDKDocument| out of the loop.
+    // |GetFormFillEnv| out of the loop.
     for (CPDFSDK_Widget* pWidget : widgets) {
       pWidget->GetInterForm()
           ->GetFormFillEnv()
-          ->GetSDKDocument()
           ->UpdateAllViews(nullptr, pWidget);
     }
   }
 
   if (bChangeMark)
-    pFormFillEnv->GetSDKDocument()->SetChangeMark();
+    pFormFillEnv->SetChangeMark();
 }
 
 void Field::UpdateFormControl(CPDFSDK_FormFillEnvironment* pFormFillEnv,
@@ -321,7 +315,7 @@
                               FX_BOOL bRefresh) {
   ASSERT(pFormControl);
 
-  CPDFSDK_InterForm* pForm = pFormFillEnv->GetSDKDocument()->GetInterForm();
+  CPDFSDK_InterForm* pForm = pFormFillEnv->GetInterForm();
   CPDFSDK_Widget* pWidget = pForm->GetWidget(pFormControl, false);
 
   if (pWidget) {
@@ -339,20 +333,19 @@
 
     if (bRefresh) {
       CPDFSDK_InterForm* pInterForm = pWidget->GetInterForm();
-      pInterForm->GetFormFillEnv()->GetSDKDocument()->UpdateAllViews(nullptr,
-                                                                     pWidget);
+      pInterForm->GetFormFillEnv()->UpdateAllViews(nullptr, pWidget);
     }
   }
 
   if (bChangeMark)
-    pFormFillEnv->GetSDKDocument()->SetChangeMark();
+    pFormFillEnv->SetChangeMark();
 }
 
 CPDFSDK_Widget* Field::GetWidget(CPDFSDK_FormFillEnvironment* pFormFillEnv,
                                  CPDF_FormControl* pFormControl,
                                  bool createIfNeeded) {
-  CPDFSDK_InterForm* pInterForm = static_cast<CPDFSDK_InterForm*>(
-      pFormFillEnv->GetSDKDocument()->GetInterForm());
+  CPDFSDK_InterForm* pInterForm =
+      static_cast<CPDFSDK_InterForm*>(pFormFillEnv->GetInterForm());
   return pInterForm ? pInterForm->GetWidget(pFormControl, createIfNeeded)
                     : nullptr;
 }
@@ -865,8 +858,7 @@
       return FALSE;
     }
 
-    CPDFSDK_InterForm* pRDInterForm =
-        m_pFormFillEnv->GetSDKDocument()->GetInterForm();
+    CPDFSDK_InterForm* pRDInterForm = m_pFormFillEnv->GetInterForm();
     CPDF_InterForm* pInterForm = pRDInterForm->GetInterForm();
     vp << (int32_t)pInterForm->FindFieldInCalculationOrder(pFormField);
   }
@@ -1270,8 +1262,7 @@
 
     CPDF_FormField* pFormField = FieldArray[0];
     ASSERT(pFormField);
-    CPDFSDK_InterForm* pInterForm =
-        m_pFormFillEnv->GetSDKDocument()->GetInterForm();
+    CPDFSDK_InterForm* pInterForm = m_pFormFillEnv->GetInterForm();
     CPDFSDK_Widget* pWidget =
         pInterForm->GetWidget(GetSmartFieldControl(pFormField), true);
     if (!pWidget)
@@ -1301,8 +1292,7 @@
                        const CFX_WideString& swFieldName,
                        int nControlIndex,
                        int number) {
-  CPDFSDK_InterForm* pInterForm =
-      pFormFillEnv->GetSDKDocument()->GetInterForm();
+  CPDFSDK_InterForm* pInterForm = pFormFillEnv->GetInterForm();
   std::vector<CPDF_FormField*> FieldArray =
       GetFormFields(pFormFillEnv, swFieldName);
   for (CPDF_FormField* pFormField : FieldArray) {
@@ -1536,8 +1526,7 @@
 
     CPDF_FormField* pFormField = FieldArray[0];
     ASSERT(pFormField);
-    CPDFSDK_InterForm* pInterForm =
-        m_pFormFillEnv->GetSDKDocument()->GetInterForm();
+    CPDFSDK_InterForm* pInterForm = m_pFormFillEnv->GetInterForm();
     CPDFSDK_Widget* pWidget =
         pInterForm->GetWidget(GetSmartFieldControl(pFormField), false);
     if (!pWidget)
@@ -1650,8 +1639,7 @@
     if (!pFormControl)
       return FALSE;
 
-    CPDFSDK_InterForm* pInterForm =
-        m_pFormFillEnv->GetSDKDocument()->GetInterForm();
+    CPDFSDK_InterForm* pInterForm = m_pFormFillEnv->GetInterForm();
     if (!pFormField->CountControls())
       return FALSE;
 
@@ -1670,8 +1658,7 @@
                          const CFX_WideString& swFieldName,
                          int nControlIndex,
                          int number) {
-  CPDFSDK_InterForm* pInterForm =
-      pFormFillEnv->GetSDKDocument()->GetInterForm();
+  CPDFSDK_InterForm* pInterForm = pFormFillEnv->GetInterForm();
   std::vector<CPDF_FormField*> FieldArray =
       GetFormFields(pFormFillEnv, swFieldName);
   for (CPDF_FormField* pFormField : FieldArray) {
@@ -1844,8 +1831,7 @@
     return FALSE;
 
   std::vector<CPDFSDK_Widget*> widgets;
-  m_pFormFillEnv->GetSDKDocument()->GetInterForm()->GetWidgets(pFormField,
-                                                               &widgets);
+  m_pFormFillEnv->GetInterForm()->GetWidgets(pFormField, &widgets);
 
   if (widgets.empty()) {
     vp << (int32_t)-1;
@@ -1913,8 +1899,7 @@
 FX_BOOL Field::print(IJS_Context* cc,
                      CJS_PropValue& vp,
                      CFX_WideString& sError) {
-  CPDFSDK_InterForm* pInterForm =
-      m_pFormFillEnv->GetSDKDocument()->GetInterForm();
+  CPDFSDK_InterForm* pInterForm = m_pFormFillEnv->GetInterForm();
   std::vector<CPDF_FormField*> FieldArray = GetFormFields(m_FieldName);
   if (FieldArray.empty())
     return FALSE;
@@ -2080,8 +2065,7 @@
       return FALSE;
 
     CPDF_FormField* pFormField = FieldArray[0];
-    CPDFSDK_InterForm* pInterForm =
-        m_pFormFillEnv->GetSDKDocument()->GetInterForm();
+    CPDFSDK_InterForm* pInterForm = m_pFormFillEnv->GetInterForm();
     CPDFSDK_Widget* pWidget =
         pInterForm->GetWidget(GetSmartFieldControl(pFormField), true);
     if (!pWidget)
@@ -2107,8 +2091,7 @@
                     const CFX_WideString& swFieldName,
                     int nControlIndex,
                     const CFX_FloatRect& rect) {
-  CPDFSDK_InterForm* pInterForm =
-      pFormFillEnv->GetSDKDocument()->GetInterForm();
+  CPDFSDK_InterForm* pInterForm = pFormFillEnv->GetInterForm();
   std::vector<CPDF_FormField*> FieldArray =
       GetFormFields(pFormFillEnv, swFieldName);
   for (CPDF_FormField* pFormField : FieldArray) {
@@ -3216,8 +3199,7 @@
   if (nCount < 1)
     return FALSE;
 
-  CPDFSDK_InterForm* pInterForm =
-      m_pFormFillEnv->GetSDKDocument()->GetInterForm();
+  CPDFSDK_InterForm* pInterForm = m_pFormFillEnv->GetInterForm();
   CPDFSDK_Widget* pWidget = nullptr;
   if (nCount == 1) {
     pWidget = pInterForm->GetWidget(pFormField->GetControl(0), false);
@@ -3228,7 +3210,7 @@
     if (!pPage)
       return FALSE;
     if (CPDFSDK_PageView* pCurPageView =
-            m_pFormFillEnv->GetSDKDocument()->GetPageView(pPage, true)) {
+            m_pFormFillEnv->GetPageView(pPage, true)) {
       for (int32_t i = 0; i < nCount; i++) {
         if (CPDFSDK_Widget* pTempWidget =
                 pInterForm->GetWidget(pFormField->GetControl(i), false)) {
@@ -3243,7 +3225,7 @@
 
   if (pWidget) {
     CPDFSDK_Annot::ObservedPtr pObserved(pWidget);
-    m_pFormFillEnv->GetSDKDocument()->SetFocusAnnot(&pObserved);
+    m_pFormFillEnv->SetFocusAnnot(&pObserved);
   }
 
   return TRUE;
diff --git a/fpdfsdk/javascript/PublicMethods.cpp b/fpdfsdk/javascript/PublicMethods.cpp
index d4f17a8..344af5a 100644
--- a/fpdfsdk/javascript/PublicMethods.cpp
+++ b/fpdfsdk/javascript/PublicMethods.cpp
@@ -11,7 +11,6 @@
 
 #include "core/fpdfdoc/cpdf_interform.h"
 #include "core/fxcrt/fx_ext.h"
-#include "fpdfsdk/cpdfsdk_document.h"
 #include "fpdfsdk/cpdfsdk_formfillenvironment.h"
 #include "fpdfsdk/cpdfsdk_interform.h"
 #include "fpdfsdk/javascript/Field.h"
@@ -1660,7 +1659,7 @@
   CJS_Context* pContext = static_cast<CJS_Context*>(cc);
   CJS_Runtime* pRuntime = CJS_Runtime::FromContext(cc);
   CPDFSDK_InterForm* pReaderInterForm =
-      pContext->GetFormFillEnv()->GetSDKDocument()->GetInterForm();
+      pContext->GetFormFillEnv()->GetInterForm();
   CPDF_InterForm* pInterForm = pReaderInterForm->GetInterForm();
 
   CFX_WideString sFunction = params[0].ToCFXWideString(pRuntime);
diff --git a/fpdfsdk/javascript/app.cpp b/fpdfsdk/javascript/app.cpp
index b41b872..325a84b 100644
--- a/fpdfsdk/javascript/app.cpp
+++ b/fpdfsdk/javascript/app.cpp
@@ -9,7 +9,6 @@
 #include <memory>
 #include <vector>
 
-#include "fpdfsdk/cpdfsdk_document.h"
 #include "fpdfsdk/cpdfsdk_formfillenvironment.h"
 #include "fpdfsdk/cpdfsdk_interform.h"
 #include "fpdfsdk/javascript/Document.h"
@@ -245,7 +244,6 @@
 
     CJS_Context* pContext = (CJS_Context*)cc;
     pContext->GetFormFillEnv()
-        ->GetSDKDocument()
         ->GetInterForm()
         ->EnableCalculate((FX_BOOL)m_bCalculate);
   } else {
@@ -294,8 +292,7 @@
     return FALSE;
 #ifdef PDF_ENABLE_XFA
   CJS_Context* pContext = (CJS_Context*)cc;
-  CPDFXFA_Document* pDoc =
-      pContext->GetFormFillEnv()->GetSDKDocument()->GetXFADocument();
+  CPDFXFA_Document* pDoc = pContext->GetFormFillEnv()->GetXFADocument();
   if (pDoc->GetDocType() == 1 || pDoc->GetDocType() == 2) {
     vp << JS_NUM_VIEWERVERSION_XFA;
     return TRUE;
@@ -423,7 +420,7 @@
     swTitle = JSGetStringFromID(IDS_STRING_JSALERT);
 
   pRuntime->BeginBlock();
-  pFormFillEnv->GetSDKDocument()->KillFocusAnnot(0);
+  pFormFillEnv->KillFocusAnnot(0);
 
   vRet = CJS_Value(pRuntime, pFormFillEnv->JS_appAlert(
                                  swMsg.c_str(), swTitle.c_str(), iType, iIcon));