Skip to content

Commit

Permalink
AddPageWithOption
Browse files Browse the repository at this point in the history
  • Loading branch information
oneplus1000 committed Jun 18, 2017
1 parent fa5907e commit b2ec6bc
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 7 deletions.
14 changes: 7 additions & 7 deletions content_obj.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ func (c *ContentObj) AppendStreamText(text string) error {
setXCount: setXCount,
x: x,
y: y,
pageheight: c.getRoot().config.PageSize.H,
pageheight: c.getRoot().curr.pageSize.H,
contentType: ContentTypeText,
lineWidth: c.getRoot().curr.lineWidth,
}
Expand Down Expand Up @@ -121,7 +121,7 @@ func (c *ContentObj) AppendStreamSubsetFont(rectangle *Rect, text string, cellOp
setXCount: setXCount,
x: x,
y: y,
pageheight: c.getRoot().config.PageSize.H,
pageheight: c.getRoot().curr.pageSize.H,
contentType: ContentTypeCell,
cellOpt: cellOpt,
lineWidth: c.getRoot().curr.lineWidth,
Expand All @@ -139,7 +139,7 @@ func (c *ContentObj) AppendStreamLine(x1 float64, y1 float64, x2 float64, y2 flo
//h := c.getRoot().config.PageSize.H
//c.stream.WriteString(fmt.Sprintf("%0.2f %0.2f m %0.2f %0.2f l s\n", x1, h-y1, x2, h-y2))
var cache cacheContentLine
cache.pageHeight = c.getRoot().config.PageSize.H
cache.pageHeight = c.getRoot().curr.pageSize.H
cache.x1 = x1
cache.y1 = y1
cache.x2 = x2
Expand All @@ -150,7 +150,7 @@ func (c *ContentObj) AppendStreamLine(x1 float64, y1 float64, x2 float64, y2 flo
//AppendStreamRectangle : draw rectangle from lower-left corner (x, y) with specif width/height
func (c *ContentObj) AppendStreamRectangle(x float64, y float64, wdth float64, hght float64, style string) {
var cache cacheContentRectangle
cache.pageHeight = c.getRoot().config.PageSize.H
cache.pageHeight = c.getRoot().curr.pageSize.H
cache.x = x
cache.y = y
cache.width = wdth
Expand All @@ -162,7 +162,7 @@ func (c *ContentObj) AppendStreamRectangle(x float64, y float64, wdth float64, h
//AppendStreamOval append oval
func (c *ContentObj) AppendStreamOval(x1 float64, y1 float64, x2 float64, y2 float64) {
var cache cacheContentOval
cache.pageHeight = c.getRoot().config.PageSize.H
cache.pageHeight = c.getRoot().curr.pageSize.H
cache.x1 = x1
cache.y1 = y1
cache.x2 = x2
Expand All @@ -181,7 +181,7 @@ func (c *ContentObj) AppendStreamOval(x1 float64, y1 float64, x2 float64, y2 flo
// DF or FD: draw and fill
func (c *ContentObj) AppendStreamCurve(x0 float64, y0 float64, x1 float64, y1 float64, x2 float64, y2 float64, x3 float64, y3 float64, style string) {
var cache cacheContentCurve
cache.pageHeight = c.getRoot().config.PageSize.H
cache.pageHeight = c.getRoot().curr.pageSize.H
cache.x0 = x0
cache.y0 = y0
cache.x1 = x1
Expand Down Expand Up @@ -250,7 +250,7 @@ func (c *ContentObj) AppendStreamSetColorFill(r uint8, g uint8, b uint8) {
//AppendStreamImage append image
func (c *ContentObj) AppendStreamImage(index int, x float64, y float64, rect *Rect) {
//fmt.Printf("index = %d",index)
h := c.getRoot().config.PageSize.H
h := c.getRoot().curr.pageSize.H
c.stream.WriteString(fmt.Sprintf("q %0.2f 0 0 %0.2f %0.2f %0.2f cm /I%d Do Q\n", rect.W, rect.H, x, h-(y+rect.H), index+1))
}

Expand Down
3 changes: 3 additions & 0 deletions current.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ type Current struct {
grayStroke float64

lineWidth float64

//current page size
pageSize Rect
}

func (c *Current) setTextColor(rgb Rgb) {
Expand Down
14 changes: 14 additions & 0 deletions gopdf.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,10 +228,24 @@ func (gp *GoPdf) Image(picPath string, x float64, y float64, rect *Rect) error {

//AddPage : add new page
func (gp *GoPdf) AddPage() {
emptyOpt := PageOption{}
gp.AddPageWithOption(emptyOpt)
}

//AddPageWithOption : add new page with option
func (gp *GoPdf) AddPageWithOption(opt PageOption) {
page := new(PageObj)
page.init(func() *GoPdf {
return gp
})

if !opt.isEmpty() { //use page option
page.setOption(opt)
gp.curr.pageSize = opt.PageSize
} else { //use default
gp.curr.pageSize = gp.config.PageSize
}

page.ResourcesRelate = strconv.Itoa(gp.indexOfProcSet+1) + " 0 R"
index := gp.addObj(page)
if gp.indexOfFirstPageObj == -1 {
Expand Down
11 changes: 11 additions & 0 deletions page_obj.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package gopdf

import (
"bytes"
"fmt"
//"fmt"
)

Expand All @@ -10,12 +11,17 @@ type PageObj struct { //impl IObj
buffer bytes.Buffer
Contents string
ResourcesRelate string
pageOption PageOption
}

func (p *PageObj) init(funcGetRoot func() *GoPdf) {

}

func (p *PageObj) setOption(opt PageOption) {
p.pageOption = opt
}

func (p *PageObj) build(objID int) error {

p.buffer.WriteString("<<\n")
Expand All @@ -33,6 +39,11 @@ func (p *PageObj) build(objID int) error {
me.buffer.WriteString(" >>\n")*/
//me.buffer.WriteString(" >>\n")
p.buffer.WriteString(" /Contents " + p.Contents + "\n") //sample Contents 8 0 R
if !p.pageOption.isEmpty() {
height := fmt.Sprintf("%0.2f", p.pageOption.PageSize.H)
width := fmt.Sprintf("%0.2f", p.pageOption.PageSize.W)
p.buffer.WriteString(" /MediaBox [ 0 0 " + width + " " + height + " ]\n")
}
p.buffer.WriteString(">>\n")
return nil
}
Expand Down
13 changes: 13 additions & 0 deletions page_option.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package gopdf

//PageOption option of page
type PageOption struct {
PageSize Rect
}

func (p PageOption) isEmpty() bool {
if p.PageSize.H == 0 && p.PageSize.W == 0 {
return true
}
return false
}

0 comments on commit b2ec6bc

Please sign in to comment.