如何从左word文档中给出poi表边距 [英] How to give poi table margin from left word document
问题描述
我正在使用POI创建Word文档.我创建了一个表和一个标题.我想在表格上留左边距,所以我使用了以下代码:
I am creating a word document using POI. I have created a table and a header. I want to give left margin to table so I used this code:
CTSectPr getSectPr = doc.getDocument().getBody().getSectPr();
CTPageMar addNewPgMar = getSectPr.addNewPgMar();
addNewPgMar.setLeft(BigInteger.valueOf(200));
addNewPgMar.setRight(BigInteger.valueOf(200));
addNewPgMar.setFooter(BigInteger.valueOf(0));
addNewPgMar.setHeader(BigInteger.valueOf(0));
[![enter image description here][1]][1]
但是此代码还为左侧的标头提供了200个空白.我只想要桌子. 预先感谢.
But this code also give 200 margin to header from left. I Want only for table. Thanks in Advance.
推荐答案
使用显示的代码设置的页边距是整个页面的页边距.页眉和页脚以及页面正文都是页面的一部分.页边距中的其他设置setFooter
和setHeader
是页眉距页面顶部和底部的距离的设置.没有特殊设置可以仅为正文或页眉/页脚设置左距.因此,更改后的左页边距也会影响页眉和页脚.
The page margins, you set using the code shown, are margins for the whole page. Headers and footers also are part of the page as well as the body. The additional settings setFooter
and setHeader
in page margins are settings for distances of the header from top and the footer from bottom of the page. There are no special settings to set left distance only for body or header/footer. So changed left page margins also affect the header and footer.
您所能做的就是为正文中的段落和表格设置其他缩进.
All you could do is set additional indentations for paragraphs and tables in the body.
示例:
import java.io.FileOutputStream;
import org.apache.poi.xwpf.usermodel.*;
import org.apache.poi.wp.usermodel.HeaderFooterType;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTSectPr;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTPageMar;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTblWidth;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STTblWidth;
import java.math.BigInteger;
public class CreateWordHeaderFooter {
public static void main(String[] args) throws Exception {
XWPFDocument document = new XWPFDocument();
// the body content
XWPFParagraph paragraph = document.createParagraph();
// set indentation of the paragraph
paragraph.setIndentationLeft(720); //720 TWentieths of an Inch Point (Twips) = 720/20 = 36 pt = 36/72 = 0.5"
XWPFRun run=paragraph.createRun();
run.setText("The Body:");
paragraph = document.createParagraph();
// set indentation of the paragraph
paragraph.setIndentationLeft(720);
run=paragraph.createRun();
run.setText("Lorem ipsum.... page 1");
// create table
XWPFTable table = document.createTable(3,3);
// set indentation of the table
CTTblWidth tableIndentation = table.getCTTbl().getTblPr().addNewTblInd();
tableIndentation.setW(BigInteger.valueOf(720));
tableIndentation.setType(STTblWidth.DXA);
for (int row = 0; row < 3; row++) {
for (int col = 0; col < 3; col++) {
table.getRow(row).getCell(col).setText("row " + row + ", col " + col);
}
}
paragraph = document.createParagraph();
// set indentation of the paragraph
paragraph.setIndentationLeft(720);
// create header start
XWPFHeader header = document.createHeader(HeaderFooterType.DEFAULT);
paragraph = header.getParagraphArray(0);
if (paragraph == null) paragraph = header.createParagraph();
paragraph.setAlignment(ParagraphAlignment.LEFT);
run = paragraph.createRun();
run.setText("The Header");
// create footer start
XWPFFooter footer = document.createFooter(HeaderFooterType.DEFAULT);
paragraph = footer.getParagraphArray(0);
if (paragraph == null) paragraph = footer.createParagraph();
paragraph.setAlignment(ParagraphAlignment.LEFT);
run = paragraph.createRun();
run.setText("The Footer");
// create page margins
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("CreateWordHeaderFooter.docx");
document.write(out);
out.close();
document.close();
}
}
此代码已使用apache poi 4.1.0
进行了测试,并且需要完整的ooxml-schemas-1.4.jar
,如
This code is tested using apache poi 4.1.0
and needs the the full ooxml-schemas-1.4.jar
as mentioned in FAQ-N10025.
这篇关于如何从左word文档中给出poi表边距的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!