I have a Uint8Array that is actually a content of the PDF file. I'd like to find the index of certain string that is located in that array, so I can insert some content at that position.
To do that I am actually converting the Uint8Array to string, and then search that string for the string I'd like to find the index for.
Here's the snippet
const pdfStr = new TextDecoder('utf-8').decode(array);
// find ByteRange
const byteRangePos = this.getSubstringIndex(pdfStr, '/ByteRange [', 1);
if (byteRangePos === -1) {
throw new Error(
'Failed to locate ByteRange.'
);
}
getSubstringIndex = (str, substring, n) => {
let times = 0, index = null;
while (times < n && index !== -1) {
index = str.indexOf(substring, index + 1);
times++;
}
return index;
}
array = this.updateArray(array, (byteRangePos + '/ByteRange '.length), byteRange);
The problem I'm encountering is the the utf-8 chars are encoded in bytes of variable length (1-4 bytes), so the length of the string I get is less than the length of the UInt8Array itself, so the index I get by searching the string doesn't match the actual place '/ByteRange' string is in UInt8Array, so it gets inserted before it should.
Is there any way to get a 1-byte string representation of UInt8Array like ASCII or something like that?
I solved the problem, by changing
const pdfStr = new TextDecoder('utf-8').decode(array);
to
const pdfStr = new TextDecoder('ascii').decode(array);