Java在word指定页数开头插入一张图片
时间: 2024-05-09 12:17:15 浏览: 157
用java往word插入图片
4星 · 用户满意度95%
可以使用Apache POI库来实现在Word指定页数开头插入一张图片。具体的实现方法可以参考以下代码:
```
import java.io.FileInputStream;
import java.io.FileOutputStream;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;
import org.apache.poi.xwpf.usermodel.XWPFPicture;
import org.apache.poi.xwpf.usermodel.XWPFPictureData;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;
import org.apache.poi.xwpf.usermodel.XWPFPictureData;
import org.apache.poi.xwpf.usermodel.XWPFPicture;
import org.apache.poi.xwpf.usermodel.ParagraphAlignment;
import org.apache.poi.xwpf.usermodel.TextOrientation;
import org.apache.xmlbeans.XmlCursor;
import org.apache.xmlbeans.XmlException;
import org.apache.xmlbeans.XmlObject;
import org.apache.xmlbeans.impl.xb.xmlschema.SpaceAttribute$Enum;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTRelativePosition;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTWrapText;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTP;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTPoint2D;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTAnchor;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTBlip;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STWrapText;
import org.openxmlformats.schemas.drawingml.x2006.main.*;
public class WordImageInsertDemo {
public static void main(String[] args) {
try {
String docxPath = "D:\\temp\\test.docx";
int pageNumber = 2;
String picPath = "D:\\temp\\test.png";
//打开docx文件
FileInputStream fis = new FileInputStream(docxPath);
XWPFDocument document = new XWPFDocument(fis);
fis.close();
//获取目标页面的段落
XWPFParagraph paragraph = document.getParagraphs().stream()
.filter(p -> document.getPosOfParagraph(p) >= document.getDocument().getBody().getSectPr().getPgSz().getH().intValue()*pageNumber)
.findFirst()
.orElse(null);
if(paragraph == null) {
System.out.println("未找到指定页面。");
return;
}
//在目标段落前面插入一张图片
XmlCursor cursor = paragraph.getCTP().newCursor();
cursor.toPrevSibling();
XWPFParagraph newParagraph = document.insertNewParagraph(cursor);
XWPFRun newRun = newParagraph.createRun();
newRun.addCarriageReturn();
//图片嵌入段落
String id = document.addPictureData(new FileInputStream(picPath), XWPFDocument.PICTURE_TYPE_PNG);
XWPFPicture picture = newRun.addPicture(new FileInputStream(picPath), XWPFDocument.PICTURE_TYPE_PNG, "abcdef", Units.toEMU(180), Units.toEMU(100));
//设置图片位置(水平和垂直居中)
picture.getCTPicture().getSpPr().getLn().setNoFill(true); //去掉边框
CTPicture ctPic = picture.getCTPicture();
CTTransform2D ctTransform2D = CTTransform2D.Factory.newInstance();
ctTransform2D.setRot(0);
CTPoint2D ctPoint2D = ctTransform2D.addNewOff();
ctPoint2D.setX(Units.toEMU(400)); //横坐标
ctPoint2D.setY(Units.toEMU(200)); //纵坐标
ctPic.getSpPr().getXfrm().setOff(ctTransform2D);
CTAnchor ctAnchor = ctPic.getAnchor();
ctAnchor.setSimplePos2(false);
ctAnchor.setX(0);
ctAnchor.setY(0);
CTMarker ctMarker = ctAnchor.addNewFrom();
ctMarker.setCol(0);
ctMarker.setColOff(0);
ctMarker.setRow(0);
ctMarker.setRowOff(0);
ctMarker = ctAnchor.addNewTo();
ctMarker.setCol(0);
ctMarker.setColOff(0);
ctMarker.setRow(0);
ctMarker.setRowOff(0);
//保存docx文件
FileOutputStream fos = new FileOutputStream(docxPath);
document.write(fos);
fos.close();
System.out.println("图片插入成功!");
} catch (Exception e) {
e.printStackTrace();
}
}
}
```
在这段代码中,首先打开docx文件,并根据指定的页数获取目标段落。然后,在目标段落前插入一张图片,并设置该图片的位置为垂直和水平居中。最后保存docx文件即可。
阅读全文