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.
Related
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 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.
I want to generate different headers in first page and other pages in poi word, So I used XWPFHeaderFooterPolicy.FIRST and XWPFHeaderFooterPolicy.DEFAULT. when I using XWPFHeaderFooterPolicy.DEFAULT I can insert my header successfully, but when I change to XWPFHeaderFooterPolicy.FIRST, I cannot see there is a header in my first page, this is my code in below, what's wrong with it? thanks!
XWPFHeader header = headerFooterPolicy.createHeader(XWPFHeaderFooterPolicy.FIRST);
paragraph = header.createParagraph();
paragraph.setAlignment(ParagraphAlignment.LEFT);
run = paragraph.createRun();
run.setText("header");
There is nothing in that class to tell Word to display different first page headers. You will need the most recent version of POI, and then create the header from XWPFDocument using:
XWPFDocument.createHeader(HeaderFooterType.FIRST);
Otherwise you need to break into the CT classes and in the section properties set the titlePg property. I don't recommend this approach though as it is likely to change.
That there is a different header set for first page only, means not that this header will also be shown. In Word GUI there is a checkbox [x] Different First Page in Header & Footer Tools to achieve that.
And according Office Open XML Part 4 - Markup Language Reference there must a boolean XML element titlePg be set to determine that there is a title page present.
In actual final apache poi version 3.15 this XML element titlePg can only be set using underlying low level objects using doc.getDocument().getBody().getSectPr().addNewTitlePg();.
But apache poi version 3.16 Beta 2 has doc.createHeader(HeaderFooterType.FIRST); which sets titlePg flag in XML.
Complete example:
import java.io.*;
import org.apache.poi.wp.usermodel.*;
import org.apache.poi.xwpf.usermodel.*;
public class CreateWordHeaderFooterDifferent {
public static void main(String[] args) throws Exception {
XWPFDocument doc= new XWPFDocument();
// the body content
XWPFParagraph paragraph = doc.createParagraph();
XWPFRun run=paragraph.createRun();
run.setText("The Body:");
paragraph = doc.createParagraph();
run=paragraph.createRun();
run.setText("Lorem ipsum.... page 1");
paragraph = doc.createParagraph();
run=paragraph.createRun();
run.addBreak(BreakType.PAGE);
run.setText("Lorem ipsum.... page 2");
paragraph = doc.createParagraph();
run=paragraph.createRun();
run.addBreak(BreakType.PAGE);
run.setText("Lorem ipsum.... page 3");
// create first page header
XWPFHeader header = doc.createHeader(HeaderFooterType.FIRST);
paragraph = header.createParagraph();
paragraph.setAlignment(ParagraphAlignment.LEFT);
run = paragraph.createRun();
run.setText("The first page header:");
// create default page header
header = doc.createHeader(HeaderFooterType.DEFAULT);
paragraph = header.createParagraph();
paragraph.setAlignment(ParagraphAlignment.LEFT);
run = paragraph.createRun();
run.setText("The default page header:");
// create footer
XWPFFooter footer = doc.createFooter(HeaderFooterType.DEFAULT);
paragraph = footer.createParagraph();
paragraph.setAlignment(ParagraphAlignment.CENTER);
run = paragraph.createRun();
run.setText("Page ");
paragraph.getCTP().addNewFldSimple().setInstr("PAGE \\* MERGEFORMAT");
run = paragraph.createRun();
run.setText(" of ");
paragraph.getCTP().addNewFldSimple().setInstr("NUMPAGES \\* MERGEFORMAT");
doc.write(new FileOutputStream("CreateWordHeaderFooterDifferent.docx"));
}
}
But in my opinion the setting titlePg flag in XML automatically while HeaderFooterType.FIRST is created is not correct. Since Word can toggle between [x] Different First Page and [] Different First Page, apache poi should also be able to do so. So the setting the titlePg flag should be a method in XWPFDocument.
I'm trying to use Apache poi word 3.8 to create word document in persian/arabic language.
My question is: how change text direction in document? ( it means changing text direction not changing just paragraph text alignment)
In MS Word we could use Right-to-left text direction to change text direction and Align Right to set alignment. What’s equivalent of the first one in poi set property?
This is bidirectional text direction support (bidi) and is not yet implemented in apache poi per default. But the underlying object org.openxmlformats.schemas.wordprocessingml.x2006.main.CTPPrBase supports this. So we must get this underlying object from the XWPFParagraph.
Example:
import java.io.FileOutputStream;
import org.apache.poi.xwpf.usermodel.*;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTP;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTPPr;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STOnOff;
public class CreateWordRTLParagraph {
public static void main(String[] args) throws Exception {
XWPFDocument doc= new XWPFDocument();
XWPFParagraph paragraph = doc.createParagraph();
XWPFRun run = paragraph.createRun();
run.setText("Paragraph 1 LTR");
paragraph = doc.createParagraph();
CTP ctp = paragraph.getCTP();
CTPPr ctppr;
if ((ctppr = ctp.getPPr()) == null) ctppr = ctp.addNewPPr();
ctppr.addNewBidi().setVal(STOnOff.ON);
run = paragraph.createRun();
run.setText("السلام عليكم");
paragraph = doc.createParagraph();
run = paragraph.createRun();
run.setText("Paragraph 3 LTR");
FileOutputStream out = new FileOutputStream("WordDocument.docx");
doc.write(out);
out.close();
doc.close();
}
}
I want to create a docx file programatically via Apache POI.
I want to add some mathematical equations in some lines.
How can I do this in a way that when the user open the docx file it see that equations as docx equation form.
I mean I don't want simply give a background colour to that run, I want when the user does double click on my equation MS-Word open it in equation forms.
Thanks in advance
This is not really complicated:
import java.io.FileOutputStream;
import org.apache.poi.xwpf.usermodel.*;
import org.openxmlformats.schemas.officeDocument.x2006.math.CTOMath;
import org.openxmlformats.schemas.officeDocument.x2006.math.CTRad;
import org.openxmlformats.schemas.officeDocument.x2006.math.CTR;
import org.openxmlformats.schemas.officeDocument.x2006.math.STStyle;
/*
To
import org.openxmlformats.schemas.officeDocument.x2006.math.CTOMath;
import org.openxmlformats.schemas.officeDocument.x2006.math.CTRad;
import org.openxmlformats.schemas.officeDocument.x2006.math.CTR;
import org.openxmlformats.schemas.officeDocument.x2006.math.STStyle;
the fully ooxml-schemas-1.3.jar is needed as mentioned in https://poi.apache.org/faq.html#faq-N10025
*/
public class CreateWordFormula {
public static void main(String[] args) throws Exception {
XWPFDocument doc= new XWPFDocument();
XWPFParagraph paragraph = doc.createParagraph();
XWPFRun run=paragraph.createRun();
run.setText("The Formula: ");
CTOMath cTOMath = paragraph.getCTP().addNewOMath();
CTR cTR = cTOMath.addNewR();
cTR.addNewRPr().addNewSty().setVal(STStyle.P);
cTR.addNewT2().setStringValue("a²+b²=c²");
run=paragraph.createRun();
run.setText(" text after the formula");
paragraph = doc.createParagraph();
run=paragraph.createRun();
run.setText("The Formula: ");
cTOMath = paragraph.getCTP().addNewOMath();
CTRad cTRad = cTOMath.addNewRad();
cTR = cTRad.addNewDeg().addNewR();
cTR.addNewRPr().addNewSty().setVal(STStyle.P);
cTR.addNewT2().setStringValue("2");
cTR = cTRad.addNewE().addNewR();
cTR.addNewRPr().addNewSty().setVal(STStyle.P);
cTR.addNewT2().setStringValue("a²+b²");
run=paragraph.createRun();
run.setText(" text after the formula");
doc.write(new FileOutputStream("WordFormula.docx"));
}
}
For CTOMath see http://grepcode.com/file/repo1.maven.org/maven2/org.apache.poi/ooxml-schemas/1.1/org/openxmlformats/schemas/officeDocument/x2006/math/CTOMath.java#CTOMath.addNewRad%28%29.