java 向word插入图片怎么让图片一排两个
时间: 2023-02-09 18:53:57 浏览: 75
如果要在 Java 中将图片插入到 Word 文档中,你可以使用 Apache POI 库。下面是一个简单的示例,该示例将图片插入到第一页的第一个表格中的第一个单元格:
```
import java.io.FileInputStream;
import java.io.FileOutputStream;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFTable;
import org.apache.poi.xwpf.usermodel.XWPFTableCell;
import org.apache.poi.xwpf.usermodel.XWPFTableRow;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTShd;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTc;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTcPr;
public class Main {
public static void main(String[] args) throws Exception {
XWPFDocument document = new XWPFDocument();
XWPFTable table = document.createTable(1, 1);
XWPFTableRow tableRow = table.getRow(0);
XWPFTableCell tableCell = tableRow.getCell(0);
XWPFParagraph paragraph = tableCell.addParagraph();
XWPFRun run = paragraph.createRun();
run.addPicture(new FileInputStream("image.jpg"), XWPFDocument.PICTURE_TYPE_JPEG, "image.jpg", Units.toEMU(200), Units.toEMU(200));
FileOutputStream out = new FileOutputStream("output.docx");
document.write(out);
out.close();
document.close();
}
}
```
如果要让图片一排两个,你需要创建一个表格,并将两个图片插入到表格的两个单元格中。下面是修改后的代码:
```
import java.io.FileInputStream;
import java.io.FileOutputStream;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFTable;
import org.apache.poi.xwpf.usermodel.XWPFTableCell;