bool AnnotWriter::UpdateAnnotArray(int pageNo, Annotations* annots, Ref annotArray) { ASSERT(annots->HasChanged()); Object array; array.initArray(mXRef); for (int i = 0; i < annots->Length(); i++) { Annotation* a = annots->At(i); if (!a->IsDeleted()) { AddToAnnots(&array, a); if (a->GetPopup()) AddToAnnots(&array, a->GetPopup()); } } // write to file WriteObject(annotArray, &array); array.free(); return true; }
// Create new PDF file and append changed or new annotations bool AnnotWriter::WriteTo(const char* name) { if (!CopyFile(name)) return false; if (!mAnnots.HasChanged()) return true; AssignShortFontNames(); mFile = fopen(name, "a+b"); bool ok = mFile != NULL; int numPages = mDoc->getNumPages(); for (int i = 0; ok && i < numPages; i ++) { mPageRef = *mDoc->getCatalog()->getPageRef(i+1); Annotations* a = mAnnots.Get(i); if (a && a->HasChanged()) { Ref annotArray; ok = ok && UpdatePage(i, a, annotArray); ok = ok && UpdateAnnotArray(i, a, annotArray); for (int j = 0; ok && j < a->Length(); j ++) { Annotation* an = a->At(j); if (!an->IsDeleted()) { if (CanWrite(an)) { ok = UpdateAnnot(an); } } else { if (!is_empty_ref(an->GetRef())) { mXRefTable.DeleteRef(an->GetRef()); } } } } } if (ok) { UpdateInfoDict(); UpdateBePDFAcroForm(); UpdateCatalog(); ok = WriteXRefTable(); ok = ok && WriteFileTrailer(); } if (mFile) { fclose(mFile); mFile = NULL; } if (!ok) { // delete file on error unlink(name); } UnassignShortFontNames(); return ok; }