unidoc / unioffice

Pure go library for creating and processing Office Word (.docx), Excel (.xlsx) and Powerpoint (.pptx) documents
https://unidoc.io/unioffice/
Other
4.37k stars 473 forks source link

How to get the page size for a Word doc? #500

Closed sagar-kalburgi-ripcord closed 12 months ago

sagar-kalburgi-ripcord commented 1 year ago

Description

demo.docx

I want to be able to get the page size of a word doc using Unidoc.

Expected Behavior

This code is supposed to print the page size of the document.

doc, err := document.OpenTemplate("demo.docx")
if err != nil {
log.Fatalf("error opening document template: %s", err)
}

fmt.Println("Size: ", doc.BodySection().X().PgSz)

Note: Please find the demo.docx attached.

Actual Behavior

The code above returns nil for page size. In fact, it returns a nil for each and every field in the struct https://apidocs.unidoc.io/unioffice/v1.21.0/github.com/unidoc/unioffice/schema/soo/wml/#CT_SectPr

github-actions[bot] commented 1 year ago

Welcome! Thanks for posting your first issue. The way things work here is that while customer issues are prioritized, other issues go into our backlog where they are assessed and fitted into the roadmap when suitable. If you need to get this done, consider buying a license which also enables you to use it in your commercial products. More information can be found on https://unidoc.io/

sampila commented 1 year ago

Hi @sagar-kalburgi-ripcord,

You need to use function document.Open instead the document.OpenTemplate, as the document.OpenTemplate will maintain the styles that being used.

sagar-kalburgi-ripcord commented 1 year ago

Thanks for the clarification! I'll try this out and respond

sagar-kalburgi-ripcord commented 12 months ago

Hi @sampila

Thanks, your suggestion worked. I have a question, so this program works


doc, err := document.OpenTemplate("demo.docx")
if err != nil {
log.Fatalf("error opening document template: %s", err)
}

fmt.Println("Size: ", doc.BodySection().X().PgSz)

And the output it prints is

Size: &{12240 15840 <nil>}

I suppose 12240 is the width of any page of the document in twips and 15840 is the height of any page in the document in twips?

But if I try to print doc.BodySection().X().PgSz.WAttr.ST_UnsignedDecimalNumber it prints a hexadecimal number 0x140002f68f0. How is this value related to 12240?

sampila commented 12 months ago

Hi @sagar-kalburgi-ripcord,

the when you are print the doc.BodySection().X().PgSz.WAttr.ST_UnsignedDecimalNumber and it is showing hexadecimal number, that's actually the address value, as it is pointer, to print the integer value, you can use

if pgSize := doc.BodySection().X().PgSz; pgSize != nil {
    if pgSize.WAttr != nil && pgSize.WAttr.ST_UnsignedDecimalNumber != nil {
        fmt.Println("Size: ", *pgSize.WAttr.ST_UnsignedDecimalNumber)
    }
}
sagar-kalburgi-ripcord commented 12 months ago

Oh I see my bad, makes sense thanks!

sampila commented 12 months ago

You are welcome. Looks like this issue solved, we closing this issue now, you can re-open this issue if you are still having problem with this issue.