diff --git a/pdf/creator/styled_paragraph.go b/pdf/creator/styled_paragraph.go index 04c26e9b..045348f2 100644 --- a/pdf/creator/styled_paragraph.go +++ b/pdf/creator/styled_paragraph.go @@ -327,8 +327,15 @@ func (p *StyledParagraph) wrapText() error { return nil } - annotation := *src - return &annotation + var annotation *model.PdfAnnotation + switch t := src.GetContext().(type) { + case *model.PdfAnnotationLink: + if annot := copyLinkAnnotation(t); annot != nil { + annotation = annot.PdfAnnotation + } + } + + return annotation } for _, chunk := range p.chunks { @@ -698,8 +705,7 @@ func drawStyledParagraphOnBlock(blk *Block, p *StyledParagraph, ctx DrawContext) // Process annotation. if !chunk.annotationProcessed { - annotCtx := chunk.annotation.GetContext() - switch t := annotCtx.(type) { + switch t := chunk.annotation.GetContext().(type) { case *model.PdfAnnotationLink: // Initialize annotation rectangle. annotRect = core.MakeArray() diff --git a/pdf/creator/text_chunk.go b/pdf/creator/text_chunk.go index 4a914f00..87da287a 100644 --- a/pdf/creator/text_chunk.go +++ b/pdf/creator/text_chunk.go @@ -76,3 +76,20 @@ func newInternalLinkAnnotation(page int64, x, y, zoom float64) *model.PdfAnnotat return annotation.PdfAnnotation } + +// copyLinkAnnotation returns a new link annotation based on an existing one. +func copyLinkAnnotation(link *model.PdfAnnotationLink) *model.PdfAnnotationLink { + if link == nil { + return nil + } + + annotation := model.NewPdfAnnotationLink() + annotation.BS = link.BS + annotation.A = link.A + + if annotDest, ok := link.Dest.(*core.PdfObjectArray); ok { + annotation.Dest = core.MakeArray(annotDest.Elements()...) + } + + return annotation +} diff --git a/pdf/creator/toc_line.go b/pdf/creator/toc_line.go index 54f7c9ad..ba246d7c 100644 --- a/pdf/creator/toc_line.go +++ b/pdf/creator/toc_line.go @@ -158,8 +158,8 @@ func (tl *TOCLine) SetLink(page int64, x, y float64) { tl.SetStyle(tl.sp.defaultLinkStyle) } -// getLink returns a new annotation if the line has a link set. -func (tl *TOCLine) getLink() *model.PdfAnnotation { +// getLineLink returns a new annotation if the line has a link set. +func (tl *TOCLine) getLineLink() *model.PdfAnnotation { if tl.linkPage <= 0 { return nil } @@ -186,17 +186,17 @@ func (tl *TOCLine) prepareParagraph(sp *StyledParagraph, ctx DrawContext) { { Text: tl.Number.Text, Style: tl.Number.Style, - annotation: tl.getLink(), + annotation: tl.getLineLink(), }, { Text: title, Style: tl.Title.Style, - annotation: tl.getLink(), + annotation: tl.getLineLink(), }, { Text: page, Style: tl.Page.Style, - annotation: tl.getLink(), + annotation: tl.getLineLink(), }, } @@ -216,7 +216,7 @@ func (tl *TOCLine) prepareParagraph(sp *StyledParagraph, ctx DrawContext) { chunk := sp.Insert(2, sepText) chunk.Style = sepStyle - chunk.annotation = tl.getLink() + chunk.annotation = tl.getLineLink() // Push page numbers to the end of the line. availWidth = availWidth - float64(sepCount)*sepWidth @@ -230,7 +230,7 @@ func (tl *TOCLine) prepareParagraph(sp *StyledParagraph, ctx DrawContext) { chunk = sp.Insert(2, strings.Repeat(" ", spaces)) chunk.Style = style - chunk.annotation = tl.getLink() + chunk.annotation = tl.getLineLink() } } }