91超碰碰碰碰久久久久久综合_超碰av人澡人澡人澡人澡人掠_国产黄大片在线观看画质优化_txt小说免费全本

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

如何使用POI向word中添加圖片

發布時間:2022-12-27 17:25:59 來源:億速云 閱讀:731 作者:iii 欄目:開發技術

本篇內容介紹了“如何使用POI向word中添加圖片”的有關知識,在實際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓小編帶領大家學習一下如何處理這些情況吧!希望大家仔細閱讀,能夠學有所成!

使用POI向word中添加圖片

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

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,因此會造成第二次出現的圖片被后續圖片覆蓋的情況。

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

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();

    }

}

“如何使用POI向word中添加圖片”的內容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業相關的知識可以關注億速云網站,小編將為大家輸出更多高質量的實用文章!

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

浦县| 曲水县| 白沙| 兰溪市| 滁州市| 连南| 措勤县| 宜宾县| 南丹县| 玉门市| 彭阳县| 上思县| 集贤县| 堆龙德庆县| 黔江区| 西青区| 伊川县| 宽甸| 永吉县| 廉江市| 那曲县| 和龙市| 黔西| 哈尔滨市| 巴东县| 楚雄市| 桐庐县| 南城县| 德江县| 张北县| 若羌县| 陵川县| 昆明市| 博罗县| 应城市| 平舆县| 洞头县| 华阴市| 天镇县| 凭祥市| 辛集市|