2016-07-29 6 views
2

DocX Libraryを使用して、C#でWordファイルに画像を追加したいとします。問題は私がウェブで何も見つけられないことです。C#.NET DocX .docxファイルに画像を追加する

状況

は、私は、ファイルを作成する方法を知っていると私は、ファイル内のテキストを書く方法を知っています。ライブラリのドキュメンテーションは悲しいほど些細なものです。あなたが私を助けることを願っています!

+0

チェック[この](https://social.msdn.microsoft.com/Forums/office/en-US/6c2ce499-0454-4b9e-b8d0-19d6350ae8dd/c-word-2010-paste-in-image-and-move-behind-text?forum = worddev)answer。 – Shakra

答えて

6

DOCXライブラリは、文書に画像を追加する方法を示すsampleが含まれています

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

ありがとうございます:) – Blue

関連する問題