C#.NET DocX将图像添加到.DocX文件中

本文关键字:DocX 文件 添加 图像 NET | 更新日期: 2023-09-27 17:59:16

我想使用DocX库将图像添加到C#中的Word文件中。问题是我在网上找不到任何东西。

情况

我知道如何创建文件,也知道如何在文件中写入文本。图书馆的文件少得可怜。希望你能帮助我!

C#.NET DocX将图像添加到.DocX文件中

DocX库包含一个示例,演示如何将图片添加到文档中:

var myImageFullPath = "C:'tmp'sample.png";
using (DocX document = DocX.Create(@"docs'HelloWorldAddPictureToWord.docx"))
{
    // Add an image into the document.    
    Image image = document.AddImage(myImageFullPath);
    // Create a picture (A custom view of an Image).
    Picture picture = image.CreatePicture();
    // Insert a new Paragraph into the document.
    Paragraph title = document.InsertParagraph().Append("This is a test for a picture").FontSize(20).Font(new FontFamily("Comic Sans MS"));
    title.Alignment = Alignment.center;
    // Insert a new Paragraph into the document.
    Paragraph p1 = document.InsertParagraph();
    // Append content to the Paragraph
    p1.AppendLine("Check out this picture ").AppendPicture(picture).Append(" its funky don't you think?");
    p1.AppendLine();
    p1.AppendPicture(picture);
    // Save this document.
    document.Save();
}