C#向一个Word文件中插入文字与图片

本文档介绍了如何使用Spire.DocFor.NET库在Word文档中插入文字和图片。通过示例代码详细展示了如何设置文字颜色、字体大小、下划线样式,以及如何插入图片并调整其位置和大小。此外,还提供了在特定位置插入图片的方法。
摘要由CSDN通过智能技术生成

在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

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值