我正在创建poi word文档。我已经设置了页边距0,但是它们是底部和页脚图像之间的多余空间,我想删除此空间。我使用了无效的代码
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));
我想在图像中显示的空间下方删除此页脚。
答案 0 :(得分:2)
您的问题与页边距无关,但与页脚中的段落设置无关。 Word
段落具有每个段落后的间距以及段落中各行之间的间距的设置。如果页脚中的图片在页脚中的段落中是内联的,则段落后的间距必须为0,而间距必须为1,以避免面对您的间距。
使用apache poi 4.1.0
可以通过以下方式设置:
...
XWPFParagraph paragraph...
...
paragraph.setSpacingAfter(0);
paragraph.setSpacingBetween(1d, LineSpacingRule.AUTO);
...
完整示例:
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();
}
}
免责声明:我不建议将页边距设为0的页面设置。大多数打印机无法打印完整尺寸的纸张。在纸张的左,右,顶部和/或底部有最小的间距的可打印区域。如果将页边距设置为0,则Word
的{{1}}会发出警告。如果忽略该警告,则可能在下次打印时损坏打印机。即使您告知这样做,大多数打印机也不会打印到其不可打印的页面范围内。那是为了避免这种破坏。