亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何使用 Apache POI 鎖定 docx 中圖像的縱橫比?

如何使用 Apache POI 鎖定 docx 中圖像的縱橫比?

狐的傳說 2022-10-07 16:24:42
有沒有辦法使用 Apache POI 為 docx 中的圖像啟用“鎖定縱橫比”選項?我正在將圖像添加到運行中,并且我想鎖定它的大小。在文檔中搜索但沒有找到。為了更清楚,我指的是 word 中可用的選項,如圖所示: https: //support.content.office.net/en-us/media/a30a8baa-6775-4931-bca6-199fda6afc6e.png
查看完整描述

1 回答

?
蕪湖不蕪

TA貢獻1796條經驗 獲得超7個贊

答案取決于圖片如何應用于Word文檔。如果這是通過XWPFRun.addPicture完成的,則它是文本運行中的內嵌圖片。然后運行包含一個繪圖層,其中包含一個包含圖片的內聯元素。然后,此內聯元素可能包含非可視圖形框架屬性,這些屬性可能具有將無變化方面設置為真的圖形框架鎖定。


在看起來像XML:/word/document.xml


<w:r>

 <w:drawing>

  <wp:inline ...>

   <wp:cNvGraphicFramePr><a:graphicFrameLocks noChangeAspect="true"/></wp:cNvGraphicFramePr>

    <a:graphic>

   ...

然后對于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 {


 public static void main(String[] args) throws Exception {


  List<String> pictureURLs = new ArrayList<String>();

  pictureURLs.add("https://www.eastcottvets.co.uk/uploads/Animals/gingerkitten.jpg");

  pictureURLs.add("https://www.catster.com/wp-content/uploads/2017/12/A-kitten-meowing.jpg");

  pictureURLs.add("https://www.animalfriends.co.uk/app/uploads/2014/08/06110347/Kitten-small.jpg");

  pictureURLs.add("https://d27ucmmhxk51xv.cloudfront.net/media/english/illustration/kitten.jpg");


  XWPFDocument document= new XWPFDocument();

  XWPFParagraph paragraph = document.createParagraph();

  XWPFRun run = paragraph.createRun();

  run.setText("The kitten pictures: ");


  URL url;

  BufferedImage image;

  Dimension dim;

  ByteArrayOutputStream bout;

  ByteArrayInputStream bin;

  for (String pictureURL : pictureURLs) {

   try {

       

    url = new URL(pictureURL);

    image = ImageIO.read(url);

    dim = new Dimension(image.getWidth(), image.getHeight());

    double width = dim.getWidth();

    double height = dim.getHeight();

    double scaling = 1.0;

    if (width > 72*3) scaling = (72*3)/width; //scale width not to be greater than 3 inches


    bout = new ByteArrayOutputStream();

    ImageIO.write(image, "jpeg", bout);

    bout.flush();

    bin = new ByteArrayInputStream(bout.toByteArray());

    run = paragraph.createRun();

    run.addPicture(bin, XWPFDocument.PICTURE_TYPE_JPEG, "kitten", 

     Units.toEMU(width*scaling), Units.toEMU(height*scaling));


    //lock aspect ratio

    run.getCTR().getDrawingArray(0).getInlineArray(0).addNewCNvGraphicFramePr().addNewGraphicFrameLocks().setNoChangeAspect(true);

    

   } catch (Exception ex) {

    ex.printStackTrace();

   }

  }


  FileOutputStream out = new FileOutputStream("CreateWordPicturesInTextRuns.docx");

  document.write(out);

  out.close();

  document.close();


 }

}


查看完整回答
反對 回復 2022-10-07
  • 1 回答
  • 0 關注
  • 326 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號