Skip to content

Commit fe0135d

Browse files
authored
Merge pull request #642 from embedpdf/fix/preserve-custom-nm
Preserve custom annotation /NM values
2 parents cf22b95 + 03e009f commit fe0135d

2 files changed

Lines changed: 14 additions & 6 deletions

File tree

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
---
2+
'@embedpdf/engines': patch
3+
---
4+
5+
Preserve custom annotation `/NM` values instead of rewriting them to a UUID v4.
6+
7+
The engine previously overwrote any `/NM` (annotation name) that wasn't a UUID v4 — both when creating new annotations (rewriting the caller's `annotation.id`) and when reading existing ones (mutating the on-disk value as a side effect of opening a PDF). This broke any consumer using a custom identity scheme (e.g. ULIDs, `firm-2024-001`, etc.).
8+
9+
The engine now only generates a UUID v4 when `/NM` is empty or missing; any non-empty value is kept as-is. PDFium's `EPDFPage_GetAnnotByName` lookup only needs a unique string, so no functional behaviour changes for callers that don't supply a custom id.

packages/engines/src/lib/pdfium/engine.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,6 @@ import {
106106
PdfVerticalAlignment,
107107
AnnotationCreateContext,
108108
getImageMetadata,
109-
isUuidV4,
110109
uuidV4,
111110
PdfAnnotationName,
112111
PdfAnnotationReplyType,
@@ -1004,7 +1003,7 @@ export class PdfiumNative implements IPdfiumExecutor {
10041003
if (subtype !== PdfAnnotationSubtype.WIDGET) return;
10051004

10061005
let annotationId = this.getAnnotString(annotPtr, 'NM');
1007-
if (!annotationId || !isUuidV4(annotationId)) {
1006+
if (!annotationId) {
10081007
annotationId = uuidV4();
10091008
this.setAnnotString(annotPtr, 'NM', annotationId);
10101009
}
@@ -1245,7 +1244,7 @@ export class PdfiumNative implements IPdfiumExecutor {
12451244
});
12461245
}
12471246

1248-
if (!isUuidV4(annotation.id)) {
1247+
if (!annotation.id) {
12491248
annotation.id = uuidV4();
12501249
}
12511250

@@ -5671,7 +5670,7 @@ export class PdfiumNative implements IPdfiumExecutor {
56715670
formHandle: number,
56725671
): PdfWidgetAnnoObject | undefined {
56735672
let index = this.getAnnotString(annotationPtr, 'NM');
5674-
if (!index || !isUuidV4(index)) {
5673+
if (!index) {
56755674
index = uuidV4();
56765675
this.setAnnotString(annotationPtr, 'NM', index);
56775676
}
@@ -5761,7 +5760,7 @@ export class PdfiumNative implements IPdfiumExecutor {
57615760
formHandle?: number,
57625761
) {
57635762
let index = this.getAnnotString(annotationPtr, 'NM');
5764-
if (!index || !isUuidV4(index)) {
5763+
if (!index) {
57655764
index = uuidV4();
57665765
this.setAnnotString(annotationPtr, 'NM', index);
57675766
}
@@ -8667,7 +8666,7 @@ export class PdfiumNative implements IPdfiumExecutor {
86678666
if (!parentPtr) return;
86688667

86698668
let nm = this.getAnnotString(parentPtr, 'NM');
8670-
if (!nm || !isUuidV4(nm)) {
8669+
if (!nm) {
86718670
nm = uuidV4();
86728671
this.setAnnotString(parentPtr, 'NM', nm);
86738672
}

0 commit comments

Comments
 (0)