Skip to content

Commit

Permalink
Fix #657
Browse files Browse the repository at this point in the history
  • Loading branch information
hhrutter committed Jul 25, 2023
1 parent 337d257 commit fff3853
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 9 deletions.
3 changes: 1 addition & 2 deletions pkg/api/test/form_test.go
Expand Up @@ -55,8 +55,7 @@ func TestRemoveFormFields(t *testing.T) {
}
want := len(ss) - 2

// Remove "firstName1" by id'(=45) and "dob1" by name as per form list output.
if err := api.RemoveFormFieldsFile(inFile, outFile, []string{"dob1", "45"}, conf); err != nil {
if err := api.RemoveFormFieldsFile(inFile, outFile, []string{"dob1", "firstName1"}, conf); err != nil {
t.Fatalf("%s: %v\n", msg, err)
}

Expand Down
10 changes: 10 additions & 0 deletions pkg/cli/cli.go
Expand Up @@ -60,6 +60,7 @@ func ChangeOwnerPassword(cmd *Command) ([]string, error) {
// ListPermissions of inFile.
func ListPermissions(cmd *Command) ([]string, error) {
return api.ListPermissionsFile(*cmd.InFile, cmd.Conf)
// TODO turn struct into []string
}

// SetPermissions of inFile.
Expand Down Expand Up @@ -161,6 +162,7 @@ func ExtractMetadata(cmd *Command) ([]string, error) {
// ListAttachments returns a list of embedded file attachments for inFile.
func ListAttachments(cmd *Command) ([]string, error) {
return api.ListAttachmentsFile(*cmd.InFile, cmd.Conf)
// TODO turn struct into []string
}

// AddAttachments embeds inFiles into a PDF context read from inFile and writes the result to outFile.
Expand All @@ -181,6 +183,7 @@ func ExtractAttachments(cmd *Command) ([]string, error) {
// Info gathers information about inFile and returns the result as []string.
func Info(cmd *Command) ([]string, error) {
return api.InfoFiles(cmd.InFiles, cmd.PageSelection, cmd.Conf)
// TODO turn struct into []string
}

// CreateCheatSheetsFonts creates single page PDF cheat sheets for user fonts in current dir.
Expand All @@ -191,6 +194,7 @@ func CreateCheatSheetsFonts(cmd *Command) ([]string, error) {
// ListFonts gathers information about supported fonts and returns the result as []string.
func ListFonts(cmd *Command) ([]string, error) {
return api.ListFonts()
// TODO turn struct into []string
}

// InstallFonts installs True Type fonts into the pdfcpu pconfig dir.
Expand All @@ -201,6 +205,7 @@ func InstallFonts(cmd *Command) ([]string, error) {
// ListKeywords returns a list of keywords for inFile.
func ListKeywords(cmd *Command) ([]string, error) {
return api.ListKeywordsFile(*cmd.InFile, cmd.Conf)
// TODO turn struct into []string
}

// AddKeywords adds keywords to inFile's document info dict and writes the result to outFile.
Expand All @@ -216,6 +221,7 @@ func RemoveKeywords(cmd *Command) ([]string, error) {
// ListProperties returns inFile's properties.
func ListProperties(cmd *Command) ([]string, error) {
return api.ListPropertiesFile(*cmd.InFile, cmd.Conf)
// TODO turn struct into []string
}

// AddProperties adds properties to inFile's document info dict and writes the result to outFile.
Expand All @@ -236,6 +242,7 @@ func Collect(cmd *Command) ([]string, error) {
// ListBoxes returns inFile's page boundaries.
func ListBoxes(cmd *Command) ([]string, error) {
return api.ListBoxesFile(*cmd.InFile, cmd.PageSelection, cmd.PageBoundaries, cmd.Conf)
// TODO turn struct into []string
}

// AddBoxes adds page boundaries to inFile's page tree and writes the result to outFile.
Expand All @@ -256,6 +263,7 @@ func Crop(cmd *Command) ([]string, error) {
// ListAnnotations returns inFile's page annotations.
func ListAnnotations(cmd *Command) ([]string, error) {
_, ss, err := api.ListAnnotationsFile(*cmd.InFile, cmd.PageSelection, cmd.Conf)
// TODO turn struct into []string
return ss, err
}

Expand All @@ -268,6 +276,7 @@ func RemoveAnnotations(cmd *Command) ([]string, error) {
// ListImages returns inFiles embedded images.
func ListImages(cmd *Command) ([]string, error) {
return api.ListImagesFile(cmd.InFiles, cmd.PageSelection, cmd.Conf)
// turn struct into []string
}

// Dump known object to stdout.
Expand All @@ -286,6 +295,7 @@ func Create(cmd *Command) ([]string, error) {
// ListFormFields returns inFile's form field ids.
func ListFormFields(cmd *Command) ([]string, error) {
return api.ListFormFieldsFile(cmd.InFiles, cmd.Conf)
// TODO turn struct into []string
}

// RemoveFormFields removes some form fields from inFile.
Expand Down
7 changes: 6 additions & 1 deletion pkg/pdfcpu/form/fill.go
Expand Up @@ -605,7 +605,12 @@ func fillBtn(
fillDetails func(id, name string, fieldType FieldType, format DataFormat) ([]string, bool, bool),
ok *bool) error {

if len(d.ArrayEntry("Kids")) > 0 {
ff := d.IntEntry("Ff")
if ff != nil && primitives.FieldFlags(*ff)&primitives.FieldPushbutton > 0 {
return nil
}

if len(d.ArrayEntry("Kids")) > 0 && primitives.FieldFlags(*ff)&primitives.FieldRadio > 0 {
if err := fillRadioButtonGroup(ctx, d, id, name, locked, format, fillDetails, ok); err != nil {
return err
}
Expand Down
16 changes: 15 additions & 1 deletion pkg/pdfcpu/form/form.go
Expand Up @@ -332,6 +332,12 @@ func collectRadioButtonGroup(xRefTable *model.XRefTable, d types.Dict, f *Field,
}

func collectBtn(xRefTable *model.XRefTable, d types.Dict, f *Field, fm *FieldMeta) error {

ff := d.IntEntry("Ff")
if ff != nil && primitives.FieldFlags(*ff)&primitives.FieldPushbutton > 0 {
return nil
}

v := types.Name("Off")
if s, found := d.Find("DV"); found {
v = s.(types.Name)
Expand All @@ -350,7 +356,9 @@ func collectBtn(xRefTable *model.XRefTable, d types.Dict, f *Field, fm *FieldMet
}

if len(d.ArrayEntry("Kids")) > 0 {
return collectRadioButtonGroup(xRefTable, d, f, fm)
if ff != nil && primitives.FieldFlags(*ff)&primitives.FieldRadio > 0 {
return collectRadioButtonGroup(xRefTable, d, f, fm)
}
}

f.typ = FTCheckBox
Expand Down Expand Up @@ -1159,6 +1167,12 @@ func RemoveFormFields(ctx *model.Context, fieldIDsOrNames []string) (bool, error
}

func resetBtn(xRefTable *model.XRefTable, d types.Dict) error {

ff := d.IntEntry("Ff")
if ff != nil && primitives.FieldFlags(*ff)&primitives.FieldPushbutton > 0 {
return nil
}

v := types.Name("Off")
if s, found := d.Find("DV"); found {
v = s.(types.Name)
Expand Down
6 changes: 5 additions & 1 deletion pkg/pdfcpu/primitives/font.go
Expand Up @@ -314,7 +314,11 @@ func fontFromAcroDict(xRefTable *model.XRefTable, fName, fLang *string, fontID s
return err
}

*fName, *fLang = *fN, *fL
*fName = *fN

if fL != nil {
*fLang = *fL
}

return err
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/pdfcpu/validate/action.go
Expand Up @@ -566,7 +566,7 @@ func validateNamedActionDict(xRefTable *model.XRefTable, d types.Dict, dictName
}

// Some known non standard named actions
if types.MemberOf(s, []string{"GoToPage", "GoBack", "GoForward", "Find", "Print", "Quit", "FullScreen"}) {
if types.MemberOf(s, []string{"GoToPage", "GoBack", "GoForward", "Find", "Print", "SaveAs", "Quit", "FullScreen"}) {
return true
}

Expand Down
32 changes: 29 additions & 3 deletions pkg/pdfcpu/validate/annotation.go
Expand Up @@ -1357,8 +1357,31 @@ func validateAppearDictEntry(xRefTable *model.XRefTable, d types.Dict, dictName
return err
}

func validateBorderArrayLength(a types.Array) bool {
return len(a) == 0 || len(a) == 3 || len(a) == 4
func validateBorderArray(xRefTable *model.XRefTable, a types.Array) bool {
if len(a) == 0 {
return true
}
if len(a) == 1 || len(a) == 2 || len(a) > 4 {
return false
}
if len(a) == 3 {
_, err := validateNumberArray(xRefTable, a)
return err == nil
}

// len = 4

o := a[3]
a1, ok := o.(types.Array)
if !ok {
return false
}
if len(a1) != 2 {
return false
}

_, err := validateNumberArray(xRefTable, a1)
return err == nil
}

func validateAnnotationDictGeneral(xRefTable *model.XRefTable, d types.Dict, dictName string) (*types.Name, error) {
Expand Down Expand Up @@ -1424,10 +1447,13 @@ func validateAnnotationDictGeneral(xRefTable *model.XRefTable, d types.Dict, dic
}

// Border, optional, array of numbers
_, err = validateNumberArrayEntry(xRefTable, d, dictName, "Border", OPTIONAL, model.V10, validateBorderArrayLength)
a, err := validateArrayEntry(xRefTable, d, dictName, "Border", OPTIONAL, model.V10, nil)
if err != nil {
return nil, err
}
if !validateBorderArray(xRefTable, a) {
return nil, errors.Errorf("invalid border array: %s", a)
}

// C, optional array, of numbers, since V1.1
_, err = validateNumberArrayEntry(xRefTable, d, dictName, "C", OPTIONAL, model.V11, nil)
Expand Down

0 comments on commit fff3853

Please sign in to comment.