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..871c0b0 100644 --- a/archive/reader.go +++ b/archive/reader.go @@ -89,11 +89,27 @@ 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 { 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 + 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 pagesCount > 0 { + z.pageMap = make(map[string]int) + z.Pages = make([]Page, pagesCount) + for index, pageUUID := range z.Content.Pages { + z.pageMap[pageUUID] = index + z.Pages[index].DocPage = index } } else { // instantiate the slice of pages