NPOI Word插入图片的方法

从资源文件中加载图片插入到word中:

Assembly assm = Assembly.GetExecutingAssembly();
Stream istr = assm.GetManifestResourceStream("idgt_converter.Resources.caution.png");
XWPFParagraph p = table.GetRow(rowIndex).GetCell(i).AddParagraph();
XWPFRun r = p.CreateRun();

var widthPx = Resource.caution.Width;
var heightPx = Resource.caution.Height;
var horzRezDpi = Resource.caution.HorizontalResolution;
var vertRezDpi = Resource.caution.VerticalResolution;
const int emusPerInch = 914400;
var widthEmus = (int)(widthPx / horzRezDpi * emusPerInch);
var heightEmus = (int)(heightPx / vertRezDpi * emusPerInch);

XWPFPicture pic = r.AddPicture(istr, (int)PictureType.PNG, "caution.png", widthEmus, heightEmus);

AddPicture方法的宽和高要注意,不是图片的像素,而是需要根据像素和分辨率计算出来的值。

你可能感兴趣的:(笔记,C#,Word,处理,NPOI)