Skip to content
Lamina PDF
0.9.0
GitHub
Open the web app

Exchange comments with other apps

You’ll send a report’s comments to a reviewer who uses another PDF app, and bring their answers back into your copy. You need this when people review the same document in different apps, or when the comments should travel without the PDF.

exportAnnotations writes the comments as XFDF, the format Acrobat and most PDF apps import. importAnnotations reads the XFDF or FDF file the reviewer sends back and adds its comments to the document as one change:

packages/sdk/examples/exchange-comments.ts
// Send a PDF's comments to a reviewer who uses another app, and bring their comments back:
// export them as XFDF, which Acrobat and most PDF apps import, and import the XFDF file they
// send back. The import keeps the comments already in the file and adds the new ones, with
// their replies and statuses, as one change that a single undo takes back.
import { Document } from "@laminapdf/sdk";
/**
* The document's comments as XFDF, recording the file's name so an import can tell when it is
* given to another PDF. Needs `init({ extract: true })`.
*/
export function exportComments(bytes: Uint8Array, name: string): Uint8Array<ArrayBuffer> {
using doc = Document.open(bytes);
return doc.exportAnnotations({ format: "xfdf", file: name });
}
/** The document with a reviewer's XFDF or FDF comments added to its own. */
export function importComments(bytes: Uint8Array, xfdf: Uint8Array): Uint8Array<ArrayBuffer> {
using doc = Document.open(bytes);
const { summary } = doc.importAnnotations(xfdf, { mode: "merge" });
console.log(`${summary.added} comments added, ${summary.matched} already here`);
// A sound, a form field's value or a comment for a missing page is left out, and said.
for (const warning of summary.warnings) {
console.warn(warning.code, warning.kind ?? "", warning.reason ?? "");
}
return doc.save();
}

The comments already in your copy stay as they are. The reviewer’s new comments, replies and statuses join them, each drawn the way Lamina PDF draws its own, and one undo takes them all back.

  • XFDF and FDF keep every reply with the comment it answers, carry attached files, and record which PDF they were made for. Importing a file made for another PDF still works, but the summary warns with interchange-document-mismatch: its comments may land in the wrong places. The check compares the two PDFs’ identifiers, or, when either has none, the file name you pass as file.
  • The import matches comments by their name (/NM) on the same page. By default, a comment the file names that is already in your copy is kept as it is, and the file’s other comments are added.
  • What Lamina PDF can’t make is left out and listed in summary.warnings as interchange-skipped: a sound comment, a link, a form field’s value, or a comment for a page your copy doesn’t have.
  • A file that doesn’t read fails with interchange-malformed, which names the line where reading stopped. An XFDF file that declares a DTD is refused with interchange-dtd, since a DTD can define entities that expand without end or read other files.
  • See what an import will do first: previewImport gives the same summary without changing anything: how many comments it adds and on which pages, and what it leaves out.
  • Replace the comments the file names: pass { mode: "replace" }, and each one already in your copy is deleted, with its replies, and the file’s version added.
  • Keep both: pass { mode: "add" }, and the file’s comments are added as copies.
  • Notes for a notes app: export { format: "markdown", comments: "highlightsAndNotes" }. Each page gets a heading, and each highlight’s text is quoted under it.
  • FDF instead of XFDF: export { format: "fdf" }, for apps that only read FDF.

Next: answer a review to reply to the comments you brought back.