C# .NET DocX 将图像添加到 .docx 文件 [英] C# .NET DocX Add an Image to a .docx File

查看:54
本文介绍了C# .NET DocX 将图像添加到 .docx 文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to add an image to a Word file in C# using the DocX Library. The problem is that I don't find anything in the web.

情况

我知道如何创建文件,也知道如何在文件中写入文本.遗憾的是,图书馆的文档非常少.希望你能帮助我!

I know how to create a file and I know how to write text in the file. The documentation of the library is sadly pretty small. Hope you can help me!

推荐答案

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

The DocX library contains a sample demonstrating how to add a picture to a document:

var myImageFullPath = "C:	mpsample.png";
using (DocX document = DocX.Create(@"docsHelloWorldAddPictureToWord.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();
}

这篇关于C# .NET DocX 将图像添加到 .docx 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆