【问题标题】:Set word document page margins through java通过java设置word文档页边距
【发布时间】:2014-08-07 08:43:36
【问题描述】:

我使用 Java 创建了一个文件,我想更改页边距但我不能

这是我的代码:

XWPFDocument document = new XWPFDocument();
XWPFParagraph paragraph = document.createParagraph();
XWPFRun run = paragraph.createRun();    

paragraph.setAlignment(ParagraphAlignment.LEFT);
paragraph.setNumID(BigInteger.ONE);

run.setFontSize(18);

run.setText("Test");

    try{
        FileOutputStream output = new FileOutputStream("C://WordDocument.docx");
        document.write(output);

        output.close();

    } catch (Exception e){
        e.printStackTrace();
    }

我想做的是document.setMarginLeft( Left_Margin );document.setMarginRight( Right_Margin );之类的东西

提前致谢

【问题讨论】:

标签: java ms-word apache-poi


【解决方案1】:

我认为他/她的意思是 ooxml-schemas 库和rest dependencies

【讨论】:

  • 我从一开始就包含了以下jar:poi-scratchpad-3.10-WORD-20140208.jar、poi-ooxml-schemas-3.10-Also This-20140208.jar、poi-ooxml-3.10 -Also This-20140208.jar, xmlbeans-2.3.0 - Also This.jar, dom4j-1.6.1 - Also This.jar
【解决方案2】:

您需要获取文档的正文并添加一个Section,然后添加一个CTPageMar,该对象提供了为您刚刚创建的部分设置边距的方法。

这实际上对我有用,

值很大,我想 10000 是页面的总宽度,但我不确定,所以找到你自己想要的值 :)

    XWPFDocument doc = new XWPFDocument();
    CTSectPr sectPr = doc.getDocument().getBody().addNewSectPr();
    CTPageMar pageMar = sectPr.addNewPgMar();
    pageMar.setLeft(BigInteger.valueOf(1500L));
    pageMar.setRight(BigInteger.valueOf(1500L));
    pageMar.setTop(BigInteger.valueOf(2000L));
    pageMar.setBottom(BigInteger.valueOf(1000L));

享受

【讨论】:

  • 你应该为你的回答添加一些解释。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-09-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多