简体   繁体   English

带图像和文本的Apache Poi WORD段落

[英]Apache Poi WORD Paragraph with image and text

i am trying to create a paragraph using APACHE POI,that contains an image on the left and some text on the right. 我正在尝试使用APACHE POI创建一个段落,该段落左侧包含一个图像,右侧包含一些文本。 Is there any way that i can set alignment between these two components? 有什么方法可以设置这两个组件之间的对齐方式?

Below is the code that does what i want in a table, but it also renders the two paragraphs. 下面是执行我想要在表中执行的代码,但是它也呈现了两个段落。

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

}

Thank you in advance. 先感谢您。

Main problem with your code is that it creates the XWPFParagraph s in the XWPFDocument first and then it sets them into the XWPFTableCell s. 您的代码的主要问题是,它首先在XWPFParagraph中创建XWPFDocument ,然后将其设置为XWPFTableCell But a XWPFTableCell contains it's own body and is able containing content of a whole document also. 但是XWPFTableCell包含它自己的主体,并且还可以包含整个文档的内容。 So after that the paragraph is in the documents body and in the table cells body. 因此,此后该段位于文档正文和表格单元格正文中。 So don't do this. 所以不要这样做。 Instead get XWPFParagraph s from or create XWPFParagraph s in the XWPFTableCell s if needed. 如果需要,可以从XWPFParagraph获取XWPFParagraph或在XWPFTableCell创建XWPFParagraph

In general your requirement "an image on the left and some text on the right" can be fulfilled two ways. 通常,您可以通过两种方法来满足您的要求:“图像在左侧,文本在右侧”。 It can be fulfilled using a table, as you tried. 您可以尝试使用表来实现它。 But it can also be fulfilled using only one paragraph, as you told in your question's title. 但是,正如您在问题标题中所说的那样,也可以仅使用一个段落来实现它。 This paragraph must contain tab stop settings then and the runs must be split by tabulators. 该段必须包含制表位设置,然后运行必须由制表符拆分。

The following code shows both solutions: 以下代码显示了两种解决方案:

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();

 }
}

Result: 结果:

在此处输入图片说明

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

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