在Word中插入文字

Step1:创建新的word文件

Document document = new Document();
document.LoadFromFile("sample.docx", FileFormat.Docx);

Step2:添加文字和文字格式

Paragraph paraInserted = new Paragraph(document);
TextRange textRange1 = paraInserted.AppendText("This is a inserted paragraph, I want to insert this paragraph in the start of document.");
textRange1.CharacterFormat.TextColor = Color.Blue;
textRange1.CharacterFormat.FontSize = 15;
textRange1.CharacterFormat.UnderlineStyle = UnderlineStyle.Dash;

Step3:添加一个新的段落

document.Sections[0].Paragraphs.Insert(0, paraInserted);

Step4:保存文件

document.SaveToFile("result.docx", FileFormat.Docx);

效果图:

完整代码:

using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;
using System.Drawing;
namespace InsertParagh
{class Program{static void Main(string[] args){Document document = new Document();document.LoadFromFile("sample.docx", FileFormat.Docx);Paragraph paraInserted = document.Sections[0].AddParagraph();TextRange textRange1 = paraInserted.AppendText("This is a inserted paragraph, I want to insert this paragraph in the start of document.");textRange1.CharacterFormat.TextColor = Color.Blue;textRange1.CharacterFormat.FontSize = 15;textRange1.CharacterFormat.UnderlineStyle = UnderlineStyle.Dash;document.Sections[0].Paragraphs.Insert(0, document.Sections[0].Paragraphs[document.Sections[0].Paragraphs.Count - 1]);document.SaveToFile("result.docx", FileFormat.Docx);}}
}

在Word中插入图片

往word文档中插入图片是一种常见操作,比起整片都是文字的文档,带上图片的文档更加生动,具有吸引力。在这一节中你将学会如何使用Spire.Doc For .NET往一个word文档中插入图片。你也可以设置图片的大小和位置。

插入图片并且设置换行样式

Spire.Doc For .NET 支持普通的换行样式比如:水平对齐,行内对齐,紧跟文本。下面是具体的代码:

using System.Drawing;
using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;namespace WordImage
{class ImageinWord{static void Main(string[] args){//Create a Document instanceDocument document = new Document();//Load a sample Word documentdocument.LoadFromFile("input.docx");//Get the first section Section section = document.Sections[0];//Get two specified paragraphsParagraph para1 = section.Paragraphs[5];Paragraph para2 = section.Paragraphs[9];//Insert images in the specified paragraphsDocPicture Pic1 = para1.AppendPicture(Image.FromFile(@"C:\Users\Administrator\Desktop\pic1.jpg"));DocPicture Pic2 = para2.AppendPicture(Image.FromFile(@"C:\Users\Administrator\Desktop\pic2.png"));//Set wrapping styles to Square and Inline respectivelyPic1.TextWrappingStyle = TextWrappingStyle.Square;Pic2.TextWrappingStyle = TextWrappingStyle.Inline;//Save the document to filedocument.SaveToFile("InsertImage.docx", FileFormat.Docx);}}
}

在Word文档中的特定位置插入图片

DocPicture.HorizontalPosition和DocPicture.VerticalPosition属性可以让你将图片插入到特定的坐标中。下面是具体的代码:

using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;
using System.Drawing;namespace InsertImage
{class Program{static void Main(string[] args){//Create a Document instanceDocument document = new Document();//Load a sample Word documentdocument.LoadFromFile("input.docx");//Get the first section Section section = document.Sections[0];//Load an image and insert it to the documentDocPicture picture = section.Paragraphs[0].AppendPicture(Image.FromFile(@"C:\Users\Administrator\Desktop\pic.jpg"));//Set the position of the image picture.HorizontalPosition = 90.0F;picture.VerticalPosition = 50.0F;//Set the size of the imagepicture.Width = 150;picture.Height = 150;//Set the wrapping style to Behindpicture.TextWrappingStyle = TextWrappingStyle.Behind;// Save the document to filedocument.SaveToFile("Insert.docx", FileFormat.Docx);}}
}

用到的框架:FreeSpire.Doc 10.2

C#向一个Word文件中插入文字与图片相关推荐

  1. python文本筛选多个word_用Python批量替换多个Word文件中的文字

    实例14:用Python批量替换多个Word文件中的文字 公众号"Python操作Office软件高效工作" 29 人赞同了该文章 我们在实例7中批量生成了采购合同.但是假设现在我 ...

  2. word批量替换多组文字 python_实例14:用Python批量替换多个Word文件中的文字

    我们在实例7中批量生成了采购合同.但是假设现在我方的公司名由"ABC商贸有限公司"变成了"ABC贸易有限公司",那我们就需要去每份合同中对应位置进行替换.当然也 ...

  3. java如何使用aspose向word文件中插入表格、固定值

    aspose操作word插入表格数据 一.word模板文件编辑 1.1插入String内容 1.2插入表格内容 二.后端代码实现 二.成果展现 需求:利用java实现向一个word文件中特定位置插入表 ...

  4. word文档中找不到服务器,【在WORD文件中插入excel表格,编辑保存后重新打开word文件并双击excel时提示“找不到服务器应用程序、源文件】excel整个表格不见了...

    为什么PPT插入EXCEL表格时显示无法找到服务器应用程序 这是由于excel或者ppt只能兼容.低版本的ppt和excel是无法打版本excel插入的表格的. 解决如下: 1.新建一个文本,将后缀名 ...

  5. c# 在 word指定位置插入文字和图片(替换 书签)

    使用  Microsoft.Office.Interop.Word  进行插入, word 文件中 插入 "书签" . 在com组件中引用  Microsoft.Word.xxx. ...

  6. 在可编辑div中插入文字或图片的问题解决思路

    最近在网上碰到一个人问了我一个问题,在可编辑div中插入文字或者图片.因为web在线编辑器我从来只是用,基本不会去研究源代码.后来正好一个在线聊天web项目中也要用到这个功能,我就特地看看了代码. 基 ...

  7. 用Python批量替换多个Word文件中的文字

    定义函数info_update.只需要向其中传入目标文件路径,待替换的词和新词即可 import docx def info_update(doc,old_info, new_info):'''此函数 ...

  8. Java替换Word模板中的文字、图片、表格、水印

    前言 由于业务需求,要求开发一个工具类,需要对Word模板中的内容进行替换,并且支持插入图片.根据业务生成自定义表格,以及对处理后的文件增加水印,处理完的文件支持保存doc.docx.pdf格式 1. ...

  9. 在Word简历中插入照片(图片)

    第一步先是插入图片: 在简历中插入照片可能会是一个问题.因为插入的照片会把光标前后的文字分隔开来.解决这个问题的方法就是设置插入的照片格式为上浮于文字就行了.以Word 2007为例,在照片.图片上面 ...

  10. java word标签替换_JAVA实现替换WORD模板中的文字和图片

    1.本来有其他方法可以实现的,但是由于jar包一直有问题所以换了种,要是还有其他的更好的方法,希望可以讨论 2.废话不说直接上代码: 3.我自己加了个生成图片的方法,也可以不需要直接用已经存在的图片替 ...

最新文章

  1. 计算机英语 动态,计算机英语高级词汇精选
  2. 【C++11新特性】 - 空间配置allocator类
  3. $(document).ready、body.Onload()和 $(window).load的区别
  4. 视觉、语音、NLP、ML、AI安全,一家金融科技公司等你来
  5. 摄影专业学生,没有摄影棚,怎么练摄影?
  6. Leetcode 304.二维区域和检索-矩阵不可变
  7. 定义cisco设备的log级别
  8. 古代埃及希腊,数学用的什么进制
  9. linux上sftp工具使用
  10. 西门子安装未找到ssf文件_西门子Step7 V5.5 中文版+Sp1 安装教程
  11. win7 升级到 win10
  12. 科研第一步:怎样获得好的idea?!
  13. 网络安全笔记-99-渗透-渗透测试方法论
  14. 计算机安装Hp1005打印机,如何安装hp1005打印机驱动程序_安装步骤是什么
  15. 全卷积网络FCN与卷积神经网络CNN的区别
  16. web前端学习路线-20个真实web开发项目集合
  17. 西安华清远见学习一周的收获总结----ubuntu的安装,适合初学者
  18. Kalendae多选日历插件
  19. Epub解析 -- Pageturner
  20. 预装linux的笔记本好处,联想将更认真对待预装Linux笔记本

热门文章

  1. 网线断了也能上网?被限速?
  2. 图解TCP/IP 读后感
  3. C语言show用法,show的用法和例句
  4. 【所见即所得】数据分析最有用的25个 Matplotlib图【附代码】
  5. redis问题及答案
  6. 【机器学习】注意力机制
  7. vivo X9的usb调试模式在哪里,开启vivo X9usb调试模式的方法
  8. windows快捷键之预览窗格
  9. win7 插入鼠标自动禁用触摸板
  10. nssa和stub_ospf中stub区域和nssa区域的主要区别是什么?