如何删除页面和页脚POI Java之间的多余空间 [英] How to remove extra space between page and footer poi java

查看:64
本文介绍了如何删除页面和页脚POI Java之间的多余空间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建poi word文档.我已经设置了页边距0,但它们是底部和页脚图像之间的多余空间,我想删除此空间.我使用了无效的代码

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.

推荐答案

您的问题与页边距无关,但与页脚中的段落设置无关. Word 段落具有每个段落后的间距以及段落中各行之间的间距的设置.如果页脚中的图片在页脚中的段落中是内联的,则段落后的间距必须为0,而间距必须为1,以避免面对您的间距.

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.

使用 apache poi 4.1.0 可以使用以下方法进行设置:

Using apache poi 4.1.0 this can be set using:

...
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 GUI 会发出警告.如果您忽略该警告,则可能在下次打印时损坏打印机.即使您告知这样做,大多数打印机也不会打印到其不可打印的页面范围内.那是为了避免这种破坏.

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.

这篇关于如何删除页面和页脚POI Java之间的多余空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆