mirror of
https://github.com/fumiama/go-docx.git
synced 2026-06-08 01:00:25 +08:00
inline drawing add Size
This commit is contained in:
@@ -95,3 +95,15 @@ func (p *Paragraph) AddInlineDrawingFrom(file string) (*Run, error) {
|
|||||||
}
|
}
|
||||||
return p.AddInlineDrawing(data)
|
return p.AddInlineDrawing(data)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Size of the inline drawing by EMU
|
||||||
|
func (in *WPInline) Size(w, h int) {
|
||||||
|
if in.Extent != nil {
|
||||||
|
in.Extent.CX = w
|
||||||
|
in.Extent.CY = h
|
||||||
|
}
|
||||||
|
if in.Graphic != nil && in.Graphic.GraphicData != nil && in.Graphic.GraphicData.Pic != nil && in.Graphic.GraphicData.Pic.SpPr != nil {
|
||||||
|
in.Graphic.GraphicData.Pic.SpPr.Xfrm.Ext.CX = w
|
||||||
|
in.Graphic.GraphicData.Pic.SpPr.Xfrm.Ext.CY = h
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,60 +0,0 @@
|
|||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"flag"
|
|
||||||
"fmt"
|
|
||||||
"os"
|
|
||||||
|
|
||||||
"github.com/fumiama/docxlib"
|
|
||||||
)
|
|
||||||
|
|
||||||
var fileLocation *string
|
|
||||||
|
|
||||||
func init() {
|
|
||||||
fileLocation = flag.String("file", "new-file.docx", "file location")
|
|
||||||
flag.Parse()
|
|
||||||
}
|
|
||||||
|
|
||||||
func main() {
|
|
||||||
//Now let's try to read the file
|
|
||||||
readFile, err := os.Open(*fileLocation)
|
|
||||||
if err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
defer readFile.Close()
|
|
||||||
fileinfo, err := readFile.Stat()
|
|
||||||
if err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
size := fileinfo.Size()
|
|
||||||
doc, err := docxlib.Parse(readFile, int64(size))
|
|
||||||
if err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
for _, para := range doc.Document.Body.Paragraphs {
|
|
||||||
fmt.Println("New paragraph")
|
|
||||||
for _, child := range para.Children {
|
|
||||||
if child.Run != nil {
|
|
||||||
if child.Run.Text != nil {
|
|
||||||
fmt.Printf("\tWe've found a new run with the text ->%s\n", child.Run.Text.Text)
|
|
||||||
}
|
|
||||||
if child.Run.Drawing != nil {
|
|
||||||
fmt.Printf("\tWe've found a new run with the drawing ->%d\n", child.Run.Drawing.Inline.DistT) // TODO: replace to refid
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if child.Link != nil {
|
|
||||||
id := child.Link.ID
|
|
||||||
text := child.Link.Run.InstrText
|
|
||||||
link, err := doc.ReferHref(id)
|
|
||||||
if err != nil {
|
|
||||||
fmt.Printf("\tWe found a link with id %s and text %s without target\n", id, text)
|
|
||||||
} else {
|
|
||||||
fmt.Printf("\tWe've found a new hyperlink with ref %s and the text %s\n", link, text)
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
fmt.Print("End of paragraph\n\n")
|
|
||||||
}
|
|
||||||
fmt.Println("End of main")
|
|
||||||
}
|
|
||||||
@@ -36,11 +36,22 @@ func main() {
|
|||||||
para3.AddText("直接粘贴 inline")
|
para3.AddText("直接粘贴 inline")
|
||||||
|
|
||||||
para4 := w.AddParagraph()
|
para4 := w.AddParagraph()
|
||||||
para4.AddInlineDrawingFrom("testdata/fumiama.JPG")
|
r, err := para4.AddInlineDrawingFrom("testdata/fumiama.JPG")
|
||||||
para4.AddInlineDrawingFrom("testdata/fumiama2x.webp")
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
r.Drawing.Inline.Size(r.Drawing.Inline.Extent.CX/2, r.Drawing.Inline.Extent.CY/2)
|
||||||
|
r, err = para4.AddInlineDrawingFrom("testdata/fumiama2x.webp")
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
r.Drawing.Inline.Size(r.Drawing.Inline.Extent.CX/2, r.Drawing.Inline.Extent.CY/2)
|
||||||
|
|
||||||
para5 := w.AddParagraph()
|
para5 := w.AddParagraph()
|
||||||
para5.AddInlineDrawingFrom("testdata/fumiamayoko.png")
|
_, err = para5.AddInlineDrawingFrom("testdata/fumiamayoko.png")
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
f, err := os.Create(*fileLocation)
|
f, err := os.Create(*fileLocation)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -83,7 +94,7 @@ func main() {
|
|||||||
if child.Link != nil {
|
if child.Link != nil {
|
||||||
id := child.Link.ID
|
id := child.Link.ID
|
||||||
text := child.Link.Run.InstrText
|
text := child.Link.Run.InstrText
|
||||||
link, err := doc.ReferHref(id)
|
link, err := doc.ReferTarget(id)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Printf("\tWe found a link with id %s and text %s without target\n", id, text)
|
fmt.Printf("\tWe found a link with id %s and text %s without target\n", id, text)
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
@@ -3,16 +3,10 @@ package docxlib
|
|||||||
import (
|
import (
|
||||||
"archive/zip"
|
"archive/zip"
|
||||||
"bytes"
|
"bytes"
|
||||||
"errors"
|
|
||||||
"io"
|
"io"
|
||||||
"io/fs"
|
"io/fs"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
|
||||||
// ErrRefIDNotFound cannot find such reference
|
|
||||||
ErrRefIDNotFound = errors.New("ref id not found")
|
|
||||||
)
|
|
||||||
|
|
||||||
// Docx is the structure that allow to access the internal represntation
|
// Docx is the structure that allow to access the internal represntation
|
||||||
// in memory of the doc (either read or about to be written)
|
// in memory of the doc (either read or about to be written)
|
||||||
type Docx struct {
|
type Docx struct {
|
||||||
|
|||||||
30
link.go
30
link.go
@@ -1,10 +1,18 @@
|
|||||||
package docxlib
|
package docxlib
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
"strconv"
|
"strconv"
|
||||||
"sync/atomic"
|
"sync/atomic"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
// ErrRefIDNotFound cannot find such reference
|
||||||
|
ErrRefIDNotFound = errors.New("ref id not found")
|
||||||
|
// ErrRefTargetNotFound cannot find such target
|
||||||
|
ErrRefTargetNotFound = errors.New("ref target not found")
|
||||||
|
)
|
||||||
|
|
||||||
// when adding an hyperlink we need to store a reference in the relationship field
|
// when adding an hyperlink we need to store a reference in the relationship field
|
||||||
//
|
//
|
||||||
// this func is not thread-safe
|
// this func is not thread-safe
|
||||||
@@ -36,16 +44,26 @@ func (f *Docx) addImageRelation(m Media) string {
|
|||||||
return rel.ID
|
return rel.ID
|
||||||
}
|
}
|
||||||
|
|
||||||
// ReferHref gets the url for a reference
|
// ReferTarget gets the target for a reference
|
||||||
func (f *Docx) ReferHref(id string) (href string, err error) {
|
func (f *Docx) ReferTarget(id string) (string, error) {
|
||||||
f.DocRelation.mu.RLock()
|
f.DocRelation.mu.RLock()
|
||||||
defer f.DocRelation.mu.RUnlock()
|
defer f.DocRelation.mu.RUnlock()
|
||||||
for _, a := range f.DocRelation.Relationships {
|
for _, a := range f.DocRelation.Relationships {
|
||||||
if a.ID == id {
|
if a.ID == id {
|
||||||
href = a.Target
|
return a.Target, nil
|
||||||
return
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
err = ErrRefIDNotFound
|
return "", ErrRefIDNotFound
|
||||||
return
|
}
|
||||||
|
|
||||||
|
// ReferID gets the rId from target
|
||||||
|
func (f *Docx) ReferID(target string) (string, error) {
|
||||||
|
f.DocRelation.mu.RLock()
|
||||||
|
defer f.DocRelation.mu.RUnlock()
|
||||||
|
for _, a := range f.DocRelation.Relationships {
|
||||||
|
if a.Target == target {
|
||||||
|
return a.ID, nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return "", ErrRefIDNotFound
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -26,7 +26,7 @@ func (p *Paragraph) String() string {
|
|||||||
case c.Link != nil:
|
case c.Link != nil:
|
||||||
id := c.Link.ID
|
id := c.Link.ID
|
||||||
text := c.Link.Run.InstrText
|
text := c.Link.Run.InstrText
|
||||||
link, err := p.file.ReferHref(id)
|
link, err := p.file.ReferTarget(id)
|
||||||
sb.WriteString(text)
|
sb.WriteString(text)
|
||||||
sb.WriteByte('(')
|
sb.WriteByte('(')
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
Reference in New Issue
Block a user