From e520cdad11e3ea3a57ca1e1597ec12f0a5ca7e63 Mon Sep 17 00:00:00 2001 From: ddvk <36803246+ddvk@users.noreply.github.com> Date: Sat, 29 Oct 2022 00:48:06 +0200 Subject: [PATCH 1/2] fix for page remapping --- annotations/pdf.go | 9 ++++++--- archive/file.go | 8 ++++++-- archive/reader.go | 25 ++++++++++++++++++++++--- 3 files changed, 34 insertions(+), 8 deletions(-) diff --git a/annotations/pdf.go b/annotations/pdf.go index f26daf1..be8f29f 100644 --- a/annotations/pdf.go +++ b/annotations/pdf.go @@ -89,15 +89,17 @@ func (p *PdfGenerator) Generate() error { c.SetOutlineTree(outlines) } - for i, pageAnnotations := range zip.Pages { + for _, pageAnnotations := range zip.Pages { hasContent := pageAnnotations.Data != nil // do not add a page when there are no annotations if !p.options.AllPages && !hasContent { continue } + //1 based, redirected page + pageNum := pageAnnotations.DocPage + 1 - page, err := p.addBackgroundPage(c, i+1) + page, err := p.addBackgroundPage(c, pageNum) if err != nil { return err } @@ -220,7 +222,8 @@ func (p *PdfGenerator) initBackgroundPages(pdfArr []byte) error { func (p *PdfGenerator) addBackgroundPage(c *creator.Creator, pageNum int) (*pdf.PdfPage, error) { var page *pdf.PdfPage - if !p.template && !p.options.AnnotationsOnly { + // if page == 0 then empty page + if !p.template && !p.options.AnnotationsOnly && pageNum > 0 { tmpPage, err := p.pdfReader.GetPage(pageNum) if err != nil { return nil, err diff --git a/archive/file.go b/archive/file.go index 664d8ca..08062ea 100644 --- a/archive/file.go +++ b/archive/file.go @@ -73,6 +73,8 @@ type Page struct { Thumbnail []byte // Pagedata contains the name of the selected background template Pagedata string + // page number of the underlying document + DocPage int } // Metadata represents the structure of a .metadata json file associated to a page. @@ -100,8 +102,10 @@ type Content struct { Orientation string `json:"orientation"` PageCount int `json:"pageCount"` // Pages is a list of page IDs - Pages []string `json:"pages"` - TextScale int `json:"textScale"` + Pages []string `json:"pages"` + Tags []string `json:"pageTags"` + RedirectionMap []int `json:"redirectionPageMap"` + TextScale int `json:"textScale"` Transform Transform `json:"transform"` } diff --git a/archive/reader.go b/archive/reader.go index 3049fcc..ea6e500 100644 --- a/archive/reader.go +++ b/archive/reader.go @@ -89,11 +89,30 @@ func (z *Zip) readContent(zr *zip.Reader) error { id, _ := util.DocPathToName(p) z.UUID = id - if z.Content.Pages != nil && len(z.Content.Pages) > 0 { + redirectedCount := len(z.Content.RedirectionMap) + pagesCount := len(z.Content.Pages) + if redirectedCount > 0 { + if redirectedCount != pagesCount { + log.Warning.Print("redirection != pages") + } + z.pageMap = make(map[string]int) + z.Pages = make([]Page, redirectedCount) + for index, docPage := range z.Content.RedirectionMap { + if index > pagesCount { + log.Warning.Print("redirection > pages") + break + } + pageUUID := z.Content.Pages[index] + z.pageMap[pageUUID] = index + z.Pages[index].DocPage = docPage + } + + } else if z.Content.Pages != nil && len(z.Content.Pages) > 0 { z.pageMap = make(map[string]int) z.Pages = make([]Page, len(z.Content.Pages)) - for i, p := range z.Content.Pages { - z.pageMap[p] = i + for index, pageUUID := range z.Content.Pages { + z.pageMap[pageUUID] = index + z.Pages[index].DocPage = index } } else { // instantiate the slice of pages From 1abecef1d9fa612ceefe8b2286560647e8026870 Mon Sep 17 00:00:00 2001 From: ddvk <36803246+ddvk@users.noreply.github.com> Date: Sat, 29 Oct 2022 01:19:46 +0200 Subject: [PATCH 2/2] minor refactor --- archive/reader.go | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/archive/reader.go b/archive/reader.go index ea6e500..871c0b0 100644 --- a/archive/reader.go +++ b/archive/reader.go @@ -92,9 +92,6 @@ func (z *Zip) readContent(zr *zip.Reader) error { redirectedCount := len(z.Content.RedirectionMap) pagesCount := len(z.Content.Pages) if redirectedCount > 0 { - if redirectedCount != pagesCount { - log.Warning.Print("redirection != pages") - } z.pageMap = make(map[string]int) z.Pages = make([]Page, redirectedCount) for index, docPage := range z.Content.RedirectionMap { @@ -107,9 +104,9 @@ func (z *Zip) readContent(zr *zip.Reader) error { z.Pages[index].DocPage = docPage } - } else if z.Content.Pages != nil && len(z.Content.Pages) > 0 { + } else if pagesCount > 0 { z.pageMap = make(map[string]int) - z.Pages = make([]Page, len(z.Content.Pages)) + z.Pages = make([]Page, pagesCount) for index, pageUUID := range z.Content.Pages { z.pageMap[pageUUID] = index z.Pages[index].DocPage = index