how to set page margins for word document using apache poi?
Asked Answered
T

2

10

I want to set page-margins for word document created using apache poi-3.9. I found it can be done using CTPageMar but CTPageMar is not being resolved. I am using apache poi-3.9

I tried this

CTSectPr sectPr = document.getDocument().getBody().addNewSectPr();
    CTPageMar pageMar = sectPr.addNewPgMar();
    pageMar.setLeft(BigInteger.valueOf(720L));
    pageMar.setTop(BigInteger.valueOf(1440L));
    pageMar.setRight(BigInteger.valueOf(720L));
    pageMar.setBottom(BigInteger.valueOf(1440L));
Truncation answered 22/4, 2015 at 8:2 Comment(5)
See this thread - apache-poi.1045710.n5.nabble.com/…Plausive
thanks @Plausive the code by mark helped but it is working for doc format not for docx. how can I set the margins while creating xwpf document. (the code from link describes setting margins for existing doc)Truncation
You might be using XWPFDocument nstead of HWPF right?Plausive
yes i am using XWPFDocumentTruncation
What happens if you upgrade to Apache POI 3.12 beta 1?Atwekk
P
7

As far as I understand, the problem is not related to the provided code, it is connected with missing CTPageMar class.

The source of missing classes problem is clearly explained in Apache POI FAQ:

To use the new OOXML file formats, POI requires a jar containing the file format XSDs [...] You can download the jar by hand from the POI Maven Repository.

URL from FAQ is not working, but you can find required jar in central maven repo.

Picul answered 5/5, 2015 at 17:4 Comment(1)
It's important to note that the ooxml-schemas file that contains everything is not the same as the poi-ooxml-schemas file. From the FAQ: "The full jar of all of the schemas is ooxml-schemas-1.3.jar, and it is currently around 15mb. The smaller poi-ooxml-schemas jar is only about 4mb. This latter jar file only contains the typically used parts though."Fishnet
D
4

To resolve the class CTPageMar you will have to add the package org.apache.poi:ooxml-schema to your classpath. The corresponding JAR contains all schemas. You can find more information here.

Disaccustom answered 7/5, 2017 at 16:48 Comment(1)
still not understand...where to download the org.apache.poi:ooxml-schema without using maven?Doom

© 2022 - 2024 — McMap. All rights reserved.