Skip to content

Commit

Permalink
Merge pull request juruen#267 from ddvk/fix_pageredirect
Browse files Browse the repository at this point in the history
fix for page remapping
  • Loading branch information
ddvk authored Nov 4, 2022
2 parents 4ee76bf + 1abecef commit 27a359f
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 9 deletions.
9 changes: 6 additions & 3 deletions annotations/pdf.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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
Expand Down
8 changes: 6 additions & 2 deletions archive/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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"`
}
Expand Down
24 changes: 20 additions & 4 deletions archive/reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 27a359f

Please sign in to comment.