Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

99
Visualizações
PyPDF2 decoding issue when adding annotations in Chinese characters with addJS

I want to use PyPDF2 to add annotations programmatically with the use of addJS, it works very well for Latins characters but not for Chinese character, tried to encode with UTF-8 but seems not work either. Here are the code:

from PyPDF2 import PdfFileWriter, PdfFileReader
Def Test():
    inputPDF = PdfFileReader('./demo/TESTPDFANNOTATION.pdf', "rb")    
    outputPDF = PdfFileWriter()
       
    pages = inputPDF.getNumPages()
    for p in range(pages):
        outputPDF.addPage(inputPDF.getPage(p))

    outputStream = open('./demo/TESTPDFANNOTATIONOUT.pdf', "wb")
    outputPDF.addJS("var annot = this.addAnnot({ \r \
                    page: 0, \r \
                    type: 'FreeText', \r \
                    contents: '你好', \r \
                    textFont: 'csongl', \r \
                    textSize: 10, \r \
                    rect: [200, 300, 200+150, 300+3*12], // height for three lines \r \
                    width: 1, \r \
                    alignment: 1 \r \
                    });")
    outputPDF.write(outputStream)    
    outputStream.close()
    return("ok")

It's weird that If I opened the PDF in notepad text editor, the Chinese characters displayed correctly however when opened with PDF, it shows something like 佀好, which seems not decoded, since they could be decoded with the online convert tool into almost the right Chinese character, not exactly the same for some cases. https://cafewebmaster.com/online_tools/utf_decode

Any advise would be highly appreciated!

Python version: 3.9+ OS: Win10

Thanks Stanley

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Finally, figured out to use another package PyMuPDF to add annotations programmatically with good support of Chinese characters.

import fitz

def writeAnnotation():
    blue  = (0,0,1)
    gold  = (1,1,0)

    pdfDoc = fitz.open('./demo/TESTPDFANNOTATION.pdf')
    page = pdfDoc[0]

    rect1 = fitz.Rect(100,100,200,150)

    strContent1= "你好!世界"

    a1 = page.addFreetextAnnot(rect1, strContent1, text_color=blue,  fill_color=gold)

    pdfDoc.save("./demo/TESTPDFANNOTATIONOUT.pdf")
    return("Well done!")
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda