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

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

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

使用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"); ?
? ? } ?

上述代碼對于重復(fù)的圖片流不會第二次生成id,因此會造成第二次出現(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é)

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

相關(guān)文章

  • SpringBoot之如何指定配置文件啟動

    SpringBoot之如何指定配置文件啟動

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    Maven插件之Dependency:analyze的使用

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

最新評論

南乐县| 宣城市| 常山县| 石渠县| 贵德县| 苍溪县| 古丈县| 凤城市| 连城县| 通江县| 瓦房店市| 广西| 黄石市| 平顺县| 探索| 屏东市| 铅山县| 泰安市| 西和县| 正镶白旗| 容城县| 屏山县| 专栏| 卢湾区| 茂名市| 当涂县| 新乡市| 惠州市| 东阿县| 周至县| 南丹县| 沙洋县| 贵港市| 巴林左旗| 剑河县| 瑞安市| 东城区| 会宁县| 旌德县| 马龙县| 迁西县|