C# .NET DocX Add an Image to a .docx File

╄→гoц情女王★ 提交于 2019-12-19 10:13:30

问题


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.

Situation

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!


回答1:


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

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();
}


来源:https://stackoverflow.com/questions/38658848/c-sharp-net-docx-add-an-image-to-a-docx-file

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!