在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 instance
Document document = new Document();
//Load a sample Word document
document.LoadFromFile("input.docx");
//Get the first section
Section section = document.Sections[0];
//Get two specified paragraphs
Paragraph para1 = section.Paragraphs[5];
Paragraph para2 = section.Paragraphs[9];
//Insert images in the specified paragraphs
DocPicture 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 respectively
Pic1.TextWrappingStyle = TextWrappingStyle.Square;
Pic2.TextWrappingStyle = TextWrappingStyle.Inline;
//Save the document to file
document.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 instance
Document document = new Document();
//Load a sample Word document
document.LoadFromFile("input.docx");
//Get the first section
Section section = document.Sections[0];
//Load an image and insert it to the document
DocPicture 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 image
picture.Width = 150;
picture.Height = 150;
//Set the wrapping style to Behind
picture.TextWrappingStyle = TextWrappingStyle.Behind;
// Save the document to file
document.SaveToFile("Insert.docx", FileFormat.Docx);
}
}
}
用到的框架:FreeSpire.Doc 10.2