Fix pdf write

This commit is contained in:
Svetlana Kulikova
2024-01-23 18:44:36 +03:00
parent e9b9f0f550
commit 1432c7b07e
8 changed files with 169 additions and 87 deletions

View File

@ -400,48 +400,6 @@ void CPdfFile::RotatePage(int nRotate)
m_pInternal->pWriter->PageRotate(nRotate);
}
#ifndef BUILDING_WASM_MODULE
PdfWriter::CDictObject* GetAcroForm(Object* oAcroForm)
{
if (!oAcroForm || !oAcroForm->isDict())
return NULL;
PdfWriter::CDictObject* pAcroForm = new PdfWriter::CDictObject();
for (int nIndex = 0; nIndex < oAcroForm->dictGetLength(); ++nIndex)
{
Object oTemp2;
char* chKey = oAcroForm->dictGetKey(nIndex);
if (strcmp("DR", chKey) == 0)
{
oAcroForm->dictGetVal(nIndex, &oTemp2);
/*
if (!oTemp2.isDict())
{
oTemp2.free();
continue;
}
PdfWriter::CResourcesDict* pDR = new PdfWriter::CResourcesDict(NULL, true, false);
pAcroForm->Add(chKey, pDR);
for (int nIndex = 0; nIndex < oTemp2.dictGetLength(); ++nIndex)
{
}
oTemp2.free();
continue;
*/
}
else
oAcroForm->dictGetValNF(nIndex, &oTemp2);
DictToCDictObject(&oTemp2, pAcroForm, false, chKey);
oTemp2.free();
}
if (!pAcroForm->Get("Fields"))
pAcroForm->Add("Fields", new PdfWriter::CArrayObject());
return pAcroForm;
}
bool CPdfFile::EditPdf(const std::wstring& wsDstFile)
{
if (wsDstFile.empty())
@ -522,25 +480,71 @@ bool CPdfFile::EditPdf(const std::wstring& wsDstFile)
return false;
}
pXref->Add(pCatalog, catRef.gen);
PdfWriter::CResourcesDict* pDR = NULL;
PdfWriter::CXref* pDRXref = NULL;
for (int nIndex = 0; nIndex < catDict.dictGetLength(); ++nIndex)
{
Object oTemp;
Object oAcroForm;
char* chKey = catDict.dictGetKey(nIndex);
if (strcmp("AcroForm", chKey) == 0)
{
catDict.dictGetVal(nIndex, &oTemp);
PdfWriter::CDictObject* pAcroForm = GetAcroForm(&oTemp);
oTemp.free();
if (pAcroForm)
catDict.dictGetVal(nIndex, &oAcroForm);
PdfWriter::CDictObject* pAcroForm = new PdfWriter::CDictObject();
for (int nIndex = 0; nIndex < oAcroForm.dictGetLength(); ++nIndex)
{
pCatalog->Add(chKey, pAcroForm);
Object oTemp2;
char* chKey = oAcroForm.dictGetKey(nIndex);
if (strcmp("DR", chKey) == 0)
{
oAcroForm.dictGetVal(nIndex, &oTemp2);
if (!oTemp2.isDict())
{
oTemp2.free();
continue;
}
Object oDR;
oAcroForm.dictGetValNF(nIndex, &oDR);
int nDRxrefNum = oDR.isRef() ? oDR.getRefNum() : xref->getNumObjects();
int nDRxrefGen = oDR.isRef() ? oDR.getRefGen() : 0;
oDR.free();
pDRXref = new PdfWriter::CXref(pDoc, nDRxrefNum);
pDR = new PdfWriter::CResourcesDict(NULL, true, false);
pDRXref->Add(pDR, nDRxrefGen);
pAcroForm->Add(chKey, pDR);
for (int nIndex2 = 0; nIndex2 < oTemp2.dictGetLength(); ++nIndex2)
{
Object oTemp;
char* chKey = oTemp2.dictGetKey(nIndex2);
oTemp2.dictGetVal(nIndex2, &oTemp);
DictToCDictObject(&oTemp, pDR, false, chKey);
oTemp.free();
}
oTemp2.free();
pDR->Fix();
continue;
}
else
oAcroForm.dictGetValNF(nIndex, &oTemp2);
DictToCDictObject(&oTemp2, pAcroForm, false, chKey);
oTemp2.free();
}
if (!pAcroForm->Get("Fields"))
pAcroForm->Add("Fields", new PdfWriter::CArrayObject());
oAcroForm.free();
pCatalog->Add(chKey, pAcroForm);
continue;
}
else
catDict.dictGetValNF(nIndex, &oTemp);
DictToCDictObject(&oTemp, pCatalog, false, chKey);
oTemp.free();
catDict.dictGetValNF(nIndex, &oAcroForm);
DictToCDictObject(&oAcroForm, pCatalog, false, chKey);
oAcroForm.free();
}
catDict.free();
@ -614,12 +618,15 @@ bool CPdfFile::EditPdf(const std::wstring& wsDstFile)
}
// Применение редактирования для writer
bool bRes = pDoc->EditPdf(wsDstFile, xref->getLastXRefPos(), xref->getNumObjects(), pXref, pCatalog, pEncryptDict, nFormField);
bool bRes = pDoc->EditPdf(wsDstFile, xref->getLastXRefPos(), xref->getNumObjects() + 1, pXref, pCatalog, pEncryptDict, nFormField);
if (bRes)
{
// Воспроизведение дерева страниц во writer
m_pInternal->GetPageTree(xref, &pagesRefObj);
m_pInternal->bEdit = true;
if (pDR && pDRXref)
bRes = pDoc->EditResources(pDRXref, pDR);
}
pagesRefObj.free();
return bRes;