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

时间:2018-10-27 11:10:17

标签: java apache apache-poi

我正在尝试使用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");

}

谢谢。

1 个答案:

答案 0 :(得分:2)

您的代码的主要问题是它首先在XWPFParagraph中创建XWPFDocument,然后将它们设置为XWPFTableCell。但是XWPFTableCell包含它自己的主体,并且还可以包含整个文档的内容。因此,此后该段位于文档正文和表格单元格正文中。所以不要这样做。如果需要,请改为从XWPFParagraph中获取XWPFParagraph,或在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(); } }

通常,您可以通过两种方式满足“左图和右图”的要求。您可以尝试使用表来实现它。但是,正如您在问题标题中所说的那样,也可以仅使用一个段落来实现它。此段必须包含制表符停止设置,然后运行必须由制表符拆分。

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

{{1}}

结果:

enter image description here