通过Office自带的类库word文档中插入图片,图片大小的单位为磅
而文档中,图片的大小已经固定,为CM。
实际工作中,首先将图片插入到word中,根据目前的大小,计算转换为目标大小的比率,将长宽按照目标大小进行缩放即可。
msword.InlineShape spa = oOperateDoc.Tables[oOperateDoc.Tables.Count].Cell(1, 1).Range.InlineShapes.AddPicture(jietuA);float yuanwidth = spa.Width;float yuanheidht = spa.Height;double bilv = GetChangepicSizeRation(yuanwidth,yuanheidht, targetW, targetH);spa.Width = (float)(yuanwidth * bilv);spa.Height = (float)(yuanheidht * bilv);
private double GetChangepicSizeRation(double width_pt, double height_pt, double targetWidthCM, double targetHeightCM) { //CM转pt double p = 0.03527;// 0.03759; //1pt=0.3759mm double TargetWdithPt = targetWidthCM / p; double TargetHeightPt = targetHeightCM / p; double[] bilv = new double[2]; bilv[0] = TargetWdithPt / width_pt; bilv[1] = TargetHeightPt / height_pt; return (bilv[0] > bilv[1] ? bilv[1] : bilv[0]); }