猿问

如何使用 Apache POI 锁定 docx 中图像的纵横比?

有没有办法使用 Apache POI 为 docx 中的图像启用“锁定纵横比”选项?我正在将图像添加到运行中,并且我想锁定它的大小。在文档中搜索但没有找到。

为了更清楚,我指的是 word 中可用的选项,如图所示: https: //support.content.office.net/en-us/media/a30a8baa-6775-4931-bca6-199fda6afc6e.png


狐的传说
浏览 285回答 1
1回答

芜湖不芜

答案取决于图片如何应用于Word文档。如果这是通过XWPFRun.addPicture完成的,则它是文本运行中的内嵌图片。然后运行包含一个绘图层,其中包含一个包含图片的内联元素。然后,此内联元素可能包含非可视图形框架属性,这些属性可能具有将无变化方面设置为真的图形框架锁定。在看起来像XML:/word/document.xml<w:r>&nbsp;<w:drawing>&nbsp; <wp:inline ...>&nbsp; &nbsp;<wp:cNvGraphicFramePr><a:graphicFrameLocks noChangeAspect="true"/></wp:cNvGraphicFramePr>&nbsp; &nbsp; <a:graphic>&nbsp; &nbsp;...然后对于XWPFRun run包含图片可以设置run.getCTR().getDrawingArray(0).getInlineArray(0).addNewCNvGraphicFramePr().addNewGraphicFrameLocks().setNoChangeAspect(true);完整示例:import java.io.*;import org.apache.poi.xwpf.usermodel.*;import org.apache.poi.util.Units;import java.util.List;import java.util.ArrayList;import java.net.URL;import javax.imageio.ImageIO;import java.awt.image.BufferedImage;import java.awt.Dimension;public class CreateWordPicturesInTextRuns {&nbsp;public static void main(String[] args) throws Exception {&nbsp; List<String> pictureURLs = new ArrayList<String>();&nbsp; pictureURLs.add("https://www.eastcottvets.co.uk/uploads/Animals/gingerkitten.jpg");&nbsp; pictureURLs.add("https://www.catster.com/wp-content/uploads/2017/12/A-kitten-meowing.jpg");&nbsp; pictureURLs.add("https://www.animalfriends.co.uk/app/uploads/2014/08/06110347/Kitten-small.jpg");&nbsp; pictureURLs.add("https://d27ucmmhxk51xv.cloudfront.net/media/english/illustration/kitten.jpg");&nbsp; XWPFDocument document= new XWPFDocument();&nbsp; XWPFParagraph paragraph = document.createParagraph();&nbsp; XWPFRun run = paragraph.createRun();&nbsp; run.setText("The kitten pictures: ");&nbsp; URL url;&nbsp; BufferedImage image;&nbsp; Dimension dim;&nbsp; ByteArrayOutputStream bout;&nbsp; ByteArrayInputStream bin;&nbsp; for (String pictureURL : pictureURLs) {&nbsp; &nbsp;try {&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; url = new URL(pictureURL);&nbsp; &nbsp; image = ImageIO.read(url);&nbsp; &nbsp; dim = new Dimension(image.getWidth(), image.getHeight());&nbsp; &nbsp; double width = dim.getWidth();&nbsp; &nbsp; double height = dim.getHeight();&nbsp; &nbsp; double scaling = 1.0;&nbsp; &nbsp; if (width > 72*3) scaling = (72*3)/width; //scale width not to be greater than 3 inches&nbsp; &nbsp; bout = new ByteArrayOutputStream();&nbsp; &nbsp; ImageIO.write(image, "jpeg", bout);&nbsp; &nbsp; bout.flush();&nbsp; &nbsp; bin = new ByteArrayInputStream(bout.toByteArray());&nbsp; &nbsp; run = paragraph.createRun();&nbsp; &nbsp; run.addPicture(bin, XWPFDocument.PICTURE_TYPE_JPEG, "kitten",&nbsp;&nbsp; &nbsp; &nbsp;Units.toEMU(width*scaling), Units.toEMU(height*scaling));&nbsp; &nbsp; //lock aspect ratio&nbsp; &nbsp; run.getCTR().getDrawingArray(0).getInlineArray(0).addNewCNvGraphicFramePr().addNewGraphicFrameLocks().setNoChangeAspect(true);&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;} catch (Exception ex) {&nbsp; &nbsp; ex.printStackTrace();&nbsp; &nbsp;}&nbsp; }&nbsp; FileOutputStream out = new FileOutputStream("CreateWordPicturesInTextRuns.docx");&nbsp; document.write(out);&nbsp; out.close();&nbsp; document.close();&nbsp;}}
随时随地看视频慕课网APP

相关分类

Java
我要回答