Attach custom attribute to annotation

Product: @pdftron/pdfnet-node

Product Version: 10.2.0

Please give a brief summary of your issue: I would like to attach a custom attribute to the annotation that has been generated by the nodejs sdk so I can distinguish with user’s annotation or prevent duplication.

Please provide a link to a minimal sample where the issue is reproducible:

<xfdf>
  <annots>
    <freetext CustomType="my-custom-type" TextColor="#2D4B8A" style="solid" width="0" creationdate="D:20230810033802Z" flags="print" date="D:20230810033802Z" name="f4aca86ebfdcd478-513552cf27700f8b" page="0" rect="353,14,587,48">
1 Like

The PDFNet Annot class has a function that allows you to add custom properties to your annotations. Here is a sample that adds a circle annotation to a blank input document. It then opens the output file and prints the custom properties added in the previous step.

const { PDFNet } = require('@pdftron/pdfnet-node')

const main = async () => {
    const doc = await PDFNet.PDFDoc.createFromFilePath('blank.pdf');
    const page = await doc.getPage(1);

    const circle = await PDFNet.CircleAnnot.create(
        doc,
        new PDFNet.Rect(100, 100, 200, 200)
    );
    await circle.setColor(await PDFNet.ColorPt.init(0, 1, 0), 3);
    await circle.setInteriorColor(await PDFNet.ColorPt.init(0, 0, 1), 3);
    const dash = [2, 4];
    await circle.setBorderStyle(
        await PDFNet.AnnotBorderStyle.createWithDashPattern(
        PDFNet.AnnotBorderStyle.Style.e_dashed,
        3,
        0,
        0,
        dash
        )
    );
    await circle.setPadding(new PDFNet.Rect(2, 2, 2, 2));
    await circle.refreshAppearance();
    circle.setCustomData('custom_string', 'custom_value');
    await page.annotPushBack(circle);
    await doc.save('output.pdf', PDFNet.SDFDoc.SaveOptions.e_incremental);

    const out_doc = await PDFNet.PDFDoc.createFromFilePath('output.pdf');
    const out_page = await doc.getPage(1);
    const out_annot = await out_page.getAnnot(0);
    let custom_value = await out_annot.getCustomData('custom_string');
    console.log('Annotation Custom Value: ' + custom_value);
}

PDFNet.runWithCleanup(main, '[YOUR LICENSE KEY HERE]').catch((err) => {
    console.log(err);    
}).then(() => {
    PDFNet.shutdown();
})
1 Like