Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .changeset/pink-balloons-make.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'@embedpdf/engines': minor
'@embedpdf/models': minor
'@embedpdf/pdfium': minor
---

Add EPDF_GetPageObjNum to expose PDF page indirect object numbers, populate objectNumber on PdfPageObject via the engine
12 changes: 11 additions & 1 deletion packages/engines/src/lib/pdfium/engine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -385,13 +385,23 @@ export class PdfiumNative implements IPdfiumExecutor {

const rotation = this.pdfiumModule.EPDF_GetPageRotationByIndex(docPtr, index) as Rotation;

const page = {
let objectNumber: number | undefined;
const epdfGetPageObjNum = this.pdfiumModule.EPDF_GetPageObjNum;
if (typeof epdfGetPageObjNum === 'function') {
const objNum = epdfGetPageObjNum(docPtr, index);
if (objNum > 0) {
objectNumber = objNum;
}
}

const page: PdfPageObject = {
index,
size: {
width: this.pdfiumModule.pdfium.getValue(sizePtr, 'float'),
height: this.pdfiumModule.pdfium.getValue(sizePtr + 4, 'float'),
},
rotation,
...(objectNumber !== undefined ? { objectNumber } : {}),
};

pages.push(page);
Expand Down
15 changes: 15 additions & 0 deletions packages/models/src/pdf.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,21 @@ export interface PdfPageObject {
*/
index: number;

/**
* PDF page dictionary indirect object number.
*
* Populated by the engine at document open time when the underlying WASM
* build exports `EPDF_GetPageObjNum`. Undefined when that export is absent
* (e.g. older builds) or when the page dictionary has no valid object number.
*
* This value is stable across page reorder / insert / delete operations on
* the same file, making it suitable as a permanent page reference for
* annotations, citations, and similar features.
*
* @public
*/
objectNumber?: number;

/**
* Orignal size of this page
*/
Expand Down
2 changes: 2 additions & 0 deletions packages/pdfium/build/code/cpp/ext_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ void PDFiumExt_ExitFormFillEnvironment(void* formHandle);

int PDFiumExt_SaveAsCopy(void* document, void* writer);

int EPDF_GetPageObjNum(void* document, int page_index);

#ifdef __cplusplus
}
#endif
16 changes: 16 additions & 0 deletions packages/pdfium/build/code/cpp/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
#include <emscripten.h>
#include "filewriter.h"
#include "string.h"
#include "fpdfsdk/cpdfsdk_helpers.h"
#include "core/fpdfapi/parser/cpdf_document.h"
#include "core/fpdfapi/parser/cpdf_dictionary.h"

#ifdef __cplusplus
extern "C"
Expand All @@ -23,6 +26,8 @@ extern "C"

EMSCRIPTEN_KEEPALIVE int PDFiumExt_SaveAsCopy(void *document, void *writer);

EMSCRIPTEN_KEEPALIVE int EPDF_GetPageObjNum(FPDF_DOCUMENT document, int page_index);

#ifdef __cplusplus
}
#endif
Expand Down Expand Up @@ -91,3 +96,14 @@ void PDFiumExt_ExitFormFillEnvironment(void *form_handle)
{
FPDFDOC_ExitFormFillEnvironment(static_cast<FPDF_FORMHANDLE>(form_handle));
}

int EPDF_GetPageObjNum(FPDF_DOCUMENT document, int page_index)
{
CPDF_Document *pDoc = CPDFDocumentFromFPDFDocument(document);
if (!pDoc)
return -1;
RetainPtr<const CPDF_Dictionary> pDict = pDoc->GetPageDictionary(page_index);
if (!pDict)
return -1;
return static_cast<int>(pDict->GetObjNum());
}
1 change: 1 addition & 0 deletions packages/pdfium/build/compile.esm.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ em++ $(ls ./code/cpp/*.cpp) \
-sEXPORTED_FUNCTIONS=$(cat ./wasm/exported-functions.txt) \
-lpdfium \
-L/workspace/packages/pdfium/pdfium-src/out/wasm/obj \
-I/workspace/packages/pdfium/pdfium-src \
-I/workspace/packages/pdfium/pdfium-src/public \
-std=c++11 \
-Wall \
Expand Down
1 change: 1 addition & 0 deletions packages/pdfium/build/compile.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ em++ $(ls ./code/cpp/*.cpp) \
-sEXPORTED_FUNCTIONS=$(cat ./wasm/exported-functions.txt) \
-lpdfium \
-L/workspace/packages/pdfium/pdfium-src/out/wasm/obj \
-I/workspace/packages/pdfium/pdfium-src \
-I/workspace/packages/pdfium/pdfium-src/public \
-std=c++11 \
-Wall \
Expand Down
1 change: 1 addition & 0 deletions packages/pdfium/src/vendor/functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export const functions = {
] as const,
EPDF_GetMetaTrapped: [['number'] as const, 'number'] as const,
EPDF_GetPageRotationByIndex: [['number', 'number'] as const, 'number'] as const,
EPDF_GetPageObjNum: [['number', 'number'] as const, 'number'] as const,
EPDF_GetPageSizeByIndexNormalized: [['number', 'number', 'number'] as const, 'boolean'] as const,
EPDF_HasMetaText: [['number', 'string'] as const, 'boolean'] as const,
EPDF_IsEncrypted: [['number'] as const, 'boolean'] as const,
Expand Down
3 changes: 2 additions & 1 deletion packages/pdfium/src/vendor/pdfium.cjs

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions packages/pdfium/src/vendor/pdfium.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ var createPdfium = (() => {
'_EPDF_GetMetaKeyCount',
'_EPDF_GetMetaKeyName',
'_EPDF_GetMetaTrapped',
'_EPDF_GetPageObjNum',
'_EPDF_GetPageRotationByIndex',
'_EPDF_GetPageSizeByIndexNormalized',
'_EPDF_HasMetaText',
Expand Down Expand Up @@ -7709,6 +7710,8 @@ var createPdfium = (() => {
'FPDF_GetPageSizeByIndexF',
3,
));
var _EPDF_GetPageObjNum = (Module['_EPDF_GetPageObjNum'] =
createExportWrapper('EPDF_GetPageObjNum', 2));
var _EPDF_GetPageRotationByIndex = (Module['_EPDF_GetPageRotationByIndex'] =
createExportWrapper('EPDF_GetPageRotationByIndex', 2));
var _EPDF_GetPageSizeByIndexNormalized = (Module['_EPDF_GetPageSizeByIndexNormalized'] =
Expand Down