diff --git a/PdfFile/PdfFile.cpp b/PdfFile/PdfFile.cpp index 2822ab0555..73c8afecd1 100644 --- a/PdfFile/PdfFile.cpp +++ b/PdfFile/PdfFile.cpp @@ -185,9 +185,12 @@ public: return; Object typeDict, pagesObj; - if (!pPagesRefObj->isRef() || !pPagesRefObj->fetch(xref, &pagesObj) || - !pagesObj.isDict() || !pagesObj.dictLookup("Type", &typeDict) || - !typeDict.isName("Pages")) + if (!pPagesRefObj->isRef() || !pPagesRefObj->fetch(xref, &pagesObj)->isDict()) + { + pagesObj.free(); + return; + } + if (pagesObj.dictLookup("Type", &typeDict)->isName() && !typeDict.isName("Pages")) { pagesObj.free(); typeDict.free(); @@ -204,14 +207,14 @@ public: return; } - PdfWriter::CPageTree* pPageT = new PdfWriter::CPageTree(pXref); + PdfWriter::CPageTree* pPageT = new PdfWriter::CPageTree(); if (!pPageT) { pagesObj.free(); RELEASEOBJECT(pXref); return; } - + pXref->Add(pPageT, topPagesRef.gen); for (int nIndex = 0; nIndex < pagesObj.dictGetLength(); ++nIndex) { Object oTemp; @@ -220,13 +223,11 @@ public: DictToCDictObject(&oTemp, pPageT, false, chKey); oTemp.free(); } - pDoc->CreatePageTree(pXref, pPageT); - pPageT->SetRef(topPagesRef.num, topPagesRef.gen); pPageT->Fix(); Object kidsArrObj; - if (!pagesObj.dictLookup("Kids", &kidsArrObj) || !kidsArrObj.isArray()) + if (!pagesObj.dictLookup("Kids", &kidsArrObj)->isArray()) { pagesObj.free(); kidsArrObj.free(); @@ -327,13 +328,14 @@ void CPdfFile::Close() RELEASEOBJECT(pXref); return; } - pInfoDict = new PdfWriter::CInfoDict(pInfoXref); + pInfoDict = new PdfWriter::CInfoDict(); if (!pInfoDict) { RELEASEOBJECT(pXref); RELEASEOBJECT(pInfoXref); return; } + pInfoXref->Add(pInfoDict, pInfo ? pInfo->GetGenNo() : 0); for (int nIndex = 0; nIndex < info.dictGetLength(); ++nIndex) { @@ -343,9 +345,6 @@ void CPdfFile::Close() DictToCDictObject(&oTemp, pInfoDict, true, chKey); oTemp.free(); } - - if (pInfo) - pInfoDict->SetRef(pInfo->GetObjId(), pInfo->GetGenNo()); pInfoDict->SetTime(PdfWriter::InfoModaDate); } info.free(); @@ -470,7 +469,7 @@ bool CPdfFile::EditPdf(const std::wstring& wsDstFile) catDict.free(); return false; } - PdfWriter::CCatalog* pCatalog = new PdfWriter::CCatalog(pXref, true); + PdfWriter::CCatalog* pCatalog = new PdfWriter::CCatalog(); if (!pCatalog) { pagesRefObj.free(); @@ -478,6 +477,7 @@ bool CPdfFile::EditPdf(const std::wstring& wsDstFile) RELEASEOBJECT(pXref); return false; } + pXref->Add(pCatalog, catRef.gen); for (int nIndex = 0; nIndex < catDict.dictGetLength(); ++nIndex) { Object oTemp; @@ -486,7 +486,6 @@ bool CPdfFile::EditPdf(const std::wstring& wsDstFile) DictToCDictObject(&oTemp, pCatalog, false, chKey); oTemp.free(); } - pCatalog->SetRef(catRef.num, catRef.gen); catDict.free(); // Проверка уникальности имён текущих цифровых подписей pdf @@ -625,13 +624,14 @@ bool CPdfFile::EditPage(int nPageIndex) pageObj.free(); return false; } - PdfWriter::CPage* pPage = new PdfWriter::CPage(pXref, pDoc); + PdfWriter::CPage* pPage = new PdfWriter::CPage(pDoc); if (!pPage) { pageObj.free(); RELEASEOBJECT(pXref); return false; } + pXref->Add(pPage, pPageRef.second); for (int nIndex = 0; nIndex < pageObj.dictGetLength(); ++nIndex) { Object oTemp; @@ -643,7 +643,6 @@ bool CPdfFile::EditPage(int nPageIndex) DictToCDictObject(&oTemp, pPage, true, chKey); oTemp.free(); } - pPage->SetRef(pPageRef.first, pPageRef.second); pPage->Fix(); pageObj.free(); @@ -701,7 +700,7 @@ PdfWriter::CDictObject* GetWidgetParent(PDFDoc* pdfDoc, PdfWriter::CDocument* pD PdfWriter::CXref* pXref = new PdfWriter::CXref(pDoc, pParentRef->getRefNum()); pParent = new PdfWriter::CDictObject(); - pXref->Add(pParent); + pXref->Add(pParent, pParentRef->getRefGen()); if (!pDoc->EditParent(pXref, pParent, pParentRef->getRefNum())) { RELEASEOBJECT(pXref); @@ -730,7 +729,6 @@ PdfWriter::CDictObject* GetWidgetParent(PDFDoc* pdfDoc, PdfWriter::CDocument* pD DictToCDictObject(&oTemp, pParent, false, chKey); oTemp.free(); } - pParent->SetRef(pParentRef->getRefNum(), pParentRef->getRefGen()); oParent.free(); @@ -863,6 +861,7 @@ bool CPdfFile::EditAnnot(int nPageIndex, int nID) RELEASEOBJECT(pXref); return false; } + pXref->Add(pAnnot, oAnnotRef.getRefGen()); for (int nIndex = 0; nIndex < oAnnot.dictGetLength(); ++nIndex) { @@ -902,7 +901,6 @@ bool CPdfFile::EditAnnot(int nPageIndex, int nID) DictToCDictObject(&oTemp, pAnnot, false, chKey); oTemp.free(); } - pAnnot->SetRef(oAnnotRef.getRefNum(), oAnnotRef.getRefGen()); oAnnotRef.free(); oAnnot.free(); if (pDoc->EditAnnot(pXref, pAnnot, nID)) diff --git a/PdfFile/SrcWriter/Annotation.cpp b/PdfFile/SrcWriter/Annotation.cpp index dc0a49ba2d..e51ef2653e 100644 --- a/PdfFile/SrcWriter/Annotation.cpp +++ b/PdfFile/SrcWriter/Annotation.cpp @@ -76,7 +76,6 @@ namespace PdfWriter CAnnotation::CAnnotation(CXref* pXref, EAnnotType eType) { m_pXref = pXref; - pXref->Add(this); Add("Type", "Annot"); Add("Subtype", c_sAnnotTypeNames[(int)eType]); diff --git a/PdfFile/SrcWriter/Catalog.cpp b/PdfFile/SrcWriter/Catalog.cpp index 860df3ec63..d389ce968f 100644 --- a/PdfFile/SrcWriter/Catalog.cpp +++ b/PdfFile/SrcWriter/Catalog.cpp @@ -99,9 +99,8 @@ namespace PdfWriter pRGBProfile->GetStream()->Write((unsigned char*)c_arrICCsRGB, c_nSizeICCsRGB, false); } } - CCatalog::CCatalog(CXref* pXref, bool bEmpty) + CCatalog::CCatalog() { - pXref->Add(this); } CPageTree* CCatalog::GetRoot() const { diff --git a/PdfFile/SrcWriter/Catalog.h b/PdfFile/SrcWriter/Catalog.h index 46b9950717..1d2b38d0c5 100644 --- a/PdfFile/SrcWriter/Catalog.h +++ b/PdfFile/SrcWriter/Catalog.h @@ -45,7 +45,7 @@ namespace PdfWriter { public: CCatalog(CXref* pXref); - CCatalog(CXref* pXref, bool bEmpty); + CCatalog(); CPageTree* GetRoot() const; EPageLayout GetPageLayout() const; void SetPageLayout(EPageLayout eLayout); diff --git a/PdfFile/SrcWriter/Document.cpp b/PdfFile/SrcWriter/Document.cpp index 83c48e61e3..8fc613f9f4 100644 --- a/PdfFile/SrcWriter/Document.cpp +++ b/PdfFile/SrcWriter/Document.cpp @@ -563,12 +563,14 @@ namespace PdfWriter { CAnnotation* pAnnot = new CLinkAnnotation(m_pXref, pDest); pAnnot->SetRect(oRect); + m_pXref->Add(pAnnot); return pAnnot; } CAnnotation* CDocument::CreateUriLinkAnnot(const TRect& oRect, const char* sUrl) { CAnnotation* pAnnot = new CUriLinkAnnotation(m_pXref, sUrl); pAnnot->SetRect(oRect); + m_pXref->Add(pAnnot); return pAnnot; } CAnnotation* CDocument::CreateInkAnnot() @@ -627,6 +629,7 @@ namespace PdfWriter } void CDocument::AddAnnotation(const int& nID, CAnnotation* pAnnot) { + m_pXref->Add(pAnnot); m_mAnnotations[nID] = pAnnot; } CImageDict* CDocument::CreateImage() @@ -1482,14 +1485,14 @@ namespace PdfWriter CXref* pXrefCatalog = new CXref(this, m_pCatalog->GetObjId()); if (pXrefCatalog) { - pXrefCatalog->Add(m_pCatalog->Copy()); + pXrefCatalog->Add(m_pCatalog->Copy(), m_pCatalog->GetGenNo()); pXrefCatalog->SetPrev(m_pXref); } CXref* pXrefPage = new CXref(this, m_vSignatures[i].pPage->GetObjId()); if (pXrefPage) { - pXrefPage->Add(m_vSignatures[i].pPage->Copy()); + pXrefPage->Add(m_vSignatures[i].pPage->Copy(), m_vSignatures[i].pPage->GetGenNo()); pXrefPage->SetPrev(pXrefCatalog); } diff --git a/PdfFile/SrcWriter/Info.h b/PdfFile/SrcWriter/Info.h index 46295fb819..d0aad92442 100644 --- a/PdfFile/SrcWriter/Info.h +++ b/PdfFile/SrcWriter/Info.h @@ -73,6 +73,7 @@ namespace PdfWriter { public: CInfoDict(CXref* pXref); + CInfoDict() {} void SetInfo(EInfoType eType, const char* sValue); const char* GetInfo(EInfoType eType); void SetInfo(EInfoType eType, const TDate& oDate); diff --git a/PdfFile/SrcWriter/Objects.cpp b/PdfFile/SrcWriter/Objects.cpp index b2a424cf76..6ce4dd0717 100644 --- a/PdfFile/SrcWriter/Objects.cpp +++ b/PdfFile/SrcWriter/Objects.cpp @@ -842,7 +842,7 @@ namespace PdfWriter return NULL; } - void CXref::Add(CObjectBase* pObject) + void CXref::Add(CObjectBase* pObject, unsigned int unObjectGen) { if (!pObject) return; @@ -856,7 +856,6 @@ namespace PdfWriter return; } - // В случае ошибки r объектe нужно применить dispose TXrefEntry* pEntry = new TXrefEntry; if (NULL == pEntry) @@ -869,7 +868,7 @@ namespace PdfWriter pEntry->nEntryType = IN_USE_ENTRY; pEntry->unByteOffset = 0; - pEntry->unGenNo = 0; + pEntry->unGenNo = unObjectGen; pEntry->pObject = pObject; pObject->SetRef(m_unStartOffset + m_arrEntries.size() - 1, pEntry->unGenNo); pObject->SetIndirect(); diff --git a/PdfFile/SrcWriter/Objects.h b/PdfFile/SrcWriter/Objects.h index 945e523136..b4a806e904 100644 --- a/PdfFile/SrcWriter/Objects.h +++ b/PdfFile/SrcWriter/Objects.h @@ -508,7 +508,7 @@ namespace PdfWriter TXrefEntry* GetEntry(unsigned int unIndex) const; TXrefEntry* GetEntryByObjectId(unsigned int unObjectId) const; CXref* GetXrefByObjectId(unsigned int unObjectId); - void Add(CObjectBase* pObject); + void Add(CObjectBase* pObject, unsigned int unObjectGen = 0); void WriteToStream(CStream* pStream, CEncrypt* pEncrypt, bool bStream = false); void SetPrev(CXref* pPrev) { diff --git a/PdfFile/SrcWriter/Pages.cpp b/PdfFile/SrcWriter/Pages.cpp index ac5ef7437a..e5125ce7de 100644 --- a/PdfFile/SrcWriter/Pages.cpp +++ b/PdfFile/SrcWriter/Pages.cpp @@ -183,7 +183,6 @@ namespace PdfWriter //---------------------------------------------------------------------------------------- CPageTree::CPageTree(CXref* pXref) { - m_pXref = pXref; pXref->Add(this); m_pPages = new CArrayObject(); @@ -193,11 +192,8 @@ namespace PdfWriter Add("Kids", m_pPages); Add("Count", m_pCount); } - CPageTree::CPageTree(CXref* pXref, bool bEmpty) + CPageTree::CPageTree() { - m_pXref = pXref; - pXref->Add(this); - m_pPages = NULL; m_pCount = NULL; } @@ -314,9 +310,9 @@ namespace PdfWriter //---------------------------------------------------------------------------------------- // CPage //---------------------------------------------------------------------------------------- - CPage::CPage(CXref* pXref, CDocument* pDocument) + CPage::CPage(CDocument* pDocument) { - Init(pXref, pDocument); + Init(pDocument); } void CPage::Fix() { @@ -431,7 +427,8 @@ namespace PdfWriter } CPage::CPage(CXref* pXref, CPageTree* pParent, CDocument* pDocument) { - Init(pXref, pDocument); + pXref->Add(this); + Init(pDocument); m_pContents = new CArrayObject(); CDictObject* pContent = new CDictObject(pXref); @@ -454,11 +451,8 @@ namespace PdfWriter pGrState = pPrev; } } - void CPage::Init(CXref* pXref, CDocument* pDocument) + void CPage::Init(CDocument* pDocument) { - pXref->Add(this); - - m_pXref = pXref; m_pDocument = pDocument; m_eGrMode = grmode_PAGE; m_pGrState = new CGrState(NULL); diff --git a/PdfFile/SrcWriter/Pages.h b/PdfFile/SrcWriter/Pages.h index edeffed1f2..13ad7eb549 100644 --- a/PdfFile/SrcWriter/Pages.h +++ b/PdfFile/SrcWriter/Pages.h @@ -62,7 +62,7 @@ namespace PdfWriter { public: CPageTree(CXref* pXref); - CPageTree(CXref* pXref, bool bEmpty); + CPageTree(); void Fix(); void AddPage(CDictObject* pPage); CObjectBase* GetObj(int nPageIndex); @@ -83,7 +83,6 @@ namespace PdfWriter CNumberObject* m_pCount; CArrayObject* m_pPages; - CXref* m_pXref; }; //---------------------------------------------------------------------------------------- // CPage @@ -91,7 +90,7 @@ namespace PdfWriter class CPage : public CDictObject { public: - CPage(CXref* pXref, CDocument* pDocument); + CPage(CDocument* pDocument); CPage(CXref* pXref, CPageTree* pParent, CDocument* pDocument); ~CPage(); @@ -165,7 +164,7 @@ namespace PdfWriter private: - void Init(CXref* pXref, CDocument* pDocument); + void Init(CDocument* pDocument); void EllipseArc(double dX, double dY, double dXRad, double dYRad, double dAngle1, double dAngle2, bool bClockDirection); CArrayObject* GetMediaBoxItem(); CDictObject* GetResourcesItem(); @@ -187,7 +186,6 @@ namespace PdfWriter CDocument* m_pDocument; CPageTree* m_pParent; - CXref* m_pXref; CPoint m_oStartPos; // Позиция начала текущего пата CPoint m_oCurPos; // Текущая позиция пата CPoint m_oTextPos; // Текущая позиция текста