欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

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

使用POI向word中添加圖片

由于一次需要向word中添加多張圖片,其中有圖片存在重復,一開始使用的創(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"); ?
? ? } ?

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

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

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)文章

  • Java中的什么場景使用遞歸,如何使用遞歸

    Java中的什么場景使用遞歸,如何使用遞歸

    這篇文章主要介紹了Java中的什么場景使用遞歸,如何使用遞歸的操作,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-09-09
  • Spring框架開發(fā)IOC兩種創(chuàng)建工廠方法詳解

    Spring框架開發(fā)IOC兩種創(chuàng)建工廠方法詳解

    這篇文章主要介紹了Spring框架IOC兩種創(chuàng)建工廠方法詳解,文中附含詳細的代碼示例分別對靜態(tài)方法和實例方法創(chuàng)建工廠作了簡要的分析
    2021-09-09
  • 哲學家就餐問題中的JAVA多線程學習

    哲學家就餐問題中的JAVA多線程學習

    哲學家就餐問題是1965年由Dijkstra提出的一種線程同步的問題,下面我們就看一下JAVA多線程如何做
    2013-11-11
  • netflix.discovery.shared.transport.TransportException:Cannot execute request on any known server

    netflix.discovery.shared.transport.TransportException:Cannot

    這篇文章主要介紹了netflix.discovery.shared.transport.TransportException:Cannot execute request on any known server報錯問題及解決方法,感興趣的朋友一起看看吧
    2023-09-09
  • Java多線程之線程安全問題詳細解析

    Java多線程之線程安全問題詳細解析

    這篇文章主要給大家介紹了關(guān)于Java多線程之線程安全問題的相關(guān)資料,Java多線程中線程安全問題是一個常見的問題,因為多個線程可能同時訪問共享的資源,文中通過圖文介紹的非常詳細,需要的朋友可以參考下
    2023-11-11
  • Android讀取本地或網(wǎng)絡圖片并轉(zhuǎn)換為Bitmap

    Android讀取本地或網(wǎng)絡圖片并轉(zhuǎn)換為Bitmap

    這篇文章主要為大家詳細介紹了Android讀取本地或網(wǎng)絡圖片,并轉(zhuǎn)換為Bitmap,感興趣的小伙伴們可以參考一下
    2016-08-08
  • idea中創(chuàng)建jsp項目的詳細實戰(zhàn)步驟

    idea中創(chuàng)建jsp項目的詳細實戰(zhàn)步驟

    才學javaWeb,以防自己忘記創(chuàng)建項目的過程,所以淺淺的記錄一下吧,下面這篇文章主要給大家介紹了關(guān)于idea中創(chuàng)建jsp項目的詳細步驟,文中通過圖文介紹的非常詳細,需要的朋友可以參考下
    2022-09-09
  • 關(guān)于maven環(huán)境的安裝及maven集成idea環(huán)境的問題

    關(guān)于maven環(huán)境的安裝及maven集成idea環(huán)境的問題

    Maven 是一個基于 Java 的工具,所以要做的第一件事情就是安裝 JDK。本文重點給大家介紹關(guān)于maven環(huán)境的安裝及和idea環(huán)境的集成問題,感興趣的朋友一起看看吧
    2021-09-09
  • Java集合教程之Collection實例詳解

    Java集合教程之Collection實例詳解

    集合,或者叫容器,是一個包含多個元素的對象,下面這篇文章主要給大家介紹了關(guān)于Java集合教程之Collection的相關(guān)資料,文中通過示例代碼介紹的非常詳細,需要的朋友可以參考下
    2018-08-08
  • Springcloud Config支持本地配置文件的方法示例

    Springcloud Config支持本地配置文件的方法示例

    這篇文章主要介紹了Springcloud Config支持本地配置文件的方法示例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-02-02

最新評論