Closed tangximing closed 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
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, }
`
@3ace I have found a font to support text field with chinese、english、japanese and korean and it's ok with your code.
@tangximing glad to hear that. Please let us know if you need further assistance.
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
prepare pdf test.pdf
read form fields
[ { "name": "ㅇㄹㅎ", "value": "" }, { "name": "电饭锅", "value": "" } ]
fill pdf with fields
[ { "name": "ㅇㄹㅎ", "value": "ㅇㅎㅇㅎ" }, { "name": "电饭锅", "value": "订个蛋糕" } ]
actual result out.pdf
expected result which is edited with foxit reader test2.pdf
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 } `