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

關(guān)于使用POI向word中添加圖片的問題

 更新時(shí)間:2022年12月23日 10:17:04   作者:acmbb  
這篇文章主要介紹了關(guān)于使用POI向word中添加圖片的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

使用POI向word中添加圖片

由于一次需要向word中添加多張圖片,其中有圖片存在重復(fù),一開始使用的創(chuàng)建圖片代碼為:

xwpf.createPicture(xwpf.getAllPictures().size()-1, 80, 30,pargraph);?
public void createPicture(int id, int width, int height,XWPFParagraph paragraph) { ?
? ? ? ? final int EMU = 9525; ?
? ? ? ? width *= EMU; ?
? ? ? ? height *= EMU; ?
? ? ? ? String blipId = getAllPictures().get(id).getPackageRelationship().getId(); ?
? ? ? ? CTInline inline = paragraph.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); ?
? ? ? ? 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"); ?
? ? } ?

上述代碼對(duì)于重復(fù)的圖片流不會(huì)第二次生成id,因此會(huì)造成第二次出現(xiàn)的圖片被后續(xù)圖片覆蓋的情況。

因此,修改為如下處理方式,解決了重復(fù)圖片的問題:

String ind = xwpf.addPictureData(is, XWPFDocument.PICTURE_TYPE_GIF);
int id = ?xwpf.getNextPicNameNumber(XWPFDocument.PICTURE_TYPE_GIF);
xwpf.createPicture(ind, id, 80, 30,pargraph);?
public void createPicture(String blipId, int id, int width, int height,XWPFParagraph paragraph) { ?
? ? ? ? final int EMU = 9525; ?
? ? ? ? width *= EMU; ?
? ? ? ? height *= EMU; ?
? ? ? ? //String blipId = getAllPictures().get(id).getPackageRelationship().getId(); ?
? ? ? ? CTInline inline = paragraph.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); ?
? ? ? ? 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"); ?
? ? } ?

使用POI給Word添加水印

Maven 引入依賴

? ? ? ?<dependency>
? ? ? ? ? ? <groupId>org.apache.poi</groupId>
? ? ? ? ? ? <artifactId>poi</artifactId>
? ? ? ? ? ? <version>3.17</version>
? ? ? ? </dependency>
? ? ? ??
? ? ? ? <dependency>
? ? ? ? ? ? <groupId>org.apache.poi</groupId>
? ? ? ? ? ? <artifactId>poi-ooxml</artifactId>
? ? ? ? ? ? <version>3.17</version>
? ? ? ? </dependency>

Java 代碼:

package com.daydayup.study001.watermark;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import org.apache.poi.xwpf.model.XWPFHeaderFooterPolicy;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFHeader;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;

public class WatermarkForWord {

    public static void main(String[] args) throws FileNotFoundException, IOException {
        XWPFDocument doc= new XWPFDocument();

          // the body content
          XWPFParagraph paragraph = doc.createParagraph();
          XWPFRun run=paragraph.createRun();  
          run.setText("The Body:");

          // create header-footer
          XWPFHeaderFooterPolicy headerFooterPolicy = doc.getHeaderFooterPolicy();
          if (headerFooterPolicy == null) headerFooterPolicy = doc.createHeaderFooterPolicy();

          // create default Watermark - fill color black and not rotated
          headerFooterPolicy.createWatermark("Watermark");

          // get the default header
          // Note: createWatermark also sets FIRST and EVEN headers 
          // but this code does not updating those other headers
          XWPFHeader header = headerFooterPolicy.getHeader(XWPFHeaderFooterPolicy.DEFAULT);
          paragraph = header.getParagraphArray(0);

          // get com.microsoft.schemas.vml.CTShape where fill color and rotation is set
          org.apache.xmlbeans.XmlObject[] xmlobjects = paragraph.getCTP().getRArray(0).getPictArray(0).selectChildren(
            new javax.xml.namespace.QName("urn:schemas-microsoft-com:vml", "shape"));

          if (xmlobjects.length > 0) {
           com.microsoft.schemas.vml.CTShape ctshape = (com.microsoft.schemas.vml.CTShape)xmlobjects[0];
           // set fill color
           ctshape.setFillcolor("#d8d8d8");
           // set rotation
           ctshape.setStyle(ctshape.getStyle() + ";rotation:315");
           //System.out.println(ctshape);
          }

          doc.write(new FileOutputStream("CreateWordHeaderFooterWatermark.docx"));
          doc.close();

    }

}

總結(jié)

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • SpringBoot之如何指定配置文件啟動(dòng)

    SpringBoot之如何指定配置文件啟動(dòng)

    這篇文章主要介紹了SpringBoot之如何指定配置文件啟動(dòng)問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。
    2023-04-04
  • springboot如何使用logback-spring配置日志格式,并分環(huán)境配置

    springboot如何使用logback-spring配置日志格式,并分環(huán)境配置

    這篇文章主要介紹了springboot如何使用logback-spring配置日志格式,并分環(huán)境配置的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-07-07
  • RabbitMQ消息隊(duì)列中多路復(fù)用Channel信道詳解

    RabbitMQ消息隊(duì)列中多路復(fù)用Channel信道詳解

    這篇文章主要介紹了RabbitMQ消息隊(duì)列中多路復(fù)用Channel信道詳解,消息Message是指在應(yīng)用間傳送的數(shù)據(jù),消息可以非常簡(jiǎn)單,比如只包含文本字符串,也可以更復(fù)雜,可能包含嵌入對(duì)象,需要的朋友可以參考下
    2023-08-08
  • 詳解Spring Boot應(yīng)用的啟動(dòng)和停止(start啟動(dòng))

    詳解Spring Boot應(yīng)用的啟動(dòng)和停止(start啟動(dòng))

    這篇文章主要介紹了詳解Spring Boot應(yīng)用的啟動(dòng)和停止(start啟動(dòng)),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2018-12-12
  • 使用Spring的StopWatch實(shí)現(xiàn)代碼性能監(jiān)控的方法詳解

    使用Spring的StopWatch實(shí)現(xiàn)代碼性能監(jiān)控的方法詳解

    在開發(fā)過程中,偶爾還是需要分析代碼的執(zhí)行時(shí)間,Spring 框架提供了一個(gè)方便的工具類 StopWatch,本文將介紹 StopWatch 的基本用法,并通過示例演示如何在項(xiàng)目中使用 StopWatch 進(jìn)行代碼性能監(jiān)控
    2023-12-12
  • 解決Idea查看源代碼警告Library source does not match the bytecode for class XXX問題

    解決Idea查看源代碼警告Library source does not mat

    在使用IDEA開發(fā)時(shí),遇到第三方j(luò)ar包中的源代碼和字節(jié)碼不一致的問題,會(huì)導(dǎo)致無法正確打斷點(diǎn)進(jìn)行調(diào)試,這通常是因?yàn)閖ar包更新后源代碼沒有同步更新造成的,解決方法是刪除舊的jar包,通過Maven重新下載或手動(dòng)下載最新的源代碼包,確保IDE中的源碼與字節(jié)碼版本一致
    2024-10-10
  • springboot整合nacos,如何讀取nacos配置文件

    springboot整合nacos,如何讀取nacos配置文件

    這篇文章主要介紹了springboot整合nacos,如何讀取nacos配置文件問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-11-11
  • SpringBoot時(shí)間格式化的方法小結(jié)

    SpringBoot時(shí)間格式化的方法小結(jié)

    SpringBoot中的時(shí)間格式化通常指的是將Java中的日期時(shí)間類型轉(zhuǎn)換為指定格式的字符串,或者將字符串類型的時(shí)間解析為Java中的日期時(shí)間類型,本文小編將給大家詳細(xì)總結(jié)了SpringBoot時(shí)間格式化的方法,剛興趣的小伙伴跟著小編一起來看看吧
    2023-10-10
  • SpringCloud聲明式Feign客戶端調(diào)用工具使用

    SpringCloud聲明式Feign客戶端調(diào)用工具使用

    這篇文章主要為大家介紹了SpringCloud聲明式Feign客戶端調(diào)用工具使用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-08-08
  • Maven插件之Dependency:analyze的使用

    Maven插件之Dependency:analyze的使用

    在軟件開發(fā)中,合理管理項(xiàng)目依賴是保證構(gòu)建穩(wěn)定性的關(guān)鍵,Maven作為流行的項(xiàng)目管理工具,提供了Dependency插件來幫助開發(fā)者分析和優(yōu)化項(xiàng)目依賴,通過執(zhí)行dependency:analyze指令,可以辨識(shí)項(xiàng)目中使用的、未聲明的、和未使用的依賴項(xiàng)
    2024-10-10

最新評(píng)論

新津县| 巴塘县| 疏勒县| 靖安县| 潼南县| 怀安县| 青浦区| 连江县| 望城县| 永仁县| 肥西县| 南通市| 马鞍山市| 中方县| 易门县| 巩留县| 天津市| 彭水| 阿拉善右旗| 原阳县| 夏津县| 营山县| 蓬溪县| 广昌县| 普兰店市| 沙湾县| 尤溪县| 惠州市| 宁夏| 金平| 永定县| 蓬安县| 尉氏县| 宝鸡市| 平谷区| 宜兰市| 凤凰县| 沐川县| 广宁县| 兴海县| 洛阳市|