繁体   English   中英

使用 Java 中的 Apache POI 删除页眉中的边距

[英]Remove the margins in the header using Apache POI in Java

我正在使用 Apache POI 版本 4.1.2。 当我尝试在页眉中创建带有图像的文档时,在图像的 4 侧为页眉分配了一些默认边距。

如何删除页眉部分的默认边距并将图像完全占据在页眉部分?

XWPFDocument document = new XWPFDocument();
CTSectPr sectPr = document.getDocument().getBody().addNewSectPr();
XWPFHeaderFooterPolicy headerFooterPolicy = new XWPFHeaderFooterPolicy(document, sectPr);
XWPFHeader header = document.createHeader(HeaderFooterType.FIRST);
XWPFParagraph paragraph = header.createParagraph();
XWPFRun run = paragraph.createRun();

//Adding the image using run.addPicture() method.

输出文件如下所示, 输出文件

我想删除边距并使该图像完全占据标题。

使用XWPFRun.addPicture图像与文本内联,因此段落设置很重要。 默认情况下,Word 段落后面有间距。 使用XWPFParagraph.setSpacingAfter可以将其设置为 0。Word 段落默认在段落中的行之间具有间距。 使用XWPFParagraph.setSpacingBetween可以将其设置为单行,因此段落中的行之间没有间距。

要使页眉中的图片出现在页面的绝对顶部,页首页边距需要为 0。页眉和页边距之间的距离也需要为 0。不幸的是,设置页面大小和页边距尚未实现XWPF . 所以我们需要使用org.openxmlformats.schemas.wordprocessingml.x2006.main.*类。

图片左右的间隙是图片的左边缘和左页边距之间的距离,分别是图片的右边缘和右页边距。 这取决于图片的宽度。 但是我看不出如何在不违反图片纵横比的情况下消除这些间隙,从而扭曲图片。 如果这是想要的,则将图片的宽度设置为适当的。

完整示例:

import java.io.FileOutputStream;
import java.io.FileInputStream;
import java.io.IOException;

import org.apache.poi.util.Units;

import org.apache.poi.xwpf.usermodel.*;

import org.apache.poi.xwpf.model.XWPFHeaderFooterPolicy;

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 CreateWordHeaderFooter {

 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:");

  paragraph = document.createParagraph();
  run=paragraph.createRun();  
  run.setText("Lorem ipsum....");

  // create header start
  CTSectPr sectPr = document.getDocument().getBody().addNewSectPr();
  XWPFHeaderFooterPolicy headerFooterPolicy = new XWPFHeaderFooterPolicy(document, sectPr);

  XWPFHeader header = headerFooterPolicy.createHeader(XWPFHeaderFooterPolicy.DEFAULT);

  paragraph = header.getParagraphArray(0);
  if (paragraph == null) paragraph = header.createParagraph();
  paragraph.setAlignment(ParagraphAlignment.CENTER);
  
  // set spacing after paragraph 0
  paragraph.setSpacingAfter(0);
  // set spacing between lines in paragraph to 1 (single)
  paragraph.setSpacingBetween(1d, LineSpacingRule.AUTO);

  // the image is inline with text so the paragraph settings matters
  run = paragraph.createRun();  
  String imgFile="./laptop.jpg";
  run.addPicture(new FileInputStream(imgFile), XWPFDocument.PICTURE_TYPE_JPEG, imgFile, Units.toEMU(300), Units.toEMU(200));

  // create footer start
  XWPFFooter footer = headerFooterPolicy.createFooter(XWPFHeaderFooterPolicy.DEFAULT);

  paragraph = footer.createParagraph();
  paragraph.setAlignment(ParagraphAlignment.CENTER);

  run = paragraph.createRun();  
  run.setText("The Footer:");
  
  // create page margins
  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));
  // set top page margin 0, so header can be at absolute top
  pageMar.setTop(BigInteger.valueOf(0));
  //pageMar.setBottom(BigInteger.valueOf(0));
  //pageMar.setFooter(BigInteger.valueOf(0));
  // set distance between header and page margin 0, so header starts at absolute top
  pageMar.setHeader(BigInteger.valueOf(0));
  //pageMar.setGutter(BigInteger.valueOf(0));

  FileOutputStream out = new FileOutputStream("./test.docx");
  document.write(out);
  out.close();
  document.close();

 }
}

结果:

在此处输入图像描述

要使图片全宽,需要知道所需的宽度(以点为单位)( pt )。 那将是页边距之间页面的内部宽度。 在我的示例中,8.5" 页面宽度 - 0.5" 左边距 - 0.5" 右边距 = 7.5"。 那将是

...
  long pageInnerWidthPt = Math.round(7.5 * 72d); //8.5" page width - 0.5" left margin - 0.5" right margin = 7.5"
  run.addPicture(new FileInputStream(imgFile), XWPFDocument.PICTURE_TYPE_JPEG, imgFile, Units.toEMU(pageInnerWidthPt), Units.toEMU(200));
...

在代码中。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM