[英]How can I extract right-to-left text from .doc and .docx files using Apache POI in java?
[英]How do I make center-aligned text on the right side of a picture (Apache POI docx)
我尝试过的事情:
XWPFRun
添加到图片所在的XWPFParagraph
中,但这会将图片视为段落中的字符并且不起作用(示例);XWPFParagraph
s,但这只是将文本放在图片下方;我还认为可以通过制作一个双栏段落然后将图片放在一栏中,将文本放在第二栏中来完成。 但是我找不到用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();
}
有什么办法可以做到这一点? 提前致谢。
最好的解决方案是使用表格。 去除边框当前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();
}
}
产生:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.