您好我想要将图像插入到Word文档中。下面的代码以原始大小插入图像:
private def writePhotosToDoc(model: WordReportModel, doc: XWPFDocument): Unit = {
val paragraphIndex = detectVariable(Variables.PHOTO_APPLICATION, doc)
if (!paragraphIndex.exists(_ == ("idx", -1))) {
val taskParagraph = doc.getParagraphs.asScala(
paragraphIndex("idx")
)
taskParagraph.removeRun(
paragraphIndex("irx")
)
model.attachments.foreach{
case(key, value) =>
val p = doc.createParagraph()
p.getCTP.setPPr(taskParagraph.getCTP.getPPr)
p.setAlignment(ParagraphAlignment.CENTER)
val r = p.createRun()
r.addBreak()
val bi = ImageIO.read(value.head)
val width = bi.getWidth
val height = bi.getHeight
r.addPicture(
new FileInputStream(value.head),
Document.PICTURE_TYPE_PNG,
value.head.getName,
Units.toEMU(width),
Units.toEMU(height)
)
}
}
}
如何插入图像和设置比例。
答案 0 :(得分:2)
我看到了this线程,并试图插入不同的大小,似乎工作正常。
这是执行开始的地方。请注意,您必须首先创建一个普通的docx文件,然后编辑并添加图像(否则文件未正确创建):
XWPFDocument doc = new XWPFDocument();
doc.write(new FileOutputStream(new File("test.docx")));
CustomXWPFDocument document = new CustomXWPFDocument(new FileInputStream(new File("test.docx")));
FileOutputStream fos = new FileOutputStream(new File("test.docx"));
String blipId = document.addPictureData(new FileInputStream(new File("res/Tulips.jpg")), Document.PICTURE_TYPE_JPEG);
System.out.println(document.getNextPicNameNumber(Document.PICTURE_TYPE_JPEG));
//System.out.println(document.getNextPicNameNumber(Document.PICTURE_TYPE_JPEG));
document.createPicture(blipId,document.getNextPicNameNumber(Document.PICTURE_TYPE_JPEG), 100, 150);
document.write(fos);
fos.flush();
fos.close();
这是 CustomXWPFDocument 类(取自question):
public class CustomXWPFDocument extends XWPFDocument
{
public CustomXWPFDocument(InputStream in) throws IOException
{
super(in);
}
public void createPicture(String blipId,int id, int width, int height)
{
final int EMU = 9525;
width *= EMU;
height *= EMU;
//String blipId = getAllPictures().get(id).getPackageRelationship().getId();
CTInline inline = createParagraph().createRun().getCTR().addNewDrawing().addNewInline();
String picXml = "" +
"<a:graphic xmlns:a=\"http://schemas.openxmlformats.org/drawingml/2006/main\">" +
" <a:graphicData uri=\"http://schemas.openxmlformats.org/drawingml/2006/picture\">" +
" <pic:pic xmlns:pic=\"http://schemas.openxmlformats.org/drawingml/2006/picture\">" +
" <pic:nvPicPr>" +
" <pic:cNvPr id=\"" + id + "\" name=\"Generated\"/>" +
" <pic:cNvPicPr/>" +
" </pic:nvPicPr>" +
" <pic:blipFill>" +
" <a:blip r:embed=\"" + blipId + "\" xmlns:r=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships\"/>" +
" <a:stretch>" +
" <a:fillRect/>" +
" </a:stretch>" +
" </pic:blipFill>" +
" <pic:spPr>" +
" <a:xfrm>" +
" <a:off x=\"0\" y=\"0\"/>" +
" <a:ext cx=\"" + width + "\" cy=\"" + height + "\"/>" +
" </a:xfrm>" +
" <a:prstGeom prst=\"rect\">" +
" <a:avLst/>" +
" </a:prstGeom>" +
" </pic:spPr>" +
" </pic:pic>" +
" </a:graphicData>" +
"</a:graphic>";
//CTGraphicalObjectData graphicData = inline.addNewGraphic().addNewGraphicData();
XmlToken xmlToken = null;
try
{
xmlToken = XmlToken.Factory.parse(picXml);
}
catch(XmlException xe)
{
xe.printStackTrace();
}
inline.set(xmlToken);
//graphicData.set(xmlToken);
inline.setDistT(0);
inline.setDistB(0);
inline.setDistL(0);
inline.setDistR(0);
CTPositiveSize2D extent = inline.addNewExtent();
extent.setCx(width);
extent.setCy(height);
CTNonVisualDrawingProps docPr = inline.addNewDocPr();
docPr.setId(id);
docPr.setName("Picture " + id);
docPr.setDescr("Generated");
}
}