pdfAnnotate icon indicating copy to clipboard operation
pdfAnnotate copied to clipboard

add utf8 characterannotaiton is failed

Open alanJ198 opened this issue 4 years ago • 5 comments

hi, when I try to add some annotations with utf8 character (for example Japanese), it seems not work. hope to get an advice for this problem, thanks

alanJ198 avatar Dec 18 '21 16:12 alanJ198

aha, I have solved the problem by myself.

alanJ198 avatar Dec 20 '21 00:12 alanJ198

aha, I have solved the problem by myself.

Hi , can you explain how it was solved ?

MarkMindCkm avatar Dec 26 '21 05:12 MarkMindCkm

@alanJ198, please share your solution, I also need it!


The following code (source is UTF-8):

factory.createTextAnnotation({
  // …
  contents: "Hello! こんにちは! Привет!",
  // …
})

Produces following annotation:

Hello! Sfikao ˜@825B!

How to write UTF-8 strings to annotations correctly?

Envek avatar Nov 09 '22 16:11 Envek

@Envek

function stringToUtf16Raw(text) {
  let out = [];
  out.push(String.fromCharCode(254));
  out.push(String.fromCharCode(255));
  for (let c of text) {
    c = c.charCodeAt(0);
    out.push(String.fromCharCode(c >> 8));
    out.push(String.fromCharCode(c & 0xFF));
  }
  return out.join('');
}

factory.createTextAnnotation({
  // …
  contents: stringToUtf16Raw("Hello! こんにちは! Привет!"),
  // …
})

suziwen avatar Feb 19 '23 14:02 suziwen

@Envek

function stringToUtf16Raw(text) {
  let out = [];
  out.push(String.fromCharCode(254));
  out.push(String.fromCharCode(255));
  for (let c of text) {
    c = c.charCodeAt(0);
    out.push(String.fromCharCode(c >> 8));
    out.push(String.fromCharCode(c & 0xFF));
  }
  return out.join('');
}

factory.createTextAnnotation({
  // …
  contents: stringToUtf16Raw("Hello! こんにちは! Привет!"),
  // …
})

no show char in browser when open the export pdf file

472530498 avatar May 29 '24 07:05 472530498