Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

增加页面宽度高度、字体设置,增加序号等 #44

Closed
wants to merge 17 commits into from
25 changes: 21 additions & 4 deletions apirun.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,22 @@ func (r *Run) Size(size string) *Run {
return r
}

// SizeCs allows to set run size
func (r *Run) SizeCs(size string) *Run {
r.RunProperties.SizeCs = &SizeCs{
Val: size,
}
return r
}

// Spacing allows to set run spacing
func (r *Run) Spacing(line int) *Run {
r.RunProperties.Spacing = &Spacing{
Line: line,
}
return r
}

// Shade allows to set run shade
func (r *Run) Shade(val, color, fill string) *Run {
r.RunProperties.Shade = &Shade{
Expand Down Expand Up @@ -100,11 +116,12 @@ func (r *Run) AddTab() *Run {
}

// Font sets the font of the run
func (r *Run) Font(ascii, hansi, hint string) *Run {
func (r *Run) Font(ascii, eastAsia, hansi, hint string) *Run {
r.RunProperties.Fonts = &RunFonts{
ASCII: ascii,
HAnsi: hansi,
Hint: hint,
ASCII: ascii,
EastAsia: eastAsia,
HAnsi: hansi,
Hint: hint,
}
return r
}
4 changes: 2 additions & 2 deletions cmd/main/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
"strconv"
"strings"

"github.com/fumiama/go-docx"

Check failure on line 33 in cmd/main/main.go

View workflow job for this annotation

GitHub Actions / lint

could not import github.com/fumiama/go-docx (-: # github.com/fumiama/go-docx
)

func main() {
Expand All @@ -42,11 +42,11 @@
droppp := flag.Bool("p", false, "drop all paragraph properties")
dupnum := flag.Uint("d", 0, "copy times of the file into dup_filename")
flag.Parse()
var w *docx.Docx

Check failure on line 45 in cmd/main/main.go

View workflow job for this annotation

GitHub Actions / lint

undefined: docx (typecheck)
if !*analyzeOnly {
fmt.Printf("Preparing new document to write at %s\n", *fileLocation)

w = docx.New().WithDefaultTheme().WithA4Page()

Check failure on line 49 in cmd/main/main.go

View workflow job for this annotation

GitHub Actions / lint

undefined: docx (typecheck)
// add new paragraph
para1 := w.AddParagraph().Justification("distribute")
r, err := para1.AddAnchorDrawingFrom("testdata/fumiama.JPG")
Expand All @@ -56,7 +56,7 @@
r.Children[0].(*docx.Drawing).Anchor.Size(r.Children[0].(*docx.Drawing).Anchor.Extent.CX/4, r.Children[0].(*docx.Drawing).Anchor.Extent.CY/4)
r.Children[0].(*docx.Drawing).Anchor.BehindDoc = 1
r.Children[0].(*docx.Drawing).Anchor.PositionH.PosOffset = r.Children[0].(*docx.Drawing).Anchor.Extent.CX
r.Children[0].(*docx.Drawing).Anchor.Graphic.GraphicData.Pic.BlipFill.Blip.AlphaModFix = &docx.AAlphaModFix{Amount: 50000}

Check failure on line 59 in cmd/main/main.go

View workflow job for this annotation

GitHub Actions / lint

undefined: docx (typecheck)
// add text
para1.AddText("test").AddTab()
para1.AddText("size").Size("44").AddTab()
Expand All @@ -66,11 +66,11 @@
para1.AddText("italic").Italic().AddTab()
para1.AddText("underline").Underline("double").AddTab()
para1.AddText("highlight").Highlight("yellow").AddTab()
para1.AddText("font").Font("Consolas", "", "cs").AddTab()
para1.AddText("font").Font("Consolas", "", "", "cs").AddTab()

para2 := w.AddParagraph().Justification("end")
para2.AddText("test all font attrs").
Size("44").Color("ff0000").Font("Consolas", "", "cs").
Size("44").Color("ff0000").Font("Consolas", "", "", "cs").
Shade("clear", "auto", "E7E6E6").
Bold().Italic().Underline("wave").
Highlight("yellow")
Expand Down
9 changes: 9 additions & 0 deletions structpara.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

import (
"encoding/xml"
"fmt"

Check failure on line 25 in structpara.go

View workflow job for this annotation

GitHub Actions / lint

"fmt" imported and not used (typecheck)

Check failure on line 25 in structpara.go

View workflow job for this annotation

GitHub Actions / lint

"fmt" imported and not used) (typecheck)
"io"
"reflect"
"strings"
Expand All @@ -32,6 +33,7 @@
XMLName xml.Name `xml:"w:pPr,omitempty"`
Tabs *Tabs
Spacing *Spacing
NumProperties *NumProperties
Ind *Ind
Justification *Justification
Shade *Shade
Expand Down Expand Up @@ -108,6 +110,13 @@
p.RunProperties = &value
case "pStyle":
p.Style = &Style{Val: getAtt(tt.Attr, "val")}
case "numPr":
var value NumProperties
err = d.DecodeElement(&value, &tt)
if err != nil && !strings.HasPrefix(err.Error(), "expected") {
return err
}
p.NumProperties = &value
case "textAlignment":
p.TextAlignment = &TextAlignment{Val: getAtt(tt.Attr, "val")}
case "adjustRightInd":
Expand Down
49 changes: 49 additions & 0 deletions structrun.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,55 @@ type RunProperties struct {
Strike *Strike
}

type NumProperties struct {
XMLName xml.Name `xml:"w:numPr,omitempty"`
NumId *NumId
Ilvl *Ilevel
}

type NumId struct {
XMLName xml.Name `xml:"w:numId,omitempty"`
Val string `xml:"w:val,attr"`
}

type Ilevel struct {
XMLName xml.Name `xml:"w:ilvl,omitempty"`
Val string `xml:"w:val,attr"`
}

func (n *NumProperties) UnmarshalXML(d *xml.Decoder, _ xml.StartElement) error {
for {
t, err := d.Token()
if err == io.EOF {
break
}
if err != nil {
return err
}

if tt, ok := t.(xml.StartElement); ok {
switch tt.Name.Local {
case "numId":
var value NumId
value.Val = getAtt(tt.Attr, "val")
n.NumId = &value
case "ilvl":
var value Ilevel
value.Val = getAtt(tt.Attr, "val")
n.Ilvl = &value
default:
err = d.Skip() // skip unsupported tags
if err != nil {
return err
}
continue
}
}
}

return nil
}

// UnmarshalXML ...
func (r *RunProperties) UnmarshalXML(d *xml.Decoder, _ xml.StartElement) error {
for {
Expand Down
132 changes: 132 additions & 0 deletions structsect.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ import (
type SectPr struct {
XMLName xml.Name `xml:"w:sectPr,omitempty"` // properties of the document, including paper size
PgSz *PgSz `xml:"w:pgSz,omitempty"`
PgMar *PgMar `xml:"w:pgMar,omitempty"`
Cols *Cols `xml:"w:cols,omitempty"`
DocGrid *DocGrid `xml:"w:docGrid,omitempty"`
}

// PgSz show the paper size
Expand All @@ -36,6 +39,25 @@ type PgSz struct {
H int `xml:"w:h,attr"` // high of paper
}

type PgMar struct {
Top int `xml:"w:top,attr"`
Left int `xml:"w:left,attr"`
Bottom int `xml:"w:bottom,attr"`
Right int `xml:"w:right,attr"`
Header int `xml:"w:header,attr"`
Footer int `xml:"w:footer,attr"`
Gutter int `xml:"w:gutter,attr"`
}

type Cols struct {
Space int `xml:"w:space,attr"`
}

type DocGrid struct {
Type string `xml:"w:type,attr"`
LinePitch int `xml:"w:linePitch,attr"`
}

// UnmarshalXML ...
func (sect *SectPr) UnmarshalXML(d *xml.Decoder, _ xml.StartElement) error {
for {
Expand All @@ -55,6 +77,27 @@ func (sect *SectPr) UnmarshalXML(d *xml.Decoder, _ xml.StartElement) error {
return err
}
sect.PgSz = &value
case "pgMar":
var value PgMar
err = d.DecodeElement(&value, &tt)
if err != nil && !strings.HasPrefix(err.Error(), "expected") {
return err
}
sect.PgMar = &value
case "cols":
var value Cols
err = d.DecodeElement(&value, &tt)
if err != nil && !strings.HasPrefix(err.Error(), "expected") {
return err
}
sect.Cols = &value
case "docGrid":
var value DocGrid
err = d.DecodeElement(&value, &tt)
if err != nil && !strings.HasPrefix(err.Error(), "expected") {
return err
}
sect.DocGrid = &value
default:
err = d.Skip() // skip unsupported tags
if err != nil {
Expand Down Expand Up @@ -90,3 +133,92 @@ func (pgsz *PgSz) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
_, err = d.Token()
return err
}

func (pgmar *PgMar) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
var err error

for _, attr := range start.Attr {
switch attr.Name.Local {
case "top":
pgmar.Top, err = strconv.Atoi(attr.Value)
if err != nil {
return err
}
case "left":
pgmar.Left, err = strconv.Atoi(attr.Value)
if err != nil {
return err
}
case "bottom":
pgmar.Bottom, err = strconv.Atoi(attr.Value)
if err != nil {
return err
}
case "right":
pgmar.Right, err = strconv.Atoi(attr.Value)
if err != nil {
return err
}
case "header":
pgmar.Header, err = strconv.Atoi(attr.Value)
if err != nil {
return err
}
case "footer":
pgmar.Footer, err = strconv.Atoi(attr.Value)
if err != nil {
return err
}
case "gutter":
pgmar.Gutter, err = strconv.Atoi(attr.Value)
if err != nil {
return err
}
default:
// ignore other attributes now
}
}
// Consume the end element
_, err = d.Token()
return err
}

func (cols *Cols) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
var err error

for _, attr := range start.Attr {
switch attr.Name.Local {
case "space":
cols.Space, err = strconv.Atoi(attr.Value)
if err != nil {
return err
}
default:
// ignore other attributes now
}
}
// Consume the end element
_, err = d.Token()
return err
}

func (dg *DocGrid) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
var err error

for _, attr := range start.Attr {
switch attr.Name.Local {
case "linePitch":
dg.LinePitch, err = strconv.Atoi(attr.Value)
if err != nil {
return err
}
case "type":
dg.Type = attr.Value
default:
// ignore other attributes now
}
}
// Consume the end element
_, err = d.Token()
return err
}
Loading