CFDF_Document* CPDF_InterForm::ExportToFDF( const CFX_WideStringC& pdf_path, const std::vector<CPDF_FormField*>& fields, bool bIncludeOrExclude, bool bSimpleFileSpec) const { CFDF_Document* pDoc = CFDF_Document::CreateNewDoc(); if (!pDoc) return nullptr; CPDF_Dictionary* pMainDict = pDoc->GetRoot()->GetDictBy("FDF"); if (!pdf_path.IsEmpty()) { if (bSimpleFileSpec) { CFX_WideString wsFilePath = CPDF_FileSpec::EncodeFileName(pdf_path); pMainDict->SetAtString("F", CFX_ByteString::FromUnicode(wsFilePath)); pMainDict->SetAtString("UF", PDF_EncodeText(wsFilePath)); } else { CPDF_FileSpec filespec; filespec.SetFileName(pdf_path); pMainDict->SetAt("F", filespec.GetObj()); } } CPDF_Array* pFields = new CPDF_Array; pMainDict->SetAt("Fields", pFields); int nCount = m_pFieldTree->m_Root.CountFields(); for (int i = 0; i < nCount; i++) { CPDF_FormField* pField = m_pFieldTree->m_Root.GetField(i); if (!pField || pField->GetType() == CPDF_FormField::PushButton) continue; uint32_t dwFlags = pField->GetFieldFlags(); if (dwFlags & 0x04) continue; if (bIncludeOrExclude == pdfium::ContainsValue(fields, pField)) { if ((dwFlags & 0x02) != 0 && pField->m_pDict->GetStringBy("V").IsEmpty()) continue; CFX_WideString fullname = FPDF_GetFullName(pField->GetFieldDict()); CPDF_Dictionary* pFieldDict = new CPDF_Dictionary; pFieldDict->SetAt("T", new CPDF_String(fullname)); if (pField->GetType() == CPDF_FormField::CheckBox || pField->GetType() == CPDF_FormField::RadioButton) { CFX_WideString csExport = pField->GetCheckValue(FALSE); CFX_ByteString csBExport = PDF_EncodeText(csExport); CPDF_Object* pOpt = FPDF_GetFieldAttr(pField->m_pDict, "Opt"); if (pOpt) pFieldDict->SetAtString("V", csBExport); else pFieldDict->SetAtName("V", csBExport); } else { CPDF_Object* pV = FPDF_GetFieldAttr(pField->m_pDict, "V"); if (pV) pFieldDict->SetAt("V", pV->CloneDirectObject()); } pFields->Add(pFieldDict); } } return pDoc; }
FX_BOOL CPDFSDK_InterForm::SubmitForm(const CFX_WideString& sDestination, FX_BOOL bUrlEncoded) { if (sDestination.IsEmpty()) return FALSE; if (!m_pDocument || !m_pInterForm) return FALSE; CPDFSDK_Environment* pEnv = m_pDocument->GetEnv(); CFX_WideString wsPDFFilePath = m_pDocument->GetPath(); CFDF_Document* pFDFDoc = m_pInterForm->ExportToFDF(wsPDFFilePath.AsStringC(), false); if (!pFDFDoc) return FALSE; CFX_ByteTextBuf FdfBuffer; FX_BOOL bRet = pFDFDoc->WriteBuf(FdfBuffer); delete pFDFDoc; if (!bRet) return FALSE; uint8_t* pBuffer = FdfBuffer.GetBuffer(); FX_STRSIZE nBufSize = FdfBuffer.GetLength(); if (bUrlEncoded && !FDFToURLEncodedData(pBuffer, nBufSize)) return FALSE; pEnv->JS_docSubmitForm(pBuffer, nBufSize, sDestination.c_str()); if (bUrlEncoded) FX_Free(pBuffer); return TRUE; }
CFDF_Document* CFDF_Document::CreateNewDoc() { CFDF_Document* pDoc = new CFDF_Document; pDoc->m_pRootDict = new CPDF_Dictionary; pDoc->AddIndirectObject(pDoc->m_pRootDict); CPDF_Dictionary* pFDFDict = new CPDF_Dictionary; pDoc->m_pRootDict->SetAt("FDF", pFDFDict); return pDoc; }
CFDF_Document* CFDF_Document::CreateNewDoc() { CFDF_Document* pDoc = new CFDF_Document; pDoc->m_pRootDict = new CPDF_Dictionary(pDoc->GetByteStringPool()); pDoc->AddIndirectObject(pDoc->m_pRootDict); pDoc->m_pRootDict->SetFor("FDF", new CPDF_Dictionary(pDoc->GetByteStringPool())); return pDoc; }
CFDF_Document* CFDF_Document::CreateNewDoc() { CFDF_Document* pDoc = FX_NEW CFDF_Document; pDoc->m_pRootDict = FX_NEW CPDF_Dictionary; pDoc->AddIndirectObject(pDoc->m_pRootDict); CPDF_Dictionary* pFDFDict = FX_NEW CPDF_Dictionary; pDoc->m_pRootDict->SetAt(FX_BSTRC("FDF"), pFDFDict); return pDoc; }
FX_BOOL CPDFSDK_InterForm::ExportFormToFDFTextBuf(CFX_ByteTextBuf& textBuf) { CFDF_Document* pFDF = m_pInterForm->ExportToFDF(m_pDocument->GetPath().AsStringC(), false); if (!pFDF) return FALSE; FX_BOOL bRet = pFDF->WriteBuf(textBuf); delete pFDF; return bRet; }
CFDF_Document* CFDF_Document::ParseFile(IFX_FileRead* pFile, FX_BOOL bOwnFile) { if (!pFile) { return NULL; } CFDF_Document* pDoc = new CFDF_Document; pDoc->ParseStream(pFile, bOwnFile); if (!pDoc->m_pRootDict) { delete pDoc; return NULL; } return pDoc; }
FX_BOOL CPDFSDK_InterForm::FDFToURLEncodedData(uint8_t*& pBuf, FX_STRSIZE& nBufSize) { CFDF_Document* pFDF = CFDF_Document::ParseMemory(pBuf, nBufSize); if (!pFDF) return TRUE; CPDF_Dictionary* pMainDict = pFDF->GetRoot()->GetDictFor("FDF"); if (!pMainDict) return FALSE; CPDF_Array* pFields = pMainDict->GetArrayFor("Fields"); if (!pFields) return FALSE; CFX_ByteTextBuf fdfEncodedData; for (uint32_t i = 0; i < pFields->GetCount(); i++) { CPDF_Dictionary* pField = pFields->GetDictAt(i); if (!pField) continue; CFX_WideString name; name = pField->GetUnicodeTextFor("T"); CFX_ByteString name_b = CFX_ByteString::FromUnicode(name); CFX_ByteString csBValue = pField->GetStringFor("V"); CFX_WideString csWValue = PDF_DecodeText(csBValue); CFX_ByteString csValue_b = CFX_ByteString::FromUnicode(csWValue); fdfEncodedData << name_b.GetBuffer(name_b.GetLength()); name_b.ReleaseBuffer(); fdfEncodedData << "="; fdfEncodedData << csValue_b.GetBuffer(csValue_b.GetLength()); csValue_b.ReleaseBuffer(); if (i != pFields->GetCount() - 1) fdfEncodedData << "&"; } nBufSize = fdfEncodedData.GetLength(); pBuf = FX_Alloc(uint8_t, nBufSize); FXSYS_memcpy(pBuf, fdfEncodedData.GetBuffer(), nBufSize); return TRUE; }
CFDF_Document* CPDF_InterForm::ExportToFDF( const CFX_WideStringC& pdf_path, const std::vector<CPDF_FormField*>& fields, bool bIncludeOrExclude, bool bSimpleFileSpec) const { CFDF_Document* pDoc = CFDF_Document::CreateNewDoc(); if (pDoc == NULL) { return NULL; } CPDF_Dictionary* pMainDict = pDoc->GetRoot()->GetDict("FDF"); if (!pdf_path.IsEmpty()) { if (bSimpleFileSpec) { CFX_WideString wsFilePath = FILESPEC_EncodeFileName(pdf_path); pMainDict->SetAtString(FX_BSTRC("F"), CFX_ByteString::FromUnicode(wsFilePath)); pMainDict->SetAtString(FX_BSTRC("UF"), PDF_EncodeText(wsFilePath)); } else { CPDF_FileSpec filespec; filespec.SetFileName(pdf_path); pMainDict->SetAt("F", static_cast<CPDF_Object*>(filespec)); } } CPDF_Array* pFields = CPDF_Array::Create(); if (pFields == NULL) { return NULL; } pMainDict->SetAt("Fields", pFields); int nCount = m_pFieldTree->m_Root.CountFields(); for (int i = 0; i < nCount; i++) { CPDF_FormField* pField = m_pFieldTree->m_Root.GetField(i); if (pField == NULL || pField->GetType() == CPDF_FormField::PushButton) { continue; } FX_DWORD dwFlags = pField->GetFieldFlags(); if (dwFlags & 0x04) continue; auto it = std::find(fields.begin(), fields.end(), pField); if (bIncludeOrExclude == (it != fields.end())) { if ((dwFlags & 0x02) != 0 && pField->m_pDict->GetString("V").IsEmpty()) continue; CFX_WideString fullname = GetFullName(pField->GetFieldDict()); CPDF_Dictionary* pFieldDict = CPDF_Dictionary::Create(); if (!pFieldDict) return nullptr; CPDF_String* pString = CPDF_String::Create(fullname); if (!pString) { pFieldDict->Release(); return nullptr; } pFieldDict->SetAt("T", pString); if (pField->GetType() == CPDF_FormField::CheckBox || pField->GetType() == CPDF_FormField::RadioButton) { CFX_WideString csExport = pField->GetCheckValue(FALSE); CFX_ByteString csBExport = PDF_EncodeText(csExport); CPDF_Object* pOpt = FPDF_GetFieldAttr(pField->m_pDict, "Opt"); if (pOpt) pFieldDict->SetAtString("V", csBExport); else pFieldDict->SetAtName("V", csBExport); } else { CPDF_Object* pV = FPDF_GetFieldAttr(pField->m_pDict, "V"); if (pV) pFieldDict->SetAt("V", pV->Clone(TRUE)); } pFields->Add(pFieldDict); } } return pDoc; }