Import and export annotations in C++

Aspose.PDF for C++ lets you transfer annotations between PDF documents through the page Annotations collection. The following example copies annotations from the first page of an existing PDF to a page in a new document.

Copy annotations from one PDF to another

The ImportExport function accepts the source PDF path and the output PDF path. It creates a blank destination page and copies the source annotations using Add(annot, true). The true argument requests that the annotation be copied rather than moved.

This example transfers annotations only. The source page text, images, and other page content are not copied. The input PDF must contain at least one page.

  1. Open the source PDF with Document.
  2. Create a destination Document.
  3. Add a Page to the destination document with PageCollection::Add().
  4. Get the first source page through PageCollection::idx_get().
  5. Iterate through the source page Annotations collection.
  6. Copy each annotation to the destination page with AnnotationCollection::Add(), passing true as the isCopy argument.
  7. Save the destination PDF with Document::Save().
#include <Aspose.PDF.Cpp/Document.h>
#include <Aspose.PDF.Cpp/InteractiveFeatures/Annotations/AnnotationCollection.h>
#include <Aspose.PDF.Cpp/Page.h>
#include <Aspose.PDF.Cpp/PageCollection.h>

using namespace System;
using namespace Aspose::Pdf;
using namespace Aspose::Pdf::Annotations;

void ImportExport(System::String inputFile, System::String outputFile)
{
    auto sourceDocument = MakeObject<Document>(inputFile);
    auto destinationDocument = MakeObject<Document>();
    auto page = destinationDocument->get_Pages()->Add();
    for (auto& annot : sourceDocument->get_Pages()->idx_get(1)->get_Annotations()) {
        page->get_Annotations()->Add(annot, true);
    }
    destinationDocument->Save(outputFile);
}