unidoc / unipdf-examples

Examples for creating and processing PDF files with UniPDF https://github.com/unidoc/unipdf
https://unidoc.io
272 stars 101 forks source link

[Question] How do blocks work #67

Closed polderudo closed 4 years ago

polderudo commented 5 years ago

Can we use Block in PDF, in order to place them on a spcific position? Any example would be nice.

gunnsth commented 4 years ago

Can you describe what you are trying to achieve? That would be helpful. A Block is a pretty flexible component, it's basically any PDF content stream content, along with its own resources (images, fonts).

A good description is in the code: https://github.com/unidoc/unipdf/blob/ea9988fb4d074bd7faad901306c0568292bd7e6c/creator/block.go#L20

// Block contains a portion of PDF Page contents. It has a width and a position and can
// be placed anywhere on a Page.  It can even contain a whole Page, and is used in the creator
// where each Drawable object can output one or more blocks, each representing content for separate pages
// (typically needed when Page breaks occur).
type Block struct {

see also in godoc: https://godoc.org/github.com/unidoc/unipdf/creator#Block

The SetPos function can be used to place a Block in absolute coordinates. https://godoc.org/github.com/unidoc/unipdf/creator#Block.SetPos

The following example code shows how to load a PDF page as a block, and then it can be used as a template and placed anywhere in a new pdf file. https://github.com/unidoc/unipdf/blob/ea9988fb4d074bd7faad901306c0568292bd7e6c/creator/creator_test.go#L82

polderudo commented 4 years ago

I seem to have had some problems when i tested this (or maybe a old version?) I retested, and the lib behaves as expected:

        c := creator.New()
    c.SetPageSize(creator.PageSizeA4)
    c.SetPageMargins(10, 10, 10, 10)

    b := creator.NewBlock(200,200)
    b.SetPos(130,130)
    pp:= c.NewParagraph("Test öäü")
    if err := b.Draw(pp); err != nil{
        panic(err)
    }
    if err := c.Draw(b); err != nil{
        panic(err)
    }