Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/370.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何使用ApachePOI在word文档中定义窄边距?_Java_Jsf_Apache Poi - Fatal编程技术网

Java 如何使用ApachePOI在word文档中定义窄边距?

Java 如何使用ApachePOI在word文档中定义窄边距?,java,jsf,apache-poi,Java,Jsf,Apache Poi,我正在使用ApachePOI创建docx文档,我希望边距设置为窄边,就像在MS Word中使用布局>边距>窄边一样 我已经看到一些其他答案建议RecordInputStream,但我不知道如何将其集成到我的代码中,因为它使用FileInputStream作为参数 我使用ByteArrayOutputStream,因为我正在使用omnifaces导出它,我希望找到一种方法使其工作 这是我的密码: ByteArrayOutputStream output = new ByteArrayOutputS

我正在使用ApachePOI创建docx文档,我希望边距设置为窄边,就像在MS Word中使用布局>边距>窄边一样

我已经看到一些其他答案建议RecordInputStream,但我不知道如何将其集成到我的代码中,因为它使用FileInputStream作为参数

我使用ByteArrayOutputStream,因为我正在使用omnifaces导出它,我希望找到一种方法使其工作

这是我的密码:

ByteArrayOutputStream output = new ByteArrayOutputStream();
XWPFDocument document = new XWPFDocument();
XWPFParagraph titleParagraph = document.createParagraph();
//some code here...
document.write(output);
document.close();    
Faces.sendFile(output.toByteArray(), wordFilename, true);

请帮忙。提前谢谢

到目前为止,还没有设置页边距的方法。因此,我们需要使用低级bean
org.openxmlformats.schemas.wordprocessingml.x2006.main.CTSectPr
org.openxmlformats.schemas.wordprocessingml.x2006.main.CTPageMar

word
所称的窄边距是0.5英寸左右的边距

import java.io.FileOutputStream;

import org.apache.poi.xwpf.usermodel.*;

import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTSectPr;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTPageMar;

import java.math.BigInteger;

public class CreateWordPageMargins {

 public static void main(String[] args) throws Exception {

  XWPFDocument document = new XWPFDocument();

  XWPFParagraph paragraph = document.createParagraph();

  XWPFRun run = paragraph.createRun();  
  run.setText("Text");

  CTSectPr sectPr = document.getDocument().getBody().getSectPr();
  if (sectPr == null) sectPr = document.getDocument().getBody().addNewSectPr();
  CTPageMar pageMar = sectPr.getPgMar();
  if (pageMar == null) pageMar = sectPr.addNewPgMar();
  pageMar.setLeft(BigInteger.valueOf(720)); //720 TWentieths of an Inch Point (Twips) = 720/20 = 36 pt = 36/72 = 0.5"
  pageMar.setRight(BigInteger.valueOf(720));
  pageMar.setTop(BigInteger.valueOf(720));
  pageMar.setBottom(BigInteger.valueOf(720));
  pageMar.setFooter(BigInteger.valueOf(720));
  pageMar.setHeader(BigInteger.valueOf(720));
  pageMar.setGutter(BigInteger.valueOf(0));

  FileOutputStream out = new FileOutputStream("CreateWordPageMargins.docx");
  document.write(out);
  out.close();
  document.close();

 }
}

到目前为止,还没有设置页边距的方法。因此,我们需要使用低级bean
org.openxmlformats.schemas.wordprocessingml.x2006.main.CTSectPr
org.openxmlformats.schemas.wordprocessingml.x2006.main.CTPageMar

word
所称的窄边距是0.5英寸左右的边距

import java.io.FileOutputStream;

import org.apache.poi.xwpf.usermodel.*;

import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTSectPr;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTPageMar;

import java.math.BigInteger;

public class CreateWordPageMargins {

 public static void main(String[] args) throws Exception {

  XWPFDocument document = new XWPFDocument();

  XWPFParagraph paragraph = document.createParagraph();

  XWPFRun run = paragraph.createRun();  
  run.setText("Text");

  CTSectPr sectPr = document.getDocument().getBody().getSectPr();
  if (sectPr == null) sectPr = document.getDocument().getBody().addNewSectPr();
  CTPageMar pageMar = sectPr.getPgMar();
  if (pageMar == null) pageMar = sectPr.addNewPgMar();
  pageMar.setLeft(BigInteger.valueOf(720)); //720 TWentieths of an Inch Point (Twips) = 720/20 = 36 pt = 36/72 = 0.5"
  pageMar.setRight(BigInteger.valueOf(720));
  pageMar.setTop(BigInteger.valueOf(720));
  pageMar.setBottom(BigInteger.valueOf(720));
  pageMar.setFooter(BigInteger.valueOf(720));
  pageMar.setHeader(BigInteger.valueOf(720));
  pageMar.setGutter(BigInteger.valueOf(0));

  FileOutputStream out = new FileOutputStream("CreateWordPageMargins.docx");
  document.write(out);
  out.close();
  document.close();

 }
}

工作起来很有魅力!多谢各位@帕里斯·卡拉吉安诺普洛斯:正如你所看到的,我的答案中的代码是最小的、完整的和可验证的。如果我们想讨论问题,我们总是需要这个,一个。因此,请就您的表格和页边距问题提出一个新问题,并提供最小、完整和可验证的代码来说明该问题。那你肯定会得到答案的。否则就不行了,真是魅力四射!多谢各位@帕里斯·卡拉吉安诺普洛斯:正如你所看到的,我的答案中的代码是最小的、完整的和可验证的。如果我们想讨论问题,我们总是需要这个,一个。因此,请就您的表格和页边距问题提出一个新问题,并提供最小、完整和可验证的代码来说明该问题。那你肯定会得到答案的。不然就不行了。