最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

java poi設(shè)置生成的word的圖片為上下型環(huán)繞以及其位置的實現(xiàn)

 更新時間:2019年09月06日 09:13:40   作者:十三  
這篇文章主要介紹了java poi設(shè)置生成的word的圖片為上下型環(huán)繞以及其位置的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧

問題描述

在使用poi-tl word模版工具時,發(fā)現(xiàn)生成的文檔中,圖片格式為嵌入型,有的圖片甚至被表格遮擋一半。而自己想要的圖片格式為上下型環(huán)繞,并且圖片需要居中。

問題分析

poi-tl渲染圖片,使用的是org.apache.poi.xwpf.usermodel.XWPFRun的addPicture方法,該方法中有一段代碼:CTInline inline = drawing.addNewInline();意思就是默認將圖片轉(zhuǎn)為inline類型,即行內(nèi)元素。

然后我們把生成的嵌入型圖片的文檔轉(zhuǎn)換成xml文件,然后再新建一個文檔,插入圖片后,設(shè)置圖片為上下型環(huán)繞,保存為另一個xml,比較下兩個xml的區(qū)別。嵌入型圖片的xml是:


上下型環(huán)繞的圖片的xml是

我們看到兩種格式的圖片標簽分別為inline和anchor。所以如果我們想把圖片設(shè)置為上下型環(huán)繞,需要重寫poi的addPicture方法,把圖片轉(zhuǎn)為anchor類型。

我們仿照org.apache.poi.xwpf.usermodel.XWPFRun的addPicture方法,將CTInline inline = drawing.addNewInline();換成 CTAnchor anchor = drawing.addNewAnchor();,然后對比著xml,依次對anchor的字段進行賦值。結(jié)果發(fā)現(xiàn)生成的word無法正常打開,查了很多資料,都說poi的CTAnchor有問題,使用后無法正常打開生成的word。

此路不通,那我們就嘗試另一種思路,我們不通過CTAnchor來生成anchor標簽,而是直接使用xml,將xml賦給poi的drawing。具體的處理方式在后面。

xml標簽和圖片格式解析

在word中,在圖片上右鍵,選擇大小和位置,就可以看到如下界面:


圖中的上下型對應的是xml中的<wp:wrapTopAndBottom/>標簽,不同環(huán)繞方式該標簽值不一樣。如果需要其他格式,可以設(shè)置好后,把文檔保存為xml,找到對應的標簽。

圖中的距正文上下左右距離,對應的是<wp:anchor distT="71755" distB="71755" distL="114300" distR="114300" ...>中的disT、disB、disL、disR屬性。


圖中位置一欄,水平對齊方式居中、相對于欄對應的是xml中的<wp:positionH relativeFrom="column"><wp:align>center</wp:align></wp:positionH>。

垂直-絕對位置0.1cm,下側(cè)段落對應的是xml中的<wp:positionV relativeFrom="paragraph"><wp:posOffset>36195</wp:posOffset></wp:positionV>。

我們可以根據(jù)不同的需要來設(shè)置不同的xml。

我使用的xml是

 String xml = "<wp:anchor allowOverlap=\"0\" layoutInCell=\"1\" locked=\"0\" behindDoc=\"0\" relativeHeight=\"0\" 
        simplePos=\"0\" distR=\"0\" distL=\"0\" distB=\"0\" distT=\"0\" " +
        " xmlns:wp=\"http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing\"" +
        " xmlns:wp14=\"http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing\"" +
        " xmlns:r=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships\" >" +
        "<wp:simplePos y=\"0\" x=\"0\"/>" +
        "<wp:positionH relativeFrom=\"column\">" +
        "<wp:align>center</wp:align>" +
        "</wp:positionH>" +
        "<wp:positionV relativeFrom=\"paragraph\">" +
        "<wp:posOffset>0</wp:posOffset>" +
        "</wp:positionV>" +
        "<wp:extent cy=\""+height+"\" cx=\""+width+"\"/>" +
        "<wp:effectExtent b=\"0\" r=\"0\" t=\"0\" l=\"0\"/>" +
        "<wp:wrapTopAndBottom/>" +
        "<wp:docPr descr=\"Picture Alt\" name=\"Picture Hit\" id=\"0\"/>" +
        "<wp:cNvGraphicFramePr>" +
        "<a:graphicFrameLocks noChangeAspect=\"true\" xmlns:a=\"http://schemas.openxmlformats.org/drawingml/2006/main\" />" +
        "</wp:cNvGraphicFramePr>" +
        "<a:graphic xmlns:a=\"http://schemas.openxmlformats.org/drawingml/2006/main\">" +
        "<a:graphicData uri=\"http://schemas.openxmlformats.org/drawingml/2006/picture\" xmlns:a=\"http://schemas.openxmlformats.org/drawingml/2006/main\">" +
        "<pic:pic xmlns:pic=\"http://schemas.openxmlformats.org/drawingml/2006/picture\">" +
        "<pic:nvPicPr>" +
        "<pic:cNvPr name=\"Picture Hit\" id=\"1\"/>" +
        "<pic:cNvPicPr/>" +
        "</pic:nvPicPr>" +
        "<pic:blipFill>" +
        "<a:blip r:embed=\""+relationId+"\"/>" +
        "<a:stretch>" +
        "<a:fillRect/>" +
        "</a:stretch>" +
        "</pic:blipFill>" +
        "<pic:spPr>" +
        "<a:xfrm>" +
        "<a:off y=\"0\" x=\"0\"/>" +
        "<a:ext cy=\""+height+"\" cx=\""+width+"\"/>" +
        "</a:xfrm>" +
        "<a:prstGeom prst=\"rect\">" +
        "<a:avLst/>" +
        "</a:prstGeom>" +
        "</pic:spPr>" +
        "</pic:pic>" +
        "</a:graphicData>" +
        "</a:graphic>" +
        "<wp14:sizeRelH relativeFrom=\"margin\">" +
        "<wp14:pctWidth>0</wp14:pctWidth>" +
        "</wp14:sizeRelH>" +
        "<wp14:sizeRelV relativeFrom=\"margin\">" +
        "<wp14:pctHeight>0</wp14:pctHeight>" +
        "</wp14:sizeRelV>" +
        "</wp:anchor>";
        

其中width和height是圖片的寬度和高度,relationId是圖片的id。

解決方案

1,首先定義一個poi-tl的圖片渲染器,使得其不再調(diào)用poi默認的圖片渲染器,而是使用我們自己定義的。

public class MyPictureRenderPolicy extends AbstractRenderPolicy<PictureRenderData> {
@Override
protected boolean validate(PictureRenderData data) {
  return (null != data.getData() || null != data.getPath());
}

@Override
public void doRender(RunTemplate runTemplate, PictureRenderData picture, XWPFTemplate template)
    throws Exception {
  XWPFRun run = runTemplate.getRun();
  MyPictureRenderPolicy.Helper.renderPicture(run, picture);
}

@Override
protected void afterRender(RenderContext context) {
  clearPlaceholder(context, false);
}

@Override
protected void doRenderException(RunTemplate runTemplate, PictureRenderData data, Exception e) {
  logger.info("Render picture " + runTemplate + " error: {}", e.getMessage());
  runTemplate.getRun().setText(data.getAltMeta(), 0);
}

public static class Helper {
  public static void renderPicture(XWPFRun run, PictureRenderData picture) throws Exception {
    int suggestFileType = suggestFileType(picture.getPath());
    InputStream ins = null == picture.getData() ? new FileInputStream(picture.getPath())
        : new ByteArrayInputStream(picture.getData());

    String relationId = run.getDocument().addPictureData(ins, suggestFileType);
    long width = Units.toEMU(picture.getWidth());
    long height = Units.toEMU(picture.getHeight());
    CTDrawing drawing = run.getCTR().addNewDrawing();
    String xml = "<wp:anchor allowOverlap=\"0\" layoutInCell=\"1\" locked=\"0\" behindDoc=\"0\" relativeHeight=\"0\" simplePos=\"0\" distR=\"0\" distL=\"0\" distB=\"0\" distT=\"0\" " +
        " xmlns:wp=\"http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing\"" +
        " xmlns:wp14=\"http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing\"" +
        " xmlns:r=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships\" >" +
        "<wp:simplePos y=\"0\" x=\"0\"/>" +
        "<wp:positionH relativeFrom=\"column\">" +
        "<wp:align>center</wp:align>" +
        "</wp:positionH>" +
        "<wp:positionV relativeFrom=\"paragraph\">" +
        "<wp:posOffset>0</wp:posOffset>" +
        "</wp:positionV>" +
        "<wp:extent cy=\""+height+"\" cx=\""+width+"\"/>" +
        "<wp:effectExtent b=\"0\" r=\"0\" t=\"0\" l=\"0\"/>" +
        "<wp:wrapTopAndBottom/>" +
        "<wp:docPr descr=\"Picture Alt\" name=\"Picture Hit\" id=\"0\"/>" +
        "<wp:cNvGraphicFramePr>" +
        "<a:graphicFrameLocks noChangeAspect=\"true\" xmlns:a=\"http://schemas.openxmlformats.org/drawingml/2006/main\" />" +
        "</wp:cNvGraphicFramePr>" +
        "<a:graphic xmlns:a=\"http://schemas.openxmlformats.org/drawingml/2006/main\">" +
        "<a:graphicData uri=\"http://schemas.openxmlformats.org/drawingml/2006/picture\" xmlns:a=\"http://schemas.openxmlformats.org/drawingml/2006/main\">" +
        "<pic:pic xmlns:pic=\"http://schemas.openxmlformats.org/drawingml/2006/picture\">" +
        "<pic:nvPicPr>" +
        "<pic:cNvPr name=\"Picture Hit\" id=\"1\"/>" +
        "<pic:cNvPicPr/>" +
        "</pic:nvPicPr>" +
        "<pic:blipFill>" +
        "<a:blip r:embed=\""+relationId+"\"/>" +
        "<a:stretch>" +
        "<a:fillRect/>" +
        "</a:stretch>" +
        "</pic:blipFill>" +
        "<pic:spPr>" +
        "<a:xfrm>" +
        "<a:off y=\"0\" x=\"0\"/>" +
        "<a:ext cy=\""+height+"\" cx=\""+width+"\"/>" +
        "</a:xfrm>" +
        "<a:prstGeom prst=\"rect\">" +
        "<a:avLst/>" +
        "</a:prstGeom>" +
        "</pic:spPr>" +
        "</pic:pic>" +
        "</a:graphicData>" +
        "</a:graphic>" +
        "<wp14:sizeRelH relativeFrom=\"margin\">" +
        "<wp14:pctWidth>0</wp14:pctWidth>" +
        "</wp14:sizeRelH>" +
        "<wp14:sizeRelV relativeFrom=\"margin\">" +
        "<wp14:pctHeight>0</wp14:pctHeight>" +
        "</wp14:sizeRelV>" +
        "</wp:anchor>";
    drawing.set(XmlToken.Factory.parse(xml, DEFAULT_XML_OPTIONS));
    CTPicture pic = getCTPictures(drawing).get(0);
    XWPFPicture xwpfPicture = new XWPFPicture(pic, run);
    run.getEmbeddedPictures().add(xwpfPicture);
  }


  public static List<CTPicture> getCTPictures(XmlObject o) {
    List<CTPicture> pictures = new ArrayList<>();
    XmlObject[] picts = o.selectPath("declare namespace pic='"
        + CTPicture.type.getName().getNamespaceURI() + "' .//pic:pic");
    for (XmlObject pict : picts) {
      if (pict instanceof XmlAnyTypeImpl) {
        // Pesky XmlBeans bug - see Bugzilla #49934
        try {
          pict = CTPicture.Factory.parse(pict.toString(),
              DEFAULT_XML_OPTIONS);
        } catch (XmlException e) {
          throw new POIXMLException(e);
        }
      }
      if (pict instanceof CTPicture) {
        pictures.add((CTPicture) pict);
      }
    }
    return pictures;
  }


  public static int suggestFileType(String imgFile) {
    int format = 0;
    if (imgFile.endsWith(".emf")) {
      format = XWPFDocument.PICTURE_TYPE_EMF;
    } else if (imgFile.endsWith(".wmf")) {
      format = XWPFDocument.PICTURE_TYPE_WMF;
    } else if (imgFile.endsWith(".pict")) {
      format = XWPFDocument.PICTURE_TYPE_PICT;
    } else if (imgFile.endsWith(".jpeg") || imgFile.endsWith(".jpg")) {
      format = XWPFDocument.PICTURE_TYPE_JPEG;
    } else if (imgFile.endsWith(".png")) {
      format = XWPFDocument.PICTURE_TYPE_PNG;
    } else if (imgFile.endsWith(".dib")) {
      format = XWPFDocument.PICTURE_TYPE_DIB;
    } else if (imgFile.endsWith(".gif")) {
      format = XWPFDocument.PICTURE_TYPE_GIF;
    } else if (imgFile.endsWith(".tiff")) {
      format = XWPFDocument.PICTURE_TYPE_TIFF;
    } else if (imgFile.endsWith(".eps")) {
      format = XWPFDocument.PICTURE_TYPE_EPS;
    } else if (imgFile.endsWith(".bmp")) {
      format = XWPFDocument.PICTURE_TYPE_BMP;
    } else if (imgFile.endsWith(".wpg")) {
      format = XWPFDocument.PICTURE_TYPE_WPG;
    } else {
      throw new RenderException(
          "Unsupported picture: " + imgFile + ". Expected emf|wmf|pict|jpeg|png|dib|gif|tiff|eps|bmp|wpg");
    }
    return format;
  }

}
}

然后在渲染模板的時候,配置我們自己定義的圖片渲染器

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

  String path = "1.docx";
  InputStream templateFile = Demo.class.getClassLoader().getResourceAsStream(path);
  Map map = new HashMap();
  map.put("pic", new PictureRenderData(120, 80, ".png", Demo.class.getClassLoader().getResourceAsStream("1.png")));


  // 將數(shù)據(jù)整合到模板中去
  Configure.ConfigureBuilder builder = Configure.newBuilder();
  builder.supportGrammerRegexForAll();
  builder.addPlugin('@', new MyPictureRenderPolicy());
  XWPFTemplate template = XWPFTemplate.compile(templateFile, builder.build()).render(map);

  String docPath = "C:\\Users\\csdc01\\Desktop\\out.docx";
  FileOutputStream outputStream1 = new FileOutputStream(docPath);
  template.write(outputStream1);
  outputStream1.flush();
  outputStream1.close();
}

源碼:https://github.com/ksyzz/poi-tl-demo.git

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • 我勸你謹慎使用Spring中的@Scheduled注解

    我勸你謹慎使用Spring中的@Scheduled注解

    這篇文章主要介紹了Spring中的@Scheduled注解使用,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-10-10
  • spring整合JMS實現(xiàn)同步收發(fā)消息(基于ActiveMQ的實現(xiàn))

    spring整合JMS實現(xiàn)同步收發(fā)消息(基于ActiveMQ的實現(xiàn))

    本篇文章主要介紹了spring整合JMS實現(xiàn)同步收發(fā)消息(基于ActiveMQ的實現(xiàn)),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-10-10
  • java-collection中的null,isEmpty用法

    java-collection中的null,isEmpty用法

    這篇文章主要介紹了java-collection中的null,isEmpty用法,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-02-02
  • Java線程實現(xiàn)時間動態(tài)顯示

    Java線程實現(xiàn)時間動態(tài)顯示

    這篇文章主要為大家詳細介紹了Java線程實現(xiàn)時間動態(tài)顯示,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-04-04
  • Java開發(fā)環(huán)境配置及Vscode搭建過程

    Java開發(fā)環(huán)境配置及Vscode搭建過程

    今天通過圖文并茂的形式給大家介紹Java開發(fā)環(huán)境配置及Vscode搭建過程,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友參考下吧
    2021-07-07
  • Mybatis結(jié)果集映射一對多簡單入門教程

    Mybatis結(jié)果集映射一對多簡單入門教程

    本文給大家介紹Mybatis結(jié)果集映射一對多簡單入門教程,包括搭建數(shù)據(jù)庫環(huán)境的過程,idea搭建maven項目的代碼詳解,本文通過實例代碼給大家介紹的非常詳細,需要的朋友參考下吧
    2021-06-06
  • java9的JShell小工具和編譯器兩種自動優(yōu)化方法

    java9的JShell小工具和編譯器兩種自動優(yōu)化方法

    這篇文章主要介紹了java9的JShell小工具和編譯器兩種自動優(yōu)化方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2019-07-07
  • springboot+mybatis通過實體類自動生成數(shù)據(jù)庫表的方法

    springboot+mybatis通過實體類自動生成數(shù)據(jù)庫表的方法

    這篇文章主要介紹了springboot+mybatis通過實體類自動生成數(shù)據(jù)庫表的方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-07-07
  • Java中Servlet的生命周期詳解

    Java中Servlet的生命周期詳解

    這篇文章主要介紹了Java中Servlet的生命周期詳解,文章圍繞主題展開詳細的內(nèi)容介紹,具有一定的參考價值,需要的小伙伴可以參考一下
    2022-08-08
  • Intellij IDEA十大快捷鍵

    Intellij IDEA十大快捷鍵

    Intellij IDEA中有很多快捷鍵讓人愛不釋手,stackoverflow上也有一些有趣的討論.這篇文章主要介紹了Intellij IDEA十大快捷鍵,需要的朋友可以參考下
    2018-03-03

最新評論

临沭县| 武义县| 海阳市| 宜兰市| 大邑县| 丰宁| 荥阳市| 镇江市| 宁都县| 昭苏县| 沂水县| 鲁山县| 仁寿县| 阳西县| 石泉县| 吉首市| 凤台县| 天台县| 介休市| 堆龙德庆县| 厦门市| 宝坻区| 潼南县| 抚松县| 雷山县| 扬州市| 察隅县| 莱西市| 明星| 潜江市| 上栗县| 东乌| 沙洋县| 湖州市| 荣昌县| 威宁| 宜阳县| 达州市| 井陉县| 芜湖县| 郓城县|