详解一款开源免费的.NET文档操作组件DocX(.NET组(3)

/// <summary> /// 将指定内容写入文档 /// </summary> /// <param>加载文件路径</param> /// <param>写入文件内容</param> /// <param>保存文件路径</param> public static void ProgrammaticallyManipulateImbeddedImage(string path, string content, string savePath) { if (string.IsNullOrEmpty(path)) { throw new ArgumentNullException(path); } if (string.IsNullOrEmpty(content)) { throw new ArgumentNullException(content); } if (string.IsNullOrEmpty(savePath)) { throw new ArgumentNullException(savePath); } try { using (var document = DocX.Load(path)) { // 确保此文档至少有一个图像。 if (document.Images.Any()) { var img = document.Images[0]; // 将内容写入图片. var b = new Bitmap(img.GetStream(FileMode.Open, FileAccess.ReadWrite)); //获取此位图的图形对象,图形对象提供绘图功能。 var g = Graphics.FromImage(b); // 画字符串内容 g.DrawString ( content, new Font("Tahoma", 20), Brushes.Blue, new PointF(0, 0) ); // 使用创建\写入流将该位图保存到文档中。 b.Save(img.GetStream(FileMode.Create, FileAccess.Write), ImageFormat.Png); } else { document.SaveAs(savePath); } } } catch (Exception ex) { throw new Exception(ex.Message); } }

四.总结:

以上是对DocX组件的API做了一个简单的解析,并且附上一些创建文档和创建图表的方法供开发者参考。希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

您可能感兴趣的文章:

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wjywys.html