Check whether a pdf has XFA fields before loading XFA fields
authorJUN FANG <jun_fang@foxitsoftware.com>
Thu, 5 Mar 2015 21:39:21 +0000 (13:39 -0800)
committerJUN FANG <jun_fang@foxitsoftware.com>
Thu, 5 Mar 2015 21:39:21 +0000 (13:39 -0800)
BUG=452793
R=tsepez@chromium.org

Review URL: https://codereview.chromium.org/973673002

fpdfsdk/src/fpdfview.cpp
samples/pdfium_test.cc
testing/embedder_test.cpp

index 6a8f890..db19124 100644 (file)
@@ -303,43 +303,40 @@ DLLEXPORT FPDF_DOCUMENT STDCALL FPDF_LoadDocument(FPDF_STRING file_path, FPDF_BY
        return pDocument;
 }
 
-DLLEXPORT FX_BOOL STDCALL FPDF_HasXFAField(FPDF_DOCUMENT document, int& docType)\r
-{\r
-       if (!document)\r
-               return FALSE;\r
-\r
-       CPDF_Dictionary* pRoot = ((CPDF_Document*)document)->GetRoot();\r
-       if (!pRoot)\r
-               return FALSE;\r
-\r
-       CPDF_Dictionary* pAcroForm = pRoot->GetDict("AcroForm");\r
-       if (!pAcroForm)\r
-               return FALSE;\r
-\r
-       CPDF_Object* pXFA = pAcroForm->GetElement("XFA");\r
-       if (!pXFA)\r
-               return FALSE;\r
-\r
-       FX_BOOL bDynamicXFA = pRoot->GetBoolean("NeedsRendering", FALSE);\r
-\r
-       if (bDynamicXFA)\r
-               docType = DOCTYPE_DYNIMIC_XFA;\r
-       else\r
-               docType = DOCTYPE_STATIC_XFA;\r
-\r
-       return TRUE;\r
-}
-
-DLLEXPORT  FPDF_BOOL STDCALL FPDF_LoadXFA(FPDF_DOCUMENT document)
+DLLEXPORT FX_BOOL STDCALL FPDF_HasXFAField(FPDF_DOCUMENT document, int& docType)
 {
-       if (!document) 
+       if (!document)
+               return FALSE;
+
+       CPDF_Document *pdfDoc = (static_cast<CPDFXFA_Document *>(document))->GetPDFDoc();
+       if (!pdfDoc)
+               return FALSE;
+
+       CPDF_Dictionary* pRoot = pdfDoc->GetRoot();
+       if (!pRoot)
+               return FALSE;
+
+       CPDF_Dictionary* pAcroForm = pRoot->GetDict("AcroForm");
+       if (!pAcroForm)
                return FALSE;
 
-       int iDocType = DOCTYPE_PDF;
-       FX_BOOL hasXFAField = FPDF_HasXFAField(document, iDocType);
-       if (!hasXFAField)
+       CPDF_Object* pXFA = pAcroForm->GetElement("XFA");
+       if (!pXFA)
                return FALSE;
-       return ((CPDFXFA_Document*)document)->LoadXFADoc();
+
+       FX_BOOL bDynamicXFA = pRoot->GetBoolean("NeedsRendering", FALSE);
+
+       if (bDynamicXFA)
+               docType = DOCTYPE_DYNIMIC_XFA;
+       else
+               docType = DOCTYPE_STATIC_XFA;
+
+       return TRUE;
+}
+
+DLLEXPORT  FPDF_BOOL STDCALL FPDF_LoadXFA(FPDF_DOCUMENT document)
+{
+       return document && (static_cast<CPDFXFA_Document *>(document))->LoadXFADoc();
 }
 
 
index d861f2f..e4a55f2 100644 (file)
@@ -468,12 +468,21 @@ void RenderPdf(const std::string& name, const char* pBuf, size_t len,
     doc = FPDFAvail_GetDocument(pdf_avail, NULL);
   }
 
+  if (!doc)
+  {
+    fprintf(stderr, "Load pdf docs unsuccessful.\n");
+    return;
+  }
+
   (void) FPDF_GetDocPermissions(doc);
   (void) FPDFAvail_IsFormAvail(pdf_avail, &hints);
 
   FPDF_FORMHANDLE form = FPDFDOC_InitFormFillEnvironment(doc, &form_callbacks);
-  if (!FPDF_LoadXFA(doc)) {
-    fprintf(stderr, "LoadXFA unsuccessful, continuing anyway.\n");
+  int docType = DOCTYPE_PDF;
+  if (FPDF_HasXFAField(doc, docType))
+  {
+      if (docType != DOCTYPE_PDF && !FPDF_LoadXFA(doc))
+          fprintf(stderr, "LoadXFA unsuccessful, continuing anyway.\n");
   }
   FPDF_SetFormFieldHighlightColor(form, 0, 0xFFE4DD);
   FPDF_SetFormFieldHighlightAlpha(form, 100);
index cf0bdf8..563dec8 100644 (file)
@@ -261,7 +261,12 @@ bool EmbedderTest::OpenDocument(const std::string& filename) {
   if (!document_) {
     return false;
   }
-  (void) FPDF_LoadXFA(document_);
+  int docType = DOCTYPE_PDF;
+  if (FPDF_HasXFAField(document_, docType))
+  {
+    if (docType != DOCTYPE_PDF)
+      (void) FPDF_LoadXFA(document_);
+  }
   (void) FPDF_GetDocPermissions(document_);
   (void) FPDFAvail_IsFormAvail(avail_, &hints_);