NPOI生成word浮動圖標

1、NPOI版本2.7.0, net框架4.8

2、安裝OpenXMLSDKToolV25.msi

3、先創建一個word文檔,并設置圖片為浮于文字之上

4、OpenXML顯示的結果

5、實際代碼如下:

public class GenerateWordDemo
{public GenerateWordDemo(){}//https://blog.fileformat.com/zh/word-processing/npoi-api-for-processing-word-documents-in-net-an-overview///https://blog.csdn.net/sD7O95O/article/details/114108985//https://blog.csdn.net/qq_38544249/article/details/124923848private void SetPage(XWPFDocument doc){// 設置頁面的大小CT_SectPr m_SectPr = new CT_SectPr();       //實例一個尺寸類的實例m_SectPr.pgSz.w = 11906;        //設置寬度(這里是一個ulong類型)m_SectPr.pgSz.h = 16838;        //設置高度(這里是一個ulong類型)                    //頁面邊距m_SectPr.pgMar.left = (ulong)80;//左邊距m_SectPr.pgMar.right = (ulong)80;//右邊距m_SectPr.pgMar.top = 80;//上邊距m_SectPr.pgMar.bottom = 80;//下邊距doc.Document.body.sectPr = m_SectPr;          //設置頁面的尺寸}/// <summary>/// 加入內聯二維碼/// </summary>/// <param name="doc"></param>/// <param name="paragraphy"></param>private void AddInlineBarCode(XWPFDocument doc, XWPFParagraph paragraphy){string appPath = System.AppDomain.CurrentDomain.BaseDirectory;string imagePath = System.IO.Path.Combine(appPath, "Images");string imageFile = System.IO.Path.Combine(imagePath, "BarCode.jpg");// 二維碼            XWPFRun run = paragraphy.CreateRun();using (FileStream picFile = new FileStream(imageFile, FileMode.Open, FileAccess.Read)){XWPFPicture pic = run.AddPicture(picFile, (int)PictureType.PNG, "barcode.png", 100 * 10857, 100 * 12857);}}private T LoadWordCT<T>(string xml, Func<XmlElement, T> act) where T : class{XmlDocument xmlDoc = new XmlDocument();try{xmlDoc.LoadXml(xml);var element = xmlDoc.DocumentElement;return act(element);                }catch (XmlException xe){return null;}}/// <summary>/// 加入二維碼/// </summary>/// <param name="doc"></param>private void AddBarCode(XWPFDocument doc, XWPFParagraph paragraphy){String relationId;XWPFPictureData picData;string appPath = System.AppDomain.CurrentDomain.BaseDirectory;string imagePath = System.IO.Path.Combine(appPath, "Images");string imageFile = System.IO.Path.Combine(imagePath, "BarCode.jpg");using (FileStream picFile = new FileStream(imageFile, FileMode.Open, FileAccess.Read)){relationId = doc.AddPictureData(picFile, (int)PictureType.PNG);picData = (XWPFPictureData)doc.GetRelationById(relationId);}XWPFRun run = paragraphy.CreateRun();CT_RPr rpr = new CT_RPr();rpr.AddNewNoProof();run.GetCTR().rPr = rpr;CT_Drawing drawing = run.GetCTR().AddNewDrawing();drawing.anchor = new List<CT_Anchor>();CT_Anchor an = new CT_Anchor();            //圖片距正文上(distT)、下(distB)、左(distL)、右(distR)的距離。114300EMUS=3.1mman.distT = 0;an.distB = 0;an.distL = (uint)114300;an.distR = (uint)114300;an.relativeHeight = 251658240u;an.behindDoc = false; //"0",圖與文字的上下關系an.locked = false;  //"0"an.layoutInCell = true;  //"1"an.allowOverlap = true;  //"1" an.simplePos1 = false;an.simplePos1Specified = false;//CT_Point2D simplePos = new CT_Point2D();//simplePos.x = (long)0;//simplePos.y = (long)0;//an.simplePos = simplePos;// https://www.cnblogs.com/zhangliming/archive/2013/04/02/2995655.htmlstring xml = "<wp:simplePos x=\"0\" y=\"0\" xmlns:wp=\"http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing\" />";an.simplePos = LoadWordCT<CT_Point2D>(xml, x => CT_Point2D.Parse(x, null));//圖左上角坐標CT_PosH posH = new CT_PosH();posH.relativeFrom = ST_RelFromH.column;posH.posOffset = -1292;   //單位:EMUS,1CM=360000EMUSan.positionH = posH;CT_PosV posV = new CT_PosV();posV.relativeFrom = ST_RelFromV.paragraph;posV.posOffset = 51661;an.positionV = posV;an.extent = new NPOI.OpenXmlFormats.Dml.WordProcessing.CT_PositiveSize2D();an.extent.cx = 1085700;an.extent.cy = 1285700;CT_EffectExtent effectExtent = new CT_EffectExtent();effectExtent.b = 0L;effectExtent.l = 0L;effectExtent.r = 0L;effectExtent.t = 0L;an.effectExtent = effectExtent;an.wrapNone = new CT_WrapNone();an.docPr = new NPOI.OpenXmlFormats.Dml.WordProcessing.CT_NonVisualDrawingProps();long id = run.Document.GetNextPicNameNumber((int)PictureType.PNG);an.docPr.id = (uint)(id);an.docPr.name = ("Drawing " + id);an.docPr.descr = "barcode.png";an.cNvGraphicFramePr = new NPOI.OpenXmlFormats.Dml.WordProcessing.CT_NonVisualGraphicFrameProperties();an.graphic = new CT_GraphicalObject();CT_GraphicalObjectData gd = new CT_GraphicalObjectData();gd.uri = "http://schemas.openxmlformats.org/drawingml/2006/picture";an.graphic.graphicData = gd;// Grab the picture objectNPOI.OpenXmlFormats.Dml.Picture.CT_Picture pic = new NPOI.OpenXmlFormats.Dml.Picture.CT_Picture();// Set it upNPOI.OpenXmlFormats.Dml.Picture.CT_PictureNonVisual nvPicPr = pic.AddNewNvPicPr();NPOI.OpenXmlFormats.Dml.CT_NonVisualDrawingProps cNvPr = nvPicPr.AddNewCNvPr();/* use "0" for the id. See ECM-576, 20.2.2.3 */cNvPr.id = (0);/* This name is not visible in Word 2010 anywhere */cNvPr.name = ("Picture " + id);cNvPr.descr = "barcode.png";CT_NonVisualPictureProperties cNvPicPr = nvPicPr.AddNewCNvPicPr();cNvPicPr.AddNewPicLocks().noChangeAspect = true;CT_BlipFillProperties blipFill = pic.AddNewBlipFill();CT_Blip blip = blipFill.AddNewBlip();blip.embed = run.Paragraph.Part.GetRelationId(picData);blipFill.AddNewStretch().AddNewFillRect();CT_ShapeProperties spPr = pic.AddNewSpPr();CT_Transform2D xfrm = spPr.AddNewXfrm();CT_Point2D off = xfrm.AddNewOff();off.x = (0);off.y = (0);NPOI.OpenXmlFormats.Dml.CT_PositiveSize2D ext = xfrm.AddNewExt();ext.cx = 100;ext.cy = 100;CT_PresetGeometry2D prstGeom = spPr.AddNewPrstGeom();prstGeom.prst = (ST_ShapeType.rect);prstGeom.AddNewAvLst();using (var ms = RecyclableMemory.GetStream()){StreamWriter sw = new StreamWriter(ms);pic.Write(sw, "pic:pic");sw.Flush();ms.Position = 0;var sr = new StreamReader(ms);var picXml = sr.ReadToEnd();gd.AddPicElement(picXml);}drawing.anchor.Add(an);}private XWPFParagraph AddParagraph(XWPFDocument doc){XWPFParagraph paragraphy = doc.CreateParagraph();CT_PPr ppr = new CT_PPr();CT_Spacing sp = ppr.AddNewSpacing();// w:spacing w:after="20" w:line="430" w:lineRule="auto" sp.after = 20;sp.line = "430";sp.lineRule = ST_LineSpacingRule.auto;CT_Ind ind = ppr.AddNewInd();ind.right = "10";CT_Jc jc = ppr.AddNewJc();// 左對齊jc.val = ST_Jc.left;CT_ParaRPr rpr = ppr.AddNewRPr();rpr.rFonts = new CT_Fonts();rpr.rFonts.ascii = "宋體";rpr.rFonts.hAnsi = "宋體";rpr.rFonts.eastAsia = "宋體";rpr.rFonts.cs = "宋體";rpr.b = new CT_OnOff();rpr.b.val = true;rpr.sz = new CT_HpsMeasure();rpr.sz.val = 32;paragraphy.GetCTP().pPr = ppr;return paragraphy;}private XWPFParagraph AddTextParagraph(XWPFDocument doc){XWPFParagraph paragraphy = doc.CreateParagraph();CT_PPr ppr = new CT_PPr();CT_Spacing sp = ppr.AddNewSpacing();sp.lineRule = ST_LineSpacingRule.auto;CT_Ind ind = ppr.AddNewInd();CT_Jc jc = ppr.AddNewJc();jc.val = ST_Jc.right;CT_ParaRPr rpr = ppr.AddNewRPr();rpr.rFonts = new CT_Fonts();rpr.rFonts.ascii = "宋體";rpr.rFonts.hAnsi = "宋體";rpr.rFonts.eastAsia = "宋體";rpr.rFonts.cs = "宋體";rpr.b = new CT_OnOff();rpr.b.val = true;rpr.sz = new CT_HpsMeasure();rpr.sz.val = 32;paragraphy.GetCTP().pPr = ppr;return paragraphy;}private XWPFRun AddTextRun(XWPFParagraph paragraphy, string text, ulong fontSize){XWPFRun run = paragraphy.CreateRun();run.IsBold = true;CT_RPr rpr = new CT_RPr();CT_Fonts ft = rpr.AddNewRFonts();ft.ascii = "宋體";ft.hAnsi = "宋體";ft.eastAsia = "宋體";ft.cs = "宋體";rpr.AddNewB();CT_HpsMeasure sz = rpr.AddNewSz();sz.val = fontSize;run.GetCTR().rPr = rpr;run.SetText(text);return run;}public bool CreateWordReport(string wordFile){try{string appPath = System.AppDomain.CurrentDomain.BaseDirectory;              string checkTime = DateTime.Now.ToString("yyyy年MM月dd日");//檢查時間//TODO:使用FileStream文件流來寫入數據(傳入參數為:文件所在路徑,對文件的操作方式,對文件內數據的操作)//通過使用文件流,創建文件流對象,向文件流中寫入內容,并保存為Word文檔格式using (var stream = new FileStream(wordFile, FileMode.Create, FileAccess.Write)){//創建document文檔對象對象實例XWPFDocument doc = new XWPFDocument();XWPFParagraph paragraphy = AddParagraph(doc);AddBarCode(doc, paragraphy);paragraphy = AddTextParagraph(doc);AddTextRun(paragraphy, "TEST1", 32);paragraphy = AddTextParagraph(doc);AddTextRun(paragraphy, "TEST2", 32);paragraphy = AddTextParagraph(doc);AddTextRun(paragraphy, "TEST3", 32);//向文檔流中寫入內容,生成worddoc.Write(stream);return true;}}catch (Exception ex){return false;}}
}

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/web/10280.shtml
繁體地址,請注明出處:http://hk.pswp.cn/web/10280.shtml
英文地址,請注明出處:http://en.pswp.cn/web/10280.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

js由那三部分組成

JavaScript 主要由三部分組成&#xff1a;ECMAScript、DOM&#xff08;文檔對象模型&#xff09;和 BOM&#xff08;瀏覽器對象模型&#xff09;。 1、ECMAScript ECMAScript 是 JavaScript 的核心&#xff0c;描述了語言的基本語法&#xff08;變量、函數、條件語句、循環、…

前端筆記-day03

文章目錄 01-初始CSS02-CSS引入方式03-標簽選擇器04-類選擇器05-id選擇器06-通配符選擇器07-畫盒子08-字體大小09-文字粗細10-字體傾斜11-行高12-行高垂直居中13-字體族14-font復合屬性15-文本縮進16-文本對齊方式17-圖片對齊方式18-文本修飾線19-文字顏色20-調試工具21-綜合案…

Dual Aggregation Transformer for Image Super-Resolution論文總結

題目&#xff1a;Dual Aggregation Transformer&#xff08;雙聚合Transformer&#xff09; for Image Super-Resolution&#xff08;圖像超分辨&#xff09; 論文&#xff08;ICCV&#xff09;&#xff1a;Chen_Dual_Aggregation_Transformer_for_Image_Super-Resolution_ICCV…

IM 是什么?

在當今數字化的時代&#xff0c;即時通訊&#xff08;IM&#xff09;已經滲透到人們的日常生活和企業的工作環境中。IM技術的快速i發展為人們提供了一種高效、便捷的溝通方式&#xff0c;不僅推動了社會的信息化進程&#xff0c;也提升了企業的協同效率和競爭力。 作為企業級I…

【GD32】01-GPIO通用輸入輸出

GD32 閑話說在前頭 這里又開一個系列啦。 原因就是之前買了立創開發板的9.9的GD32E230C8T6的板子&#xff0c;買都買了就跟著立創開發板學習一下&#xff08;屬于是一次性支持了兩個國產品牌了&#xff0c;立創和兆易創新&#xff09;。并且我還買了GD32F407VET6的板子&…

資金流分析下的企業供貨關系強度模型

圖技術 利用neo4j、networkx、dgl、python做圖分析挖掘 【1】最短路徑算法dijkstra 【2】基于networkx的隱性集團關系識別模型 【3】基于Neo4j的擔保社群型態分析挖掘 【4】基于python求有向無環圖中target到其他節點全路徑 【5】有向圖中任意兩點的路徑 【6】圖基礎入門 【7】…

項目管理中控制質量的工具與技術

項目管理中控制質量的工具與技術 控制質量的工具與技術包括多種方法&#xff0c;旨在確保產品或服務達到既定的質量標準。關于具體的工具格式和樣式&#xff0c;以下是一些示例&#xff1a; 統計技術&#xff1a; 這是一種將質量控制要素的數據轉化為實際控制手段的技術。通…

Visual Studio和Visual Studio Code適用于哪些編程語言

Visual Studio和Visual Studio Code都適用于多種編程語言&#xff0c;它們的適用編程語言如下&#xff1a; Visual Studio適用于&#xff1a; C#Visual Basic .NETF#CJavaScriptTypeScriptPythonHTML/CSSJava&#xff08;通過插件支持&#xff09; Visual Studio Code適用于…

Jtti:哪些方法可以降低美國CN2服務器的延遲?

降低美國CN2服務器的延遲可以采取多種方法&#xff0c;以下是一些常用的方法&#xff1a; 1.選擇優質的網絡提供商和服務商&#xff1a;選擇具有高質量網絡和優質服務的網絡提供商和服務商是降低延遲的關鍵。確保您選擇的網絡提供商具有可靠的基礎設施和優質的網絡連接&#xf…

C++:關于圓形魚眼半全景圖轉為等距圓柱投影圖

C&#xff1a;空間坐標映射到球面坐標/全景圖_如何將球體坐標映射到球面uv-CSDN博客 C&#xff1a;關于360全景圖像和立方體6面全景圖像的相互轉換_彩色全景拆解正方體6個面-CSDN博客 之前記錄了立方體和360全景之間的轉換&#xff0c;這次記錄下魚眼圖與360全景圖之間的轉換…

C++ STL的鎖介紹

在 C Standard Template Library (STL) 中&#xff0c;有幾個鎖的實現&#xff0c;這些都位于 <mutex> 頭文件。以下是一些常見的鎖及其功能&#xff1a; std::mutex&#xff1a;最基本的互斥鎖&#xff0c;不可遞歸使用。該鎖提供了獨占的非公平鎖定能力。 std::mutex…

處理瀏覽器緩存問題

處理瀏覽器緩存問題 main.js router.onError((error) > {hasRefreshed false;console.log("異常" error.message);let ind0 error.message.indexOf(Loading chunk chunk-)let ind2 error.message.indexOf(failed.)if(ind0 ! -1 && ind2 !-1){console.…

spring-boot-starter-validation校驗框架

介紹 Spring Boot Starter Validation是一個Spring Boot模塊&#xff0c;用于簡化應用程序中的驗證功能。它包含以下依賴&#xff1a; Hibernate Validator&#xff1a;Hibernate Validator是JavaBean Validation&#xff08;JSR 380&#xff09;規范的參考實現&#xff0c;提…

信創應用軟件之辦公流版簽

信創應用軟件之辦公流版簽 文章目錄 信創應用軟件之辦公流版簽概述流式文件版式文件電子簽章廠商金山辦公永中-永中Office中標-中標普華Office福昕科技e簽寶法大大 概述 辦公流版簽軟件主要包括辦公中常用到的流式軟件、版式軟件以及電子簽章。 版式文件和流式文件都是文書類…

軟件體系結構總結

文章目錄 一、軟件體系結構概述1.1 基本概念1.1.1 背景1.1.2 定義1.1.3 系統1.1.3.1 定義1.1.3.2 特性1.1.3.3 系統的體系結構 1.1.4 軟件設計的層次性1.1.5 體系結構的類別&#xff08;類型&#xff09;1.1.6 重要性&#xff08;意義&#xff09; 1.2 模塊及其設計1.2.1 定義1…

HTTP超時時間設置

在進行超時時間設置之前我們需要了解一次http請求經歷的過程 瀏覽器進行DNS域名解析&#xff0c;得到對應的IP地址根據這個IP&#xff0c;找到對應的服務器建立連接&#xff08;三次握手&#xff09;建立TCP連接后發起HTTP請求&#xff08;一個完整的http請求報文&#xff09;服…

[單機]完美國際_V155_GM工具_VM虛擬機

[端游] 完美國際單機版V155一鍵端PC電腦網絡游戲完美世界幻海凌云家園 本教程僅限學習使用&#xff0c;禁止商用&#xff0c;一切后果與本人無關&#xff0c;此聲明具有法律效應&#xff01;&#xff01;&#xff01;&#xff01; 教程是本人親自搭建成功的&#xff0c;絕對是…

出海企業哪種組網方案更省事?

對于出海企業而言&#xff0c;建立跨地區的數據傳輸和協同工作至關重要&#xff0c;以提升運營效率。因此&#xff0c;網絡構建變得迫在眉睫。通過構建企業組網&#xff0c;企業能夠加強與海外分支、客戶和合作伙伴之間的聯系&#xff0c;加速海外業務的發展。 然而&#xff0c…

Stable Diffusion教程|圖生圖原理和實戰

Stable Diffusion憑借其卓越的圖生圖功能&#xff0c;極大地提升了圖像生成的可控性與輸出品質&#xff0c;賦予用戶前所未有的個性化創作風格表達能力。這一革新特性使得Stable Diffusion不僅能精準地捕捉用戶的藝術愿景&#xff0c;更能以數字化手段孕育出新穎且極具創意的畫…

【Java 8】Lambda: mAA::get 返回函數式接口實例

正文 代碼介紹&#xff1a;分別有兩個函數式接口Lazy跟LazyDependencyCreator&#xff0c;一個HashMap存儲<Object, LazyDependencyCreator>鍵值對。重點在于mProviders.put(AA.class,mAA::get)&#xff0c;看了很久都沒看懂&#xff0c;mAA::get返回的不是mAA本身嗎&am…