Nothing Special   »   [go: up one dir, main page]

Skip to content

Commit

Permalink
Fix #515
Browse files Browse the repository at this point in the history
  • Loading branch information
hhrutter committed Feb 24, 2023
1 parent d26377a commit 74efd88
Show file tree
Hide file tree
Showing 10 changed files with 438 additions and 216 deletions.
26 changes: 19 additions & 7 deletions cmd/pdfcpu/process.go
Original file line number Diff line number Diff line change
Expand Up @@ -1665,8 +1665,12 @@ func processRemoveAnnotationsCommand(conf *model.Configuration) {
os.Exit(1)
}

inFile := ""
objNrs := []int{}
inFile, outFile := "", ""

var (
idsAndTypes []string
objNrs []int
)

for i, arg := range flag.Args() {
if i == 0 {
Expand All @@ -1676,15 +1680,23 @@ func processRemoveAnnotationsCommand(conf *model.Configuration) {
}
continue
}
i, err := strconv.Atoi(arg)
if i == 1 {
if hasPDFExtension(arg) {
outFile = arg
continue
}
}

j, err := strconv.Atoi(arg)
if err != nil {
fmt.Fprintln(os.Stderr, "objNr has to be a positiv numeric value")
os.Exit(1)
// strings args may be and id or annotType
idsAndTypes = append(idsAndTypes, arg)
continue
}
objNrs = append(objNrs, i)
objNrs = append(objNrs, j)
}

process(cli.RemoveAnnotationsCommand(inFile, "", selectedPages, objNrs, conf))
process(cli.RemoveAnnotationsCommand(inFile, outFile, selectedPages, idsAndTypes, objNrs, conf))
}

func processListImagesCommand(conf *model.Configuration) {
Expand Down
27 changes: 21 additions & 6 deletions cmd/pdfcpu/usage.go
Original file line number Diff line number Diff line change
Expand Up @@ -917,7 +917,7 @@ Examples:
` + usageBoxDescription

usageAnnotsList = "pdfcpu annotations list [-p(ages) selectedPages] inFile"
usageAnnotsRemove = "pdfcpu annotations remove [-p(ages) selectedPages] inFile [objNr...]" + generalFlags
usageAnnotsRemove = "pdfcpu annotations remove [-p(ages) selectedPages] inFile [outFile] [objNr|annotId|annotType]..." + generalFlags

usageAnnots = "usage: " + usageAnnotsList +
"\n " + usageAnnotsRemove
Expand All @@ -926,21 +926,36 @@ Examples:
pages ... Please refer to "pdfcpu selectedpages"
inFile ... input pdf file
objNr ... annotation dict objNr
objNr ... obj# from "pdfcpu annotations list"
annotId ... id from "pdfcpu annotations list"
annotType ... Text, Link, FreeText, Line, Square, Circle, Polygon, PolyLine, HighLight, Underline, Squiggly, StrikeOut, Stamp,
Caret, Ink, Popup, FileAttachment, Sound, Movie, Widget, Screen, PrinterMark, TrapNet, Watermark, 3D, Redact
Examples:
pdfcpu annot list in.pdf
pdfcpu annot list -pages 1-2 in.pdf
List all annotations:
pdfcpu annot list in.pdf
Remove all page annotations:
pdfcpu annot remove in.pdf
List annotation of first two pages:
pdfcpu annot list -pages 1-2 in.pdf
Remove all page annotations and write to out.pdf:
pdfcpu annot remove in.pdf out.pdf
Remove annotations for first 10 pages:
pdfcpu annot remove -pages 1-10 in.pdf
Remove annotations with obj# 37, 38 (see output of pdfcpu annot list)
pdfcpu annot remove in.pdf 37 38
Remove all Widget annotations and write to out.pdf:
pdfcpu annot remove in.pdf out.pdf Widget
Remove all Ink and Widget annotations on page 3:
pdfcpu annot remove -pages 3 in.pdf Ink Widget
Remove annotations by type, id and obj# and write to out.pdf:
pdfcpu annot remove in.pdf out.pdf Link 30 Text someId
`

usageImagesList = "pdfcpu images list [-p(ages) selectedPages] inFile..." + generalFlags
Expand Down
28 changes: 16 additions & 12 deletions pkg/api/annotate.go
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,7 @@ func AddAnnotationsMapFile(inFile, outFile string, m map[int][]model.AnnotationR

// RemoveAnnotations removes annotations for selected pages by id and object number
// from a PDF context read from rs and writes the result to w.
func RemoveAnnotations(rs io.ReadSeeker, w io.Writer, selectedPages, ids []string, objNrs []int, conf *model.Configuration) error {
func RemoveAnnotations(rs io.ReadSeeker, w io.Writer, selectedPages, idsAndTypes []string, objNrs []int, conf *model.Configuration) error {
if conf == nil {
conf = model.NewDefaultConfiguration()
conf.Cmd = model.REMOVEANNOTATIONS
Expand All @@ -355,7 +355,7 @@ func RemoveAnnotations(rs io.ReadSeeker, w io.Writer, selectedPages, ids []strin
return err
}

ok, err := ctx.RemoveAnnotations(pages, ids, objNrs, false)
ok, err := ctx.RemoveAnnotations(pages, idsAndTypes, objNrs, false)
if err != nil {
return err
}
Expand All @@ -376,7 +376,7 @@ func RemoveAnnotations(rs io.ReadSeeker, w io.Writer, selectedPages, ids []strin

// RemoveAnnotationsAsIncrement removes annotations for selected pages by ids and object number
// from a PDF context read from rs and writes out a PDF increment.
func RemoveAnnotationsAsIncrement(rws io.ReadWriteSeeker, selectedPages, ids []string, objNrs []int, conf *model.Configuration) error {
func RemoveAnnotationsAsIncrement(rws io.ReadWriteSeeker, selectedPages, idsAndTypes []string, objNrs []int, conf *model.Configuration) error {
if conf == nil {
conf = model.NewDefaultConfiguration()
conf.Cmd = model.REMOVEANNOTATIONS
Expand All @@ -400,7 +400,7 @@ func RemoveAnnotationsAsIncrement(rws io.ReadWriteSeeker, selectedPages, ids []s
return err
}

ok, err := ctx.RemoveAnnotations(pages, ids, objNrs, true)
ok, err := ctx.RemoveAnnotations(pages, idsAndTypes, objNrs, true)
if err != nil {
return err
}
Expand All @@ -425,7 +425,9 @@ func RemoveAnnotationsAsIncrement(rws io.ReadWriteSeeker, selectedPages, ids []s

// RemoveAnnotationsFile removes annotations for selected pages by id and object number
// from a PDF context read from inFile and writes the result to outFile.
func RemoveAnnotationsFile(inFile, outFile string, selectedPages, ids []string, objNrs []int, conf *model.Configuration, incr bool) (err error) {
func RemoveAnnotationsFile(inFile, outFile string, selectedPages, idsAndTypes []string, objNrs []int, conf *model.Configuration, incr bool) (err error) {

var f1, f2 *os.File

tmpFile := inFile + ".tmp"
if outFile != "" && inFile != outFile {
Expand All @@ -434,17 +436,19 @@ func RemoveAnnotationsFile(inFile, outFile string, selectedPages, ids []string,
} else {
log.CLI.Printf("writing %s...\n", inFile)
if incr {
f, err := os.OpenFile(inFile, os.O_RDWR, 0644)
if err != nil {
if f1, err = os.OpenFile(inFile, os.O_RDWR, 0644); err != nil {
return err
}
defer f.Close()
return RemoveAnnotationsAsIncrement(f, selectedPages, ids, objNrs, conf)
defer func() {
cerr := f1.Close()
if err == nil {
err = cerr
}
}()
return RemoveAnnotationsAsIncrement(f1, selectedPages, idsAndTypes, objNrs, conf)
}
}

var f1, f2 *os.File

if f1, err = os.Open(inFile); err != nil {
return err
}
Expand Down Expand Up @@ -474,5 +478,5 @@ func RemoveAnnotationsFile(inFile, outFile string, selectedPages, ids []string,
}
}()

return RemoveAnnotations(f1, f2, selectedPages, ids, objNrs, conf)
return RemoveAnnotations(f1, f2, selectedPages, idsAndTypes, objNrs, conf)
}
Loading

0 comments on commit 74efd88

Please sign in to comment.