Search code examples
javaapache-poixwpf

CTPageMar values unit?


I am using apache poi CTPageMar class to set a page margin to some value given by the user. The problem is that I did not find what is the unit of the values that must be passed in the functions setLeft,setRight,setTop and setBottom. I tried cm, pixels, inches but they all seem wrong. Any idea?

XWPFDocument wordDocument = new XWPFDocument(new FileInputStream(input));
CTSectPr sectPr = wordDocument.getDocument().getBody().addNewSectPr();
CTPageMar pageMar = sectPr.addNewPgMar();
pageMar.setLeft(BigInteger.valueOf(left));
pageMar.setTop(BigInteger.valueOf(top));
pageMar.setRight(BigInteger.valueOf(right));
pageMar.setBottom(BigInteger.valueOf(bottom));
wordDocument.write(new FileOutputStream(output));

Solution

  • The measure unit is Twip (twentieth of an inch point). One twip is 1/1440 inch. So

    ...
      int twipsPerInch =  1440;
      pageMar.setLeft(BigInteger.valueOf(1 * twipsPerInch));
    ...
    

    will be 1 inch left margin.