aboutsummaryrefslogtreecommitdiffstats
path: root/libqpdf/QPDFPageObjectHelper.cc
diff options
context:
space:
mode:
authorJay Berkenbilt <ejb@ql.org>2021-02-21 22:14:52 +0100
committerJay Berkenbilt <ejb@ql.org>2021-02-23 00:42:06 +0100
commit61d41e2e8858d8c51e097d8f993291dc210f30a5 (patch)
treeb3e1cbfb7f4cc44f7f0a2fd3abde630dbd0bccf6 /libqpdf/QPDFPageObjectHelper.cc
parent7b3cbacf5dc78b6f2b57204796f7dd0880a68b66 (diff)
downloadqpdf-61d41e2e8858d8c51e097d8f993291dc210f30a5.tar.zst
Add copyAnnotations, use with overlay/underlay (fixes #395)
Diffstat (limited to 'libqpdf/QPDFPageObjectHelper.cc')
-rw-r--r--libqpdf/QPDFPageObjectHelper.cc75
1 files changed, 75 insertions, 0 deletions
diff --git a/libqpdf/QPDFPageObjectHelper.cc b/libqpdf/QPDFPageObjectHelper.cc
index 18ac4d02..aa7eb912 100644
--- a/libqpdf/QPDFPageObjectHelper.cc
+++ b/libqpdf/QPDFPageObjectHelper.cc
@@ -1236,3 +1236,78 @@ QPDFPageObjectHelper::flattenRotation(QPDFAcroFormDocumentHelper* afdh)
this->oh.replaceKey("/Annots", QPDFObjectHandle::newArray(new_annots));
}
}
+
+void
+QPDFPageObjectHelper::copyAnnotations(
+ QPDFPageObjectHelper from_page, QPDFMatrix const& cm,
+ QPDFAcroFormDocumentHelper* afdh,
+ QPDFAcroFormDocumentHelper* from_afdh)
+{
+ auto old_annots = from_page.getObjectHandle().getKey("/Annots");
+ if (! old_annots.isArray())
+ {
+ return;
+ }
+
+ QPDF* from_qpdf = from_page.getObjectHandle().getOwningQPDF();
+ if (! from_qpdf)
+ {
+ throw std::runtime_error(
+ "QPDFPageObjectHelper::copyAnnotations:"
+ " from page is a direct object");
+ }
+ QPDF* this_qpdf = this->oh.getOwningQPDF();
+ if (! this_qpdf)
+ {
+ throw std::runtime_error(
+ "QPDFPageObjectHelper::copyAnnotations:"
+ " this page is a direct object");
+ }
+
+ std::vector<QPDFObjectHandle> new_annots;
+ std::vector<QPDFObjectHandle> new_fields;
+ std::set<QPDFObjGen> old_fields;
+ PointerHolder<QPDFAcroFormDocumentHelper> afdhph;
+ PointerHolder<QPDFAcroFormDocumentHelper> from_afdhph;
+ if (! afdh)
+ {
+ afdhph = new QPDFAcroFormDocumentHelper(*this_qpdf);
+ afdh = afdhph.getPointer();
+ }
+ if (this_qpdf == from_qpdf)
+ {
+ from_afdh = afdh;
+ }
+ else if (from_afdh)
+ {
+ if (from_afdh->getQPDF().getUniqueId() != from_qpdf->getUniqueId())
+ {
+ throw std::logic_error(
+ "QPDFAcroFormDocumentHelper::copyAnnotations: from_afdh"
+ " is not from the same QPDF as from_page");
+ }
+ }
+ else
+ {
+ from_afdhph = new QPDFAcroFormDocumentHelper(*from_qpdf);
+ from_afdh = from_afdhph.getPointer();
+ }
+
+ afdh->transformAnnotations(
+ old_annots, new_annots, new_fields, old_fields, cm,
+ from_qpdf, from_afdh);
+ for (auto const& f: new_fields)
+ {
+ afdh->addFormField(QPDFFormFieldObjectHelper(f));
+ }
+ auto annots = this->oh.getKey("/Annots");
+ if (! annots.isArray())
+ {
+ annots = QPDFObjectHandle::newArray();
+ this->oh.replaceKey("/Annots", annots);
+ }
+ for (auto const& annot: new_annots)
+ {
+ annots.appendItem(annot);
+ }
+}