Skip to content

Commit

Permalink
Added new example to show how to add image to Word document. It's als…
Browse files Browse the repository at this point in the history
…o available as part of blog http://evotec.pl/docx-add-image-to-microsoft-word-document-programmatically/

Changed some solution settings to make sure it works with AnyCpu.
  • Loading branch information
PrzemyslawKlys authored and PrzemyslawKlys committed Mar 5, 2016
1 parent 3250337 commit 0acb1b0
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 2 deletions.
4 changes: 2 additions & 2 deletions DocX.sln
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ Global
{3EA73F1C-9E0B-4ED5-B04B-6C043D14D1AA}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU
{3EA73F1C-9E0B-4ED5-B04B-6C043D14D1AA}.Release|Mixed Platforms.Build.0 = Release|Any CPU
{3EA73F1C-9E0B-4ED5-B04B-6C043D14D1AA}.Release|x86.ActiveCfg = Release|Any CPU
{F3022BB7-0E40-4C80-A495-37FEAF3671AB}.Debug|Any CPU.ActiveCfg = Debug|x86
{F3022BB7-0E40-4C80-A495-37FEAF3671AB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{F3022BB7-0E40-4C80-A495-37FEAF3671AB}.Debug|Any CPU.Build.0 = Debug|Any CPU
{F3022BB7-0E40-4C80-A495-37FEAF3671AB}.Debug|Mixed Platforms.ActiveCfg = Debug|Any CPU
{F3022BB7-0E40-4C80-A495-37FEAF3671AB}.Debug|Mixed Platforms.Build.0 = Debug|Any CPU
{F3022BB7-0E40-4C80-A495-37FEAF3671AB}.Debug|x86.ActiveCfg = Debug|x86
Expand All @@ -69,7 +70,6 @@ Global
{F3022BB7-0E40-4C80-A495-37FEAF3671AB}.Release|x86.ActiveCfg = Release|x86
{F3022BB7-0E40-4C80-A495-37FEAF3671AB}.Release|x86.Build.0 = Release|x86
{1EB1EE8F-9978-425B-A742-533DCCEB4811}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{1EB1EE8F-9978-425B-A742-533DCCEB4811}.Debug|Any CPU.Build.0 = Debug|Any CPU
{1EB1EE8F-9978-425B-A742-533DCCEB4811}.Debug|Mixed Platforms.ActiveCfg = Debug|Any CPU
{1EB1EE8F-9978-425B-A742-533DCCEB4811}.Debug|x86.ActiveCfg = Debug|Any CPU
{1EB1EE8F-9978-425B-A742-533DCCEB4811}.Release|Any CPU.ActiveCfg = Release|Any CPU
Expand Down
52 changes: 52 additions & 0 deletions Examples/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ static void Main(string[] args)
HelloWorldKeepWithNext();
HelloWorldAdvancedFormatting();
HelloWorldProtectedDocument();
HelloWorldAddPictureToWord();
RightToLeft();
Indentation();
HeadersAndFooters();
Expand Down Expand Up @@ -1825,7 +1826,58 @@ static void TableWithSpecifiedWidths()

}

/// <summary>
/// Create a document with two pictures. One picture is inserted normal way, the other one with rotation
/// </summary>
static void HelloWorldAddPictureToWord() {
Console.WriteLine("\tHelloWorldAddPictureToWord()");

// Create a document.
using (DocX document = DocX.Create(@"docs\HelloWorldAddPictureToWord.docx"))
{
// Add an image into the document.
RelativeDirectory rd = new RelativeDirectory(); // prepares the files for testing
rd.Up(2);
Image image = document.AddImage(rd.Path + @"\images\logo_template.png");

// Create a picture (A custom view of an Image).
Picture picture = image.CreatePicture();
picture.Rotation = 10;
picture.SetPictureShape(BasicShapes.cube);

// 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("Just below there should be a picture ").Append("picture").Bold().Append(" inserted in a non-conventional way.");
p1.AppendLine();
p1.AppendLine("Check out this picture ").AppendPicture(picture).Append(" its funky don't you think?");
p1.AppendLine();

// Insert a new Paragraph into the document.
Paragraph p2 = document.InsertParagraph();
// Append content to the Paragraph.

p2.AppendLine("Is it correct?");
p2.AppendLine();

// Lets add another picture (without the fancy stuff)
Picture pictureNormal = image.CreatePicture();

Paragraph p3 = document.InsertParagraph();
p3.AppendLine("Lets add another picture (without the fancy rotation stuff)");
p3.AppendLine();
p3.AppendPicture(pictureNormal);

// Save this document.
document.Save();
Console.WriteLine("\tCreated: docs\\HelloWorldAddPictureToWord.docx\n");
}
}


}
Expand Down

0 comments on commit 0acb1b0

Please sign in to comment.