如何在图片的右侧制作居中对齐的文本(Apache POI docx)
How do I make center-aligned text on the right side of a picture (Apache POI docx)
这是我想要得到的结果:
我尝试过的事情:
- 将另一个
XWPFRun
添加到图片所在的 XWPFParagraph
中,但这会将图片视为段落中的一个字符并且不起作用 (example);
- 将图片和文字放在 table 的两个不同单元格中,但我找不到使 table 边框不可见的方法;
- 创建两个单独的
XWPFParagraph
,但这只是将文本放在图片下方;
我还认为可以通过制作一个双栏段落然后将图片放在一栏中,将文字放在第二栏中来完成。但是我找不到用 Apache POI 制作两栏段落的方法。
这是我在 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();
}
有什么方法可以做到这一点吗?提前致谢。
最好的解决方案是使用 table。要删除边框,当前 apache poi
XWPFTable 提供了 removeBorders
方法。
完整示例:
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();
}
}
生产:
这是我想要得到的结果:
我尝试过的事情:
- 将另一个
XWPFRun
添加到图片所在的XWPFParagraph
中,但这会将图片视为段落中的一个字符并且不起作用 (example); - 将图片和文字放在 table 的两个不同单元格中,但我找不到使 table 边框不可见的方法;
- 创建两个单独的
XWPFParagraph
,但这只是将文本放在图片下方;
我还认为可以通过制作一个双栏段落然后将图片放在一栏中,将文字放在第二栏中来完成。但是我找不到用 Apache POI 制作两栏段落的方法。
这是我在 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();
}
有什么方法可以做到这一点吗?提前致谢。
最好的解决方案是使用 table。要删除边框,当前 apache poi
XWPFTable 提供了 removeBorders
方法。
完整示例:
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();
}
}
生产: