Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PropertiesAdd adn PropertiesRemove does not handle missing info dict #489

Closed
suwro opened this issue Jun 23, 2022 · 4 comments
Closed

PropertiesAdd adn PropertiesRemove does not handle missing info dict #489

suwro opened this issue Jun 23, 2022 · 4 comments
Assignees

Comments

@suwro
Copy link

suwro commented Jun 23, 2022

Hi,
pdfcpu v0.3.13
I have some scanned pdf on which PropertiesList.Info is nil. I can't add title or property it trows error

goroutine 1 [running]: github.com/pdfcpu/pdfcpu/pkg/pdfcpu.PropertiesAdd(0xc000148b60, 0xc000141e30?)

in properties.go line 44 and 61 there's a TODO handle missing empty dict
can you please add this feature

thank you

here's my code - is working if Info is not nil

date, err := ioutil.ReadFile(inPdf)
ckErr(err)

ctx, err := api.ReadContext(io.ReadSeeker(bytes.NewReader(date)), pdfcpu.NewDefaultConfiguration())
ckErr(err)

pdfcpu.PropertiesRemove(ctx.XRefTable, []string{"Title", "Autor"})
properties := map[string]string{"Title": "modified ok"}
pdfcpu.PropertiesAdd(ctx.XRefTable, properties)

var b bytes.Buffer
w := bufio.NewWriter(&b)

err = api.WriteContext(ctx, w)
ckErr(err)

err = ioutil.WriteFile("prop_mod.pdf", b.Bytes(), 0644)
ckErr(err)
@hhrutter
Copy link
Collaborator

Thanks!

I'll check it out.

@suwro
Copy link
Author

suwro commented Jul 2, 2022

Hello, any news about this?
can't we just link an empty Dict and return it ?

@suwro
Copy link
Author

suwro commented Jul 4, 2022

Done, i've slove it by adding new IndirectRef before propertiesAdd and eliminated PropertiesRemove

date, err := ioutil.ReadFile(inPdf)
ckErr(err)

ctx, err := api.ReadContext(io.ReadSeeker(bytes.NewReader(date)), pdfcpu.NewDefaultConfiguration())
ckErr(err)

// ADD NEW EMPTY INFO IF NIL
if ctx.XRefTable.Info == nil {
    ctx.XRefTable.Info = pdfcpu.NewIndirectRef(1, 0)
}

properties := map[string]string{"Title": "modified ok"}
pdfcpu.PropertiesAdd(ctx.XRefTable, properties)

var b bytes.Buffer
w := bufio.NewWriter(&b)

err = api.WriteContext(ctx, w)
ckErr(err)

err = ioutil.WriteFile("hello.pdf", b.Bytes(), 0644)
ckErr(err)

can you please modify properties.go line 44 (PropertiesAdd) with this:

if xRefTable.Info == nil {
    xRefTable.Info = NewIndirectRef(1, 0)
}

If PropertiesRemove is nil just return - do nothing since there's nothing to remove (properties.go line 61)

@hhrutter
Copy link
Collaborator

hhrutter commented Jul 9, 2022

This is fixed with the latest commit.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants