I have a Word table with custom first row, but the following rows aren't adjusted. Please tell me why, and how to do the same widths of cells of the second row?
XWPFDocument document = new XWPFDocument();
CTDocument1 doc = document.getDocument();
CTBody body = doc.getBody();
if (!body.isSetSectPr()) {
body.addNewSectPr();
}
CTSectPr section = body.getSectPr();
if(!section.isSetPgSz()) {
section.addNewPgSz();
}
CTPageSz pageSize = section.getPgSz();
pageSize.setW(BigInteger.valueOf(15840));
pageSize.setH(BigInteger.valueOf(12240));
XWPFParagraph paragraph = document.createParagraph();
CTSectPr ctSectPr = section ;//paragraph.getCTP().addNewPPr().addNewSectPr();
CTColumns ctColumns = ctSectPr.addNewCols();
ctColumns.setNum(BigInteger.valueOf(1));
paragraph.setAlignment(ParagraphAlignment.CENTER);
XWPFRun test = paragraph.createRun();
test.setFontSize(15);
test.setBold(true);
test.setFontFamily("Times New Roman");
test.setText("Перечни документов, необходимые для применения и исполнения технических регламентов (форма № 21)");
test.addBreak();
XWPFTable supergroupTable = document.createTable();
supergroupTable.setWidth(15200);
//supergroupTable.getCTTbl().addNewTblGrid().addNewGridCol().setW(BigInteger.valueOf(1*720));
//supergroupTable.getCTTbl().getTblGrid().addNewGridCol().setW(BigInteger.valueOf(12*720));
CTTblWidth tblWidth = supergroupTable.getRow(0).getCell(0).getCTTc().addNewTcPr().addNewTcW();
tblWidth.setW(BigInteger.valueOf(400));
supergroupTable.getRow(0).getCell(0).setText("№");
//STTblWidth.DXA is used to specify width in twentieths of a point.
tblWidth.setType(STTblWidth.DXA);
XWPFTableRow supegroupRow = supergroupTable.getRow(0);
XWPFTableCell supergroupCell = supegroupRow.createCell();
tblWidth = supergroupTable.getRow(0).getCell(1).getCTTc().addNewTcPr().addNewTcW();
tblWidth.setW(BigInteger.valueOf(1700));
tblWidth.setType(STTblWidth.DXA);
supergroupCell.setText("Обозначение технического регламента");
supergroupCell = supegroupRow.createCell();
tblWidth = supergroupTable.getRow(0).getCell(2).getCTTc().addNewTcPr().addNewTcW();
tblWidth.setW(BigInteger.valueOf(1700));
tblWidth.setType(STTblWidth.DXA);
supergroupCell.setText("Наименование на русском языке ");
supergroupCell = supegroupRow.createCell();
tblWidth = supergroupTable.getRow(0).getCell(3).getCTTc().addNewTcPr().addNewTcW();
tblWidth.setW(BigInteger.valueOf(880));
tblWidth.setType(STTblWidth.DXA);
supergroupCell.setText("Статус");
supergroupCell = supegroupRow.createCell();
tblWidth = supergroupTable.getRow(0).getCell(4).getCTTc().addNewTcPr().addNewTcW();
tblWidth.setW(BigInteger.valueOf(1700));
tblWidth.setType(STTblWidth.DXA);
supergroupCell.setText("Обозначение документа");
supergroupCell = supegroupRow.createCell();
tblWidth = supergroupTable.getRow(0).getCell(5).getCTTc().addNewTcPr().addNewTcW();
tblWidth.setW(BigInteger.valueOf(1700));
tblWidth.setType(STTblWidth.DXA);
supergroupCell.setText("Наименование на русском языке");
supergroupCell = supegroupRow.createCell();
tblWidth = supergroupTable.getRow(0).getCell(6).getCTTc().addNewTcPr().addNewTcW();
tblWidth.setW(BigInteger.valueOf(880));
tblWidth.setType(STTblWidth.DXA);
supergroupCell.setText("Статус");
supegroupRow = supergroupTable.createRow();
supergroupCell = supegroupRow.getCell(0);
supergroupCell.setText("cell 1");
supergroupCell = supegroupRow.getCell(1);
supergroupCell.setText("cell 2");
supergroupCell = supegroupRow.getCell(2);
supergroupCell.setText("cell 3");
supergroupCell = supegroupRow.getCell(3);
supergroupCell.setText("cell 4");
supergroupCell = supegroupRow.getCell(4);
supergroupCell.setText("cell 5");
supergroupCell = supegroupRow.getCell(5);
supergroupCell.setText("cell 6");
supergroupCell = supegroupRow.getCell(6);
supergroupCell.setText("cell 7");
When I trying to set width of the second row in the same manner that it is set up for the first row the document come to mess and I can't do it the same manner.
Current versions of apache poi provide setWidth methods on high level, so the low level CT... classes are not needed. Using the low level CT... classes one needs special knowledge of the exact XML which needs to be created, else one creates wrong XML very fast. This seems to be the case in your case.
Using current apache poi 5.0.0 creating a table as yours is as simple as this:
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.CTPageSz;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STPageOrientation;
import java.math.BigInteger;
public class CreateWordTableCellWidths {
public static void main(String[] args) throws Exception {
XWPFDocument document = new XWPFDocument();
//page settings are not provided high level on apache poi until now.
CTSectPr sectPr = document.getDocument().getBody().addNewSectPr();
CTPageSz pageSz = sectPr.addNewPgSz();
pageSz.setW(BigInteger.valueOf(15840)); //15840 Twips = 15840/20 = 792 pt = 792/72 = 11"
pageSz.setH(BigInteger.valueOf(12240)); //12240 Twips = 12240/20 = 612 pt = 612/72 = 8.5"
pageSz.setOrient(STPageOrientation.LANDSCAPE);
XWPFParagraph paragraph = document.createParagraph();
XWPFRun run = paragraph.createRun();
run.setText("Header line...");
//create table having first row having 7 columns
XWPFTable table = document.createTable(1, 7);
table.setWidth("100%");
//set cell width and content for first row
table.getRow(0).getCell(0).setText("Nr.");
table.getRow(0).getCell(0).setWidth("400");
table.getRow(0).getCell(1).setText("Loremipsumsemitdolor dolorsemitloremipsum semitdolor");
table.getRow(0).getCell(1).setWidth("1700");
table.getRow(0).getCell(2).setText("Lorem ipsum semit dolor dolor semit lorem");
table.getRow(0).getCell(2).setWidth("1700");
table.getRow(0).getCell(3).setText("Lorem ipsum");
table.getRow(0).getCell(3).setWidth("880");
table.getRow(0).getCell(4).setText("Lorem ipsum semit dolor dolor semit lorem");
table.getRow(0).getCell(4).setWidth("1700");
table.getRow(0).getCell(5).setText("Lorem ipsum semit dolor dolor semit lorem");
table.getRow(0).getCell(5).setWidth("1700");
table.getRow(0).getCell(6).setText("Lorem ipsum");
table.getRow(0).getCell(6).setWidth("880");
//create further row
XWPFTableRow row = table.createRow();
row.getCell(0).setText("cell 1");
row.getCell(0).setWidth("400");
row.getCell(1).setText("cell 2");
row.getCell(1).setWidth("1700");
row.getCell(2).setText("cell 3");
row.getCell(2).setWidth("1700");
row.getCell(3).setText("cell 4");
row.getCell(3).setWidth("880");
row.getCell(4).setText("cell 5");
row.getCell(4).setWidth("1700");
row.getCell(5).setText("cell 6");
row.getCell(5).setWidth("1700");
row.getCell(6).setText("cell 7");
row.getCell(6).setWidth("880");
paragraph = document.createParagraph();
FileOutputStream out = new FileOutputStream("CreateWordTableCellWidths.docx");
document.write(out);
out.close();
document.close();
}
}
Only page settings are not provided high level on apache poi until now. So to set page settings using the low level CT... classes is necessary.
For usage in Microsoft Word in general you set cell widths for cells in first row only. Further rows are using those cell widths too except if there are merged cells used.
For usage in Libreoffice or OpenOffice Writer width of all cells in all rows needs to be set.
Related
I need to have a dynamic text field to control by code the size of the text and the text.
I create a dynamic text field on the Stage with the "notification" instance and the following code:
function Message () {
var text1 = new createjs.Text ("Message", "15px Arial", "# ff7700");
text.textBaseline = "alphabetic";
stage.addChild (text);
}
this.notification.text = text1;
What am I doing wrong? From already thank you very much
half resolved
var text = new createjs.Text("Mensaje", "25px Times Bold", "#ff0000");
text.x = 150;
text.y = 150;
text.color = "#0000ff";
text.textAlign = "center";
text.textBaseline = "alphabetic";
stage.addChild(text);
I was trying to find how to add error bars to the scatter plot chart using Apache POI as shown in the picture below. My research could not help me find the corresponding API. Can you please help to find the API.
Adding error bars is not supported by the high level apache poi classes until now. It would must be a method in XDDFChartData.Series.
But *.xlsx files are simply ZIP archives. So we can set the error bars using Excel's GUI. Then unzip the *.xlsx ZIP archive and have a look at /xl/charts/chart1.xml to find what has changed.
We will find something like:
...
<c:ser>
...
<c:errBars>
<c:errDir val="y"/>
<c:errBarType val="both"/>
<c:errValType val="percentage"/>
<c:val val="10.0"/>
</c:errBars>
...
</c:ser>
...
Now we can create that XML using the low level classes of ooxml-schemas:
...
// add error bars
chart.getCTChart().getPlotArea().getScatterChartArray(0).getSerArray(0).addNewErrBars();
// set error bars direction only Y
chart.getCTChart().getPlotArea().getScatterChartArray(0).getSerArray(0).getErrBarsArray(0).addNewErrDir().setVal(
org.openxmlformats.schemas.drawingml.x2006.chart.STErrDir.Y);
// set error bars type to both (minus and plus)
chart.getCTChart().getPlotArea().getScatterChartArray(0).getSerArray(0).getErrBarsArray(0).addNewErrBarType().setVal(
org.openxmlformats.schemas.drawingml.x2006.chart.STErrBarType.BOTH);
// set error bars value type to percentage
chart.getCTChart().getPlotArea().getScatterChartArray(0).getSerArray(0).getErrBarsArray(0).addNewErrValType().setVal(
org.openxmlformats.schemas.drawingml.x2006.chart.STErrValType.PERCENTAGE);
// set error bars value to 10%
chart.getCTChart().getPlotArea().getScatterChartArray(0).getSerArray(0).getErrBarsArray(0).addNewVal().setVal(10d);
...
Complete example:
import java.io.*;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.*;
import org.apache.poi.ss.util.*;
import org.apache.poi.xddf.usermodel.*;
import org.apache.poi.xddf.usermodel.chart.*;
public class ScatterChart {
public static void main(String[] args) throws IOException {
Double[][] data = new Double[][]{
new Double[]{0d, 1d, 2d, 3d, 4d, 5d, 6d, 7d, 8d, 9d},
new Double[]{1.1d, 1.5d, 1.2d, 2.5d, 2.7d, 6.5d, 6.5d, 1.0d, 1.0d, 0.5d},
};
try (XSSFWorkbook wb = new XSSFWorkbook()) {
XSSFSheet sheet = wb.createSheet();
final int NUM_OF_ROWS = data.length;
final int NUM_OF_COLUMNS = data[0].length;
Row row;
Cell cell;
int rowIndex = 0;
int colIndex = 0;
for (Double[] dataRow : data) {
row = sheet.createRow(rowIndex++);
colIndex = 0;
for (Double value : dataRow) {
cell = row.createCell(colIndex++);
cell.setCellValue(value);
}
}
XSSFDrawing drawing = sheet.createDrawingPatriarch();
XSSFClientAnchor anchor = drawing.createAnchor(0, 0, 0, 0, 0, 3, 10, 23);
XSSFChart chart = drawing.createChart(anchor);
XDDFChartLegend legend = chart.getOrAddLegend();
legend.setPosition(LegendPosition.TOP_RIGHT);
XDDFSolidFillProperties gridLinesFill = new XDDFSolidFillProperties(XDDFColor.from(
new byte[]{(byte)230,(byte)230,(byte)230}));
XDDFLineProperties gridLineProperties = new XDDFLineProperties();
gridLineProperties.setFillProperties(gridLinesFill);
XDDFValueAxis bottomAxis = chart.createValueAxis(AxisPosition.BOTTOM);
bottomAxis.setTitle("x");
XDDFShapeProperties shapeProperties = bottomAxis.getOrAddMajorGridProperties();
shapeProperties.setLineProperties(gridLineProperties);
XDDFValueAxis leftAxis = chart.createValueAxis(AxisPosition.LEFT);
leftAxis.setTitle("f(x)");
leftAxis.setCrosses(AxisCrosses.AUTO_ZERO);
shapeProperties = leftAxis.getOrAddMajorGridProperties();
shapeProperties.setLineProperties(gridLineProperties);
XDDFDataSource<Double> xs = XDDFDataSourcesFactory.fromNumericCellRange(
sheet,
new CellRangeAddress(0, 0, 0, NUM_OF_COLUMNS - 1)
);
XDDFNumericalDataSource<Double> ys = XDDFDataSourcesFactory.fromNumericCellRange(
sheet,
new CellRangeAddress(1, 1, 0, NUM_OF_COLUMNS - 1)
);
XDDFScatterChartData chartData = (XDDFScatterChartData) chart.createData(ChartTypes.SCATTER, bottomAxis, leftAxis);
chartData.setVaryColors(false);
XDDFScatterChartData.Series series = (XDDFScatterChartData.Series) chartData.addSeries(xs, ys);
series.setTitle("Series 1", null);
series.setSmooth(false);
chart.plot(chartData);
solidLineSeries(series, PresetColor.BLUE);
// add error bars
chart.getCTChart().getPlotArea().getScatterChartArray(0).getSerArray(0).addNewErrBars();
// set error bars direction only Y
chart.getCTChart().getPlotArea().getScatterChartArray(0).getSerArray(0).getErrBarsArray(0).addNewErrDir().setVal(
org.openxmlformats.schemas.drawingml.x2006.chart.STErrDir.Y);
// set error bars type to both (minus and plus)
chart.getCTChart().getPlotArea().getScatterChartArray(0).getSerArray(0).getErrBarsArray(0).addNewErrBarType().setVal(
org.openxmlformats.schemas.drawingml.x2006.chart.STErrBarType.BOTH);
// set error bars not have no end caps - necessary for current Excel versions
chart.getCTChart().getPlotArea().getScatterChartArray(0).getSerArray(0).getErrBarsArray(0).addNewNoEndCap().setVal(false);
// set error bars value type to percentage
chart.getCTChart().getPlotArea().getScatterChartArray(0).getSerArray(0).getErrBarsArray(0).addNewErrValType().setVal(
org.openxmlformats.schemas.drawingml.x2006.chart.STErrValType.PERCENTAGE);
// set error bars value to 10%
chart.getCTChart().getPlotArea().getScatterChartArray(0).getSerArray(0).getErrBarsArray(0).addNewVal().setVal(10d);
// Write the output to a file
try (FileOutputStream fileOut = new FileOutputStream("ooxml-scatter-chart.xlsx")) {
wb.write(fileOut);
}
}
}
private static void solidLineSeries(XDDFChartData.Series series, PresetColor color) {
XDDFSolidFillProperties fill = new XDDFSolidFillProperties(XDDFColor.from(color));
XDDFLineProperties line = new XDDFLineProperties();
line.setFillProperties(fill);
XDDFShapeProperties properties = series.getShapeProperties();
if (properties == null) {
properties = new XDDFShapeProperties();
}
properties.setLineProperties(line);
series.setShapeProperties(properties);
}
}
I am constructing a row with 8 columns and
Need to create exponential data(number) in single cell in apache poi WORD without using paragraph.break .
If the content (2) shall be superscript, then there are two possibilities using Microsoft Word. Either really superscript align or set the text position away from the text base line. For both the (2) must be in it's own text run.
Superscript alignment can be achieved using XWPFRun.setSubscript having VerticalAlign.SUPERSCRIPT.
Text position can be set using XWPFRun.setTextPosition where int val is of measurement unit half pt.
Example:
import java.io.FileOutputStream;
import org.apache.poi.xwpf.usermodel.*;
public class CreateWordSuperScript {
public static void main(String[] args) throws Exception {
XWPFDocument document= new XWPFDocument();
XWPFParagraph paragraph;
XWPFRun run;
paragraph = document.createParagraph();
run = paragraph.createRun();
run.setText("6 ");
run=paragraph.createRun();
run.setText("(2)");
run.setSubscript(VerticalAlign.SUPERSCRIPT); // superscript (2)
paragraph = document.createParagraph();
run=paragraph.createRun();
run.setText("6 ");
run=paragraph.createRun();
run.setText("(2)");
run.setTextPosition(11); // (2) position = baseline + 11 half pt ~ 5.5 pt
FileOutputStream out = new FileOutputStream("word.docx");
document.write(out);
out.close();
document.close();
}
}
I am creating a poi word document . I have setup page margin 0 but their is extra space between bottom and footer image i want to remove this space. I have used this code which did not work
addNewPgMar.setLeft(BigInteger.valueOf(0));
addNewPgMar.setRight(BigInteger.valueOf(210));
addNewPgMar.setGutter(BigInteger.valueOf(0));
addNewPgMar.setFooter(BigInteger.valueOf(0));
addNewPgMar.setHeader(BigInteger.valueOf(0));
I want to remove this footer below space which is showing in image.
Your problem has nothing to do with the page margins but with the paragraph settings in the footer. A Word paragraph has settings for spacing after each paragraph as well as for spacing between the lines in the paragraph. If the picture in your footer is inline in a paragraph in the footer, then the spacing after the paragraph must be 0 and the spacing between must be 1 to avoid the spacing you are facing.
Using apache poi 4.1.0 this can be set using:
...
XWPFParagraph paragraph...
...
paragraph.setSpacingAfter(0);
paragraph.setSpacingBetween(1d, LineSpacingRule.AUTO);
...
Complete example:
import java.io.FileOutputStream;
import java.io.FileInputStream;
import org.apache.poi.xwpf.usermodel.*;
import org.apache.poi.wp.usermodel.HeaderFooterType;
import org.apache.poi.util.Units;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTSectPr;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTPageSz;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTPageMar;
import java.math.BigInteger;
public class CreateWordHeaderFooterNullMargin {
public static void main(String[] args) throws Exception {
XWPFDocument document = new XWPFDocument();
// the body content
XWPFParagraph paragraph = document.createParagraph();
XWPFRun run=paragraph.createRun();
run.setText("The Body");
// create header start
XWPFHeader header = document.createHeader(HeaderFooterType.DEFAULT);
paragraph = header.getParagraphArray(0);
if (paragraph == null) paragraph = header.createParagraph();
paragraph.setSpacingAfter(0);
paragraph.setSpacingBetween(1d, LineSpacingRule.AUTO);
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.CENTER);
paragraph.setSpacingAfter(0);
paragraph.setSpacingBetween(1d, LineSpacingRule.AUTO);
run = paragraph.createRun();
String imgFile="Chrysanthemum.jpg";
run.addPicture(new FileInputStream(imgFile), XWPFDocument.PICTURE_TYPE_JPEG, imgFile, Units.toEMU(500), Units.toEMU(25));
// create page margins
CTSectPr sectPr = document.getDocument().getBody().getSectPr();
if (sectPr == null) sectPr = document.getDocument().getBody().addNewSectPr();
CTPageSz pageSz = sectPr.addNewPgSz(); // paper format letter
pageSz.setW(BigInteger.valueOf(12240)); //12240 Twips = 12240/20 = 612 pt = 612/72 = 8.5"
pageSz.setH(BigInteger.valueOf(15840)); //15840 Twips = 15840/20 = 792 pt = 792/72 = 11"
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(0));
pageMar.setBottom(BigInteger.valueOf(0));
pageMar.setFooter(BigInteger.valueOf(0));
pageMar.setHeader(BigInteger.valueOf(0));
pageMar.setGutter(BigInteger.valueOf(0));
FileOutputStream out = new FileOutputStream("CreateWordHeaderFooterNullMargin.docx");
document.write(out);
out.close();
document.close();
}
}
Disclaimer: In my opinion page settings where margins are 0 are not recommendable. Most printers are not able printing the full paper's size. There are printable areas with minimal spaces on left, right, top and/or bottom of the paper. If you set page margins 0, then Word's GUI will warn about this. If you ignore that warning then you possible can damage the printer while next printing. Most printers will not print into their not printable page ranges even if you told to do so. That is to avoid that damaging.
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]
But this code also give 200 margin to header from left. I Want only for table.
Thanks in Advance.
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.
Example:
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();
}
}
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.