Node JS Content Replacer for Pagination?

I have this code i wrote where I stamp the same header on every page with a place holder for the PAGE_NUMBER, then i iterate over all of the pages and replace the placeholder with the page number. For some reason, the content replacer always replaces the placeholder on every single page with the same page number never the actual corresponding one, so page 5 shows page 1, page 10 shows page 1, even though I am logging things out and see the pages and current page index are correct corresponding to their page. So my question is why does content replacer not work on a per page basis especially if i am doing the action to process on a per page basis? I am very confused, could someone shed some light on to this or maybe another solution?

I tried to use the stamper class, the problem i have is my design calls for this page number to be dynamically place so its not in the same spot every time, that is why i must rely on replacement logic.

/**

Post process the PDF with pagination for page number as well as total page count.
@param doc -Finished Apryse PDF that will be sent or uploaded to end consumer.*/
async function withPagination(doc: PDFNet.PDFDoc) {
// Initialize Apryse Modules
const replacer = await PDFNet.ContentReplacer.create();

// Pagination Constants

const PAGE_COUNT = await doc.getPageCount();

const pagesIterator = await doc.getPageIterator(1);

for (pagesIterator; await pagesIterator.hasNext(); pagesIterator.next()) {
const page = await pagesIterator.current();
const pageNumber = await page.getIndex();
await replacer.addString(PAGE_NUMBER_PATTERN, ${pageNumber}.padStart(2, " "));
await replacer.process(page);
}

}

Hello,

Thanks for reaching out to us. I understand that you are dynamically trying to add a different page number for each of your pages using the ContentReplacer. Can you please send me the input file which contains the file content you are replacing through email? You can email this via our support email: support@apryse.com

In the email, can you please go into detail on your replacement logic? Generally you can use the Stamper class to add an iterative page number onto a page as is shown here:

I understand you mention this does not work for you. Given your use case, If you can elaborate on why this does not work for you based on your previous tests, our team can formulate the best solution moving forward.

We look forward to hearing from you in regards to these details.

Thanks,

Thanks using the stamper ultimately worked, but the way stamper and content replacer work is a little odd to me maybe you could shed some light on how it works. If I use a page set and stamp for that entire range of pages in a pdf then it doesn’t work and only applies to the first page. However if i iterate over every single page and stamp each page as its own range, then everything works normally with content replacer. So my question is, does stamper need to explicitly be burned onto every single page when used with content replacer? Also not that i am flattening annotations as well.

Hi,

I’m glad to hear that the stamper solution worked for you. It’s hard to say why your usage of ContentReplacer was giving you the initial behavior. Our team would need to examine the PDF file you were replacing to fundamentally know why the behavior you were seeing was happening. Please feel free to reach out to us via email with the file if this is a concern. Please also include a full code sample which shows your approach.