首页 > 解决方案 > 带有图像和文本的 Apache Poi WORD 段落

问题描述

我正在尝试使用 APACHE POI 创建一个段落,其中包含左侧的图像和右侧的一些文本。有什么方法可以设置这两个组件之间的对齐方式吗?

下面是在表格中执行我想要的代码,但它也呈现了两个段落。

public void createWord() throws IOException, InvalidFormatException {

    XWPFDocument document = new XWPFDocument();
      XWPFTable table = document.createTable();
      FileOutputStream out = new FileOutputStream(new File("createdocument.docx"));
      File img = new File("C:\\Users\\r4rfgretg\\Pictures\\geregrg.png");
        XWPFParagraph imageParagraph = document.createParagraph();
        imageParagraph.setFontAlignment(ParagraphAlignment.CENTER.getValue());
        XWPFRun imageRun = imageParagraph.createRun();
        imageRun.addBreak();
        imageRun.addBreak();
        imageRun.addBreak();

        imageRun.addPicture(new FileInputStream(img), org.apache.poi.xwpf.usermodel.Document.PICTURE_TYPE_PNG, "test",
                Units.toEMU(100), Units.toEMU(100));

        XWPFParagraph textParagraph = document.createParagraph();
        XWPFRun textRun = textParagraph.createRun();
        textRun.addBreak();
        textRun.addBreak();
        textRun.addBreak();
        imageRun.addBreak();
        imageRun.addBreak();
        imageRun.addBreak();
        textRun.setText("KOU323D342342OUMjuj43432424S");
        textRun.addBreak();
        textRun.addBreak();
        textRun.setText("1/1/2019                           1/1/2020");
        textRun.addBreak();
        textRun.addBreak();
        textRun.setText("GR123456789");

      XWPFTableRow tableRowOne = table.getRow(0);
      tableRowOne.getCell(0).setParagraph(imageParagraph);
      tableRowOne.addNewTableCell().setParagraph(textParagraph);

    document.write(out);
    out.close();
    System.out.println("createdocument.docx written successully");

}

先感谢您。

标签: javaapacheapache-poi

解决方案


您的代码的主要问题是它首先创建了XWPFParagraphs,XWPFDocument然后将它们设置为XWPFTableCells。但是 aXWPFTableCell包含它自己的主体,也能够包含整个文档的内容。因此,在那之后,该段落位于文档正文和表格单元格正文中。所以不要这样做。如果需要,可以从 s 中获取XWPFParagraphs 或在 s 中创建XWPFParagraphs 。XWPFTableCell

一般来说,您的要求“左边的图像和右边的一些文本”可以通过两种方式实现。正如您尝试的那样,它可以使用表格来完成。但正如您在问题标题中所说,它也可以仅使用一个段落来实现。该段落必须包含制表位设置,并且运行必须由制表符拆分。

以下代码显示了这两种解决方案:

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

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

import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTabStop;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STTabJc;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTblWidth;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STTblWidth;

import java.math.BigInteger;

public class CreateWordTabulatorAndTable {
 public static void main(String[] args) throws Exception {

  String text = "Text";
  String imgFile="Koala.png";
  int twipsPerInch = 1440; //measurement unit for table cell width and tab stop pos is twips (twentieth of an inch point)

  XWPFDocument document = new XWPFDocument();
  XWPFParagraph paragraph = document.createParagraph();
  XWPFRun run = paragraph.createRun(); 
  run.setText("Image on the left and text on the right");

  paragraph = document.createParagraph();
  run = paragraph.createRun(); 
  run.setText("First using a table:");

  //create table
  XWPFTable table = document.createTable();
  table.setWidth(6*twipsPerInch);
  //create CTTblGrid for this table with widths of the 2 columns. 
  //necessary for Libreoffice/Openoffice to accept the column widths.
  //first column = 2 inches width
  table.getCTTbl().addNewTblGrid().addNewGridCol().setW(BigInteger.valueOf(2*twipsPerInch));
  //second column = 4 inches width
  table.getCTTbl().getTblGrid().addNewGridCol().setW(BigInteger.valueOf(4*twipsPerInch));
  //create first row
  XWPFTableRow tableRow = table.getRow(0);
  //first cell
  XWPFTableCell cell = tableRow.getCell(0);
  //set width for first column = 2 inches
  CTTblWidth tblWidth = cell.getCTTc().addNewTcPr().addNewTcW();
  tblWidth.setW(BigInteger.valueOf(2*twipsPerInch));
  //STTblWidth.DXA is used to specify width in twentieths of a point.
  tblWidth.setType(STTblWidth.DXA);
  //first paragraph in first cell
  paragraph = cell.getParagraphArray(0); if (paragraph == null) paragraph = cell.addParagraph();
  //first run in paragraph having picture
  run = paragraph.createRun();  
  run.addPicture(new FileInputStream(imgFile), XWPFDocument.PICTURE_TYPE_PNG, imgFile, Units.toEMU(100), Units.toEMU(100));
  //second cell
  cell = tableRow.addNewTableCell();
  cell.setText(text);

  paragraph = document.createParagraph();

//---------------------------------------------------------------------------------------------------

  paragraph = document.createParagraph();
  run = paragraph.createRun(); 
  run.setText("Second using tabulator having tab stops:");

  //create tab stop at 2 inches position
  paragraph = document.createParagraph();
  paragraph.setAlignment(ParagraphAlignment.LEFT);
  CTTabStop tabStop = paragraph.getCTP().getPPr().addNewTabs().addNewTab();
  tabStop = paragraph.getCTP().getPPr().getTabs().addNewTab();
  tabStop.setVal(STTabJc.LEFT);
  tabStop.setPos(BigInteger.valueOf(2 * twipsPerInch));
  //first run in paragraph having picture
  run = paragraph.createRun();  
  run.addPicture(new FileInputStream(imgFile), XWPFDocument.PICTURE_TYPE_PNG, imgFile, Units.toEMU(100), Units.toEMU(100));
  run.addTab();
  //second run 
  run = paragraph.createRun();  
  run.setText(text);

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

 }
}

结果:

在此处输入图像描述


推荐阅读