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

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

Closed
sagar-kalburgi-ripcord opened this issue Oct 31, 2023 · 7 comments
Closed

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

sagar-kalburgi-ripcord opened this issue Oct 31, 2023 · 7 comments

Comments

@sagar-kalburgi-ripcord
Copy link

sagar-kalburgi-ripcord commented Oct 31, 2023

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

Copy link

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
Copy link
Collaborator

sampila commented Nov 3, 2023

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
Copy link
Author

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

@sagar-kalburgi-ripcord
Copy link
Author

sagar-kalburgi-ripcord commented Nov 9, 2023

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
Copy link
Collaborator

sampila commented Nov 10, 2023

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
Copy link
Author

Oh I see my bad, makes sense thanks!

@sampila
Copy link
Collaborator

sampila commented Nov 10, 2023

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.

@sampila sampila closed this as completed Nov 10, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants