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

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

问题描述

我正在创建一个 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));

我想在图像中显示的空间下方删除此页脚.

解决方案

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

使用 apache poi 4.1.0 这可以使用:

<预><代码>...XWPFP段落段落......段落.setSpacingAfter(0);段落.setSpacingBetween(1d, LineSpacingRule.AUTO);...

完整示例:

import java.io.FileOutputStream;导入 java.io.FileInputStream;导入 org.apache.poi.xwpf.usermodel.*;导入 org.apache.poi.wp.usermodel.HeaderFooterType;导入 org.apache.poi.util.Units;导入 org.openxmlformats.schemas.wordprocessingml.x2006.main.CTSectPr;导入 org.openxmlformats.schemas.wordprocessingml.x2006.main.CTPageSz;导入 org.openxmlformats.schemas.wordprocessingml.x2006.main.CTPageMar;导入 java.math.BigInteger;公共类 CreateWordHeaderFooterNullMargin {public static void main(String[] args) 抛出异常 {XWPFDocument 文档 = 新 XWPFDocument();//正文内容XWPFParagraph 段落 = document.createParagraph();XWPFRun run=paragraph.createRun();run.setText("The Body");//创建头开始XWPFHeader header = document.createHeader(HeaderFooterType.DEFAULT);段落 = header.getParagraphArray(0);if (paragraph == null)paragraph = header.createParagraph();段落.setSpacingAfter(0);段落.setSpacingBetween(1d, LineSpacingRule.AUTO);运行 = 段落.createRun();run.setText("标题");//创建页脚开始XWPFFooter 页脚 = document.createFooter(HeaderFooterType.DEFAULT);段落 = 页脚.getParagraphArray(0);如果(段落==空)段落=footer.createParagraph();段落.setAlignment(ParagraphAlignment.CENTER);段落.setSpacingAfter(0);段落.setSpacingBetween(1d, LineSpacingRule.AUTO);运行 = 段落.createRun();String imgFile="菊花.jpg";run.addPicture(new FileInputStream(imgFile), XWPFDocument.PICTURE_TYPE_JPEG, imgFile, Units.toEMU(500), Units.toEMU(25));//创建页边距CTSectPr sectPr = document.getDocument().getBody().getSectPr();if (sectPr == null) sectPr = document.getDocument().getBody().addNewSectPr();CTPageSz pageSz = sectPr.addNewPgSz();//纸质格式信pageSz.setW(BigInteger.valueOf(12240));//12240 缇 = 12240/20 = 612 pt = 612/72 = 8.5"pageSz.setH(BigInteger.valueOf(15840));//15840 缇 = 15840/20 = 792 pt = 792/72 = 11"CTPageMar pageMar = sectPr.getPgMar();if (pageMar == null) pageMar = sectPr.addNewPgMar();pageMar.setLeft(BigInteger.valueOf(720));//720 二十分之一英寸点 (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);关闭();文档.close();}}

免责声明:在我看来,页边距为 0 的页面设置是不推荐的.大多数打印机无法打印完整的纸张尺寸.在纸张的左侧、右侧、顶部和/或底部有最小空间的可打印区域.如果您将页边距设置为 0,则 WordGUI 将对此发出警告.如果您忽略该警告,则可能会在下次打印时损坏打印机.即使您被告知要这样做,大多数打印机也不会打印到它们不可打印的页面范围内.那是为了避免破坏.

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.

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

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