-
-
Notifications
You must be signed in to change notification settings - Fork 199
Add widget page support for placeholder-plain #246
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: develop
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,4 @@ | ||
| import {SignPdfError} from '@signpdf/utils'; | ||
| import getPagesDictionaryRef from './getPagesDictionaryRef'; | ||
| import findObject from './findObject'; | ||
|
|
||
|
|
@@ -6,14 +7,19 @@ import findObject from './findObject'; | |
| * | ||
| * @param {Buffer} pdfBuffer | ||
| * @param {Object} info As extracted from readRef() | ||
| * @param {Number} [pageNumber = 0] Desired page number | ||
| */ | ||
| export default function getPageRef(pdfBuffer, info) { | ||
| export default function getPageRef(pdfBuffer, info, pageNumber = 0) { | ||
| const pagesRef = getPagesDictionaryRef(info); | ||
| const pagesDictionary = findObject(pdfBuffer, info.xref, pagesRef); | ||
| const kidsPosition = pagesDictionary.indexOf('/Kids'); | ||
| const kidsStart = pagesDictionary.indexOf('[', kidsPosition) + 1; | ||
| const kidsEnd = pagesDictionary.indexOf(']', kidsPosition); | ||
| const pages = pagesDictionary.slice(kidsStart, kidsEnd).toString(); | ||
| const split = pages.trim().split(' ', 3); | ||
| return `${split[0]} ${split[1]} ${split[2]}`; | ||
| const pagesSplit = []; | ||
| pages.trim().split(' ').forEach((v, i) => (i % 3 === 0 ? pagesSplit.push([v]) : pagesSplit[pagesSplit.length - 1].push(v))); | ||
| if (pageNumber < 0 || pagesSplit.length <= pageNumber) { | ||
| throw new SignPdfError(`Failed to get reference of page "${pageNumber}".`, SignPdfError.TYPE_INPUT); | ||
| } | ||
| return pagesSplit[pageNumber].join(' '); | ||
|
Comment on lines
+21
to
+24
|
||
| } | ||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -42,6 +42,7 @@ const getAcroFormRef = (slice) => { | |||||
| * @property {number} [signatureLength] | ||||||
| * @property {string} [subFilter] One of SUBFILTER_* from \@signpdf/utils | ||||||
| * @property {number[]} [widgetRect] [x1, y1, x2, y2] widget rectangle | ||||||
| * @property {number} [widgetPage] Page number where the widget should be placed | ||||||
|
||||||
| * @property {number} [widgetPage] Page number where the widget should be placed | |
| * @property {number} [widgetPage] 0-based page index where the widget should be placed |
Copilot
AI
Mar 27, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The new widgetPage option changes behavior (allows placing the widget on non-first pages) but there’s no test covering multi-page PDFs or out-of-range page selection. Add/update tests to assert the annotation is added to the requested page (a multi-page fixture like resources/issue-158-test.pdf could work) and that invalid widgetPage values surface a SignPdfError.TYPE_INPUT.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
/Kidsparsing usespages.trim().split(' '), which only splits on a single space and will break on valid PDFs that use other whitespace (newlines, tabs) or multiple spaces between tokens. This can mis-group theobj gen Rtriples and return the wrong page ref. Consider tokenizing with a whitespace regex (and filtering empty tokens) and then selecting the 3 tokens forpageNumber(or iterating until the desired index) with a clear length check.