[英]How do I make center-aligned text on the right side of a picture (Apache POI docx)
This is the result I want to get:这是我想要得到的结果:
Things I've tried:我尝试过的事情:
XWPFRun
to the XWPFParagraph
the picture is in, but that treats the picture as a character in the paragraph and it doesn't work ( example );XWPFRun
添加到图片所在的XWPFParagraph
中,但这会将图片视为段落中的字符并且不起作用(示例);XWPFParagraph
s, but that just puts the text below the picture;XWPFParagraph
s,但这只是将文本放在图片下方; I also thought it could be done by making a two-column paragraph then putting the picture in one column, and the text in the second column.我还认为可以通过制作一个双栏段落然后将图片放在一栏中,将文本放在第二栏中来完成。 But I couldn't find a way to make a two-column paragraph with Apache POI.
但是我找不到用Apache POI制作两栏段落的方法。
Here's the code I used for two XWPFRun
s in a XWPFParagraph
which doesn't give me the result I want:这是我在
XWPFParagraph
中用于两个XWPFRun
的代码,它没有给我想要的结果:
XWPFDocument doc = new XWPFDocument();
XWPFHeader header = doc.createHeader(HeaderFooterType.DEFAULT);
XWPFParagraph headerParagraph = header.createParagraph();
XWPFRun pictureRun = headerParagraph.createRun();
String imgFile = "D:\\picture.jpg";
try (FileInputStream is = new FileInputStream(imgFile)) {
pictureRun.addPicture(is,
Document.PICTURE_TYPE_PNG,
imgFile,
Units.pixelToEMU(297),
Units.pixelToEMU(124));
} catch (Exception e) {
e.printStackTrace();
}
XWPFRun headerTextRun = headerParagraph.insertNewRun(1);
headerTextRun.setText("Some text");
try (FileOutputStream out = new FileOutputStream("D:\\test.docx")) {
doc.write(out);
} catch (Exception e) {
e.printStackTrace();
}
Is there some way I can do this?有什么办法可以做到这一点? Thanks in advance.
提前致谢。
The best solution for this will be using a table.最好的解决方案是使用表格。 To remove the borders current
apache poi
XWPFTable provides removeBorders
method.去除边框当前
apache poi
XWPFTable提供了removeBorders
方法。
Complete example:完整示例:
import java.io.FileOutputStream;
import java.io.FileInputStream;
import org.apache.poi.xwpf.usermodel.*;
import org.apache.poi.util.Units;
import java.math.BigInteger;
public class CreateWordTablePIctureAndTextNoBorders {
static final int TWIPS_PER_INCH = 1440; //measurement unit for table cell width and tab stop pos is twips (twentieth of an inch point);
public static void main(String[] args) throws Exception {
XWPFDocument document = new XWPFDocument();
XWPFParagraph paragraph = document.createParagraph();
XWPFRun run = paragraph.createRun();
run.setText("Image on the left and text on the right");
//create table
XWPFTable table = document.createTable();
//remove all table borders
table.removeBorders();
//set table width 6 inches
table.setWidth(6 * TWIPS_PER_INCH);
//create CTTblGrid for this table with widths of the 2 columns.
//necessary for Libreoffice/Openoffice to accept the column widths.
//first column = 3 inches width
table.getCTTbl().addNewTblGrid().addNewGridCol().setW(BigInteger.valueOf(3 * TWIPS_PER_INCH));
//second column = 3 inches width
table.getCTTbl().getTblGrid().addNewGridCol().setW(BigInteger.valueOf(3 * TWIPS_PER_INCH));
//get or create first row
XWPFTableRow tableRow = table.getRow(0); if (tableRow == null) tableRow = table.createRow();
//get or create first cell
XWPFTableCell cell = tableRow.getCell(0); if (cell == null) cell = tableRow.addNewTableCell();
//set cell vertical align
cell.setVerticalAlignment(XWPFTableCell.XWPFVertAlign.CENTER);
//set width for first column
cell.setWidth("50%");
//get or add first paragraph in first cell
paragraph = cell.getParagraphArray(0); if (paragraph == null) paragraph = cell.addParagraph();
//create run in paragraph having picture
run = paragraph.createRun();
run.addPicture(new FileInputStream("./Koala.png"), XWPFDocument.PICTURE_TYPE_PNG, "Koala.png", Units.toEMU(200), Units.toEMU(150));
//get or create econd cell
cell = tableRow.getCell(1); if (cell == null) cell = tableRow.addNewTableCell();
cell.setWidth("50%");
cell.setVerticalAlignment(XWPFTableCell.XWPFVertAlign.CENTER);
//get or add first paragraph in second cell
paragraph = cell.getParagraphArray(0); if (paragraph == null) paragraph = cell.addParagraph();
paragraph.setAlignment(ParagraphAlignment.CENTER);
run = paragraph.createRun();
run.setText("Lorem ipsum dolor");
run = paragraph.createRun();
run.addBreak();
run.setText("consetetur sadipscing elitr, sed diam");
run = paragraph.createRun();
run.addBreak();
run.setText("nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat,");
run = paragraph.createRun();
run.addBreak();
run.setText("sed diam voluptua.");
paragraph = document.createParagraph();
FileOutputStream out = new FileOutputStream("CreateWordTablePIctureAndTextNoBorders.docx");
document.write(out);
out.close();
document.close();
}
}
Produces:产生:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.