unidoc / unipdf

Golang PDF library for creating and processing PDF files (pure go)
https://unidoc.io
Other
2.56k stars 251 forks source link

[BUG] Cannot fill pdf with japanese or korean form field #418

Closed tangximing closed 3 years ago

tangximing commented 3 years ago

Description

Cannot fill pdf with japanese or korean form field

Expected Behavior

form field can be filled with text in chinese, english, japanese and korean

Actual Behavior

  1. prepare pdf test.pdf

  2. read form fields [ { "name": "ㅇㄹㅎ", "value": "" }, { "name": "电饭锅", "value": "" } ]

  3. fill pdf with fields [ { "name": "ㅇㄹㅎ", "value": "ㅇㅎㅇㅎ" }, { "name": "电饭锅", "value": "订个蛋糕" } ]

  4. actual result out.pdf

  5. expected result which is edited with foxit reader test2.pdf

  6. code of filling form ` type FillFormArg struct { Filename string FormJson string OutFilename string } func FillForm(arg *FillFormArg) (err error) { b := []byte(arg.FormJson) jsonReader := bytes.NewReader(b)

    fd, err := fjson.LoadFromJSON(jsonReader) if err != nil { return err }

    f, err := os.Open(arg.Filename) if err != nil { return } defer f.Close()

    pdfReader, err := model.NewPdfReader(f) if err != nil { return }

    // Populate the form data. err = pdfReader.AcroForm.Fill(fd) if err != nil { return }

    // Flatten form. fieldAppearance := annotator.FieldAppearance{OnlyIfMissing: true, RegenerateTextFields: true}

    // NOTE: To customize certain styles try: // style := fieldAppearance.Style() // style.CheckmarkGlyph = "a22" // style.AutoFontSizeFraction = 0.70 // fieldAppearance.SetStyle(style) // // or for specifying a full set of appearance styles: // fieldAppearance.SetStyle(annotator.AppearanceStyle{ // CheckmarkGlyph: "a22", // AutoFontSizeFraction: 0.70, // FillColor: model.NewPdfColorDeviceGray(0.8), // BorderColor: model.NewPdfColorDeviceRGB(1, 0, 0), // BorderSize: 2.0, // AllowMK: false, // })

    err = pdfReader.FlattenFields(true, fieldAppearance) if err != nil { return err }

    // Write out. pdfWriter := model.NewPdfWriter() pdfWriter.SetForms(nil)

    for _, p := range pdfReader.PageList { err := pdfWriter.AddPage(p) if err != nil { return err } }

    out, err := os.Create(arg.OutFilename) if err != nil { return err } defer out.Close()

    if err = pdfWriter.Write(out); err != nil { return } return } `

3ace commented 3 years ago

Hi @tangximing for using CJK font to fill a pdf form, you should first set the font for the text field that support CJK characters.

We have a code example for doing that:

https://github.com/unidoc/unipdf-examples/blob/master/forms/pdf_form_fill_custom_font.go

tangximing commented 3 years ago

Hi @tangximing for using CJK font to fill a pdf form, you should first set the font for the text field that support CJK characters.

We have a code example for doing that:

https://github.com/unidoc/unipdf-examples/blob/master/forms/pdf_form_fill_custom_font.go

@3ace I have tried the example, and it seems that we need to know what's the language in the text field in advance, but in out situation, the text field may be filled with any language, so how do i fix this? and if i can set the font in pdf not in code like below ` style.Fonts = &annotator.AppearanceFontStyle{ Fallback: &annotator.AppearanceFont{ Font: defaultFontReplacement, Name: defaultFontReplacement.FontDescriptor().FontName.String(), Size: 0, }, FieldFallbacks: map[string]*annotator.AppearanceFont{ "email4": { Font: fontReplacement, Name: fontReplacement.FontDescriptor().FontName.String(), Size: 0, }, "address5[addr_line1]": { Font: cjkFont, Name: cjkFont.FontDescriptor().FontName.String(), Size: 0, }, "address5[addr_line2]": { Font: cjkFont, Name: cjkFont.FontDescriptor().FontName.String(), Size: 0, }, "address5[city]": { Font: cjkFont, Name: cjkFont.FontDescriptor().FontName.String(), Size: 0, }, "address5[state]": { Font: cjkFont, Name: cjkFont.FontDescriptor().FontName.String(), Size: 0, }, "address5[postal]": { Font: cjkFont, Name: cjkFont.FontDescriptor().FontName.String(), Size: 0, }, }, ForceReplace: true, }

`

tangximing commented 3 years ago

@3ace I have found a font to support text field with chinese、english、japanese and korean and it's ok with your code.

3ace commented 3 years ago

@tangximing glad to hear that. Please let us know if you need further assistance.