Skip to content

Commit 0acb1b0

Browse files
PrzemyslawKlysPrzemyslawKlys
authored andcommitted
Added new example to show how to add image to Word document. It's also 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.
1 parent 3250337 commit 0acb1b0

File tree

2 files changed

+54
-2
lines changed

2 files changed

+54
-2
lines changed

DocX.sln

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,8 @@ Global
5757
{3EA73F1C-9E0B-4ED5-B04B-6C043D14D1AA}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU
5858
{3EA73F1C-9E0B-4ED5-B04B-6C043D14D1AA}.Release|Mixed Platforms.Build.0 = Release|Any CPU
5959
{3EA73F1C-9E0B-4ED5-B04B-6C043D14D1AA}.Release|x86.ActiveCfg = Release|Any CPU
60-
{F3022BB7-0E40-4C80-A495-37FEAF3671AB}.Debug|Any CPU.ActiveCfg = Debug|x86
60+
{F3022BB7-0E40-4C80-A495-37FEAF3671AB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
61+
{F3022BB7-0E40-4C80-A495-37FEAF3671AB}.Debug|Any CPU.Build.0 = Debug|Any CPU
6162
{F3022BB7-0E40-4C80-A495-37FEAF3671AB}.Debug|Mixed Platforms.ActiveCfg = Debug|Any CPU
6263
{F3022BB7-0E40-4C80-A495-37FEAF3671AB}.Debug|Mixed Platforms.Build.0 = Debug|Any CPU
6364
{F3022BB7-0E40-4C80-A495-37FEAF3671AB}.Debug|x86.ActiveCfg = Debug|x86
@@ -69,7 +70,6 @@ Global
6970
{F3022BB7-0E40-4C80-A495-37FEAF3671AB}.Release|x86.ActiveCfg = Release|x86
7071
{F3022BB7-0E40-4C80-A495-37FEAF3671AB}.Release|x86.Build.0 = Release|x86
7172
{1EB1EE8F-9978-425B-A742-533DCCEB4811}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
72-
{1EB1EE8F-9978-425B-A742-533DCCEB4811}.Debug|Any CPU.Build.0 = Debug|Any CPU
7373
{1EB1EE8F-9978-425B-A742-533DCCEB4811}.Debug|Mixed Platforms.ActiveCfg = Debug|Any CPU
7474
{1EB1EE8F-9978-425B-A742-533DCCEB4811}.Debug|x86.ActiveCfg = Debug|Any CPU
7575
{1EB1EE8F-9978-425B-A742-533DCCEB4811}.Release|Any CPU.ActiveCfg = Release|Any CPU

Examples/Program.cs

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ static void Main(string[] args)
2424
HelloWorldKeepWithNext();
2525
HelloWorldAdvancedFormatting();
2626
HelloWorldProtectedDocument();
27+
HelloWorldAddPictureToWord();
2728
RightToLeft();
2829
Indentation();
2930
HeadersAndFooters();
@@ -1825,7 +1826,58 @@ static void TableWithSpecifiedWidths()
18251826

18261827
}
18271828

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

1835+
// Create a document.
1836+
using (DocX document = DocX.Create(@"docs\HelloWorldAddPictureToWord.docx"))
1837+
{
1838+
// Add an image into the document.
1839+
RelativeDirectory rd = new RelativeDirectory(); // prepares the files for testing
1840+
rd.Up(2);
1841+
Image image = document.AddImage(rd.Path + @"\images\logo_template.png");
1842+
1843+
// Create a picture (A custom view of an Image).
1844+
Picture picture = image.CreatePicture();
1845+
picture.Rotation = 10;
1846+
picture.SetPictureShape(BasicShapes.cube);
1847+
1848+
// Insert a new Paragraph into the document.
1849+
Paragraph title = document.InsertParagraph().Append("This is a test for a picture").FontSize(20).Font(new FontFamily("Comic Sans MS"));
1850+
title.Alignment = Alignment.center;
1851+
1852+
// Insert a new Paragraph into the document.
1853+
Paragraph p1 = document.InsertParagraph();
1854+
1855+
// Append content to the Paragraph
1856+
p1.AppendLine("Just below there should be a picture ").Append("picture").Bold().Append(" inserted in a non-conventional way.");
1857+
p1.AppendLine();
1858+
p1.AppendLine("Check out this picture ").AppendPicture(picture).Append(" its funky don't you think?");
1859+
p1.AppendLine();
1860+
1861+
// Insert a new Paragraph into the document.
1862+
Paragraph p2 = document.InsertParagraph();
1863+
// Append content to the Paragraph.
1864+
1865+
p2.AppendLine("Is it correct?");
1866+
p2.AppendLine();
1867+
1868+
// Lets add another picture (without the fancy stuff)
1869+
Picture pictureNormal = image.CreatePicture();
1870+
1871+
Paragraph p3 = document.InsertParagraph();
1872+
p3.AppendLine("Lets add another picture (without the fancy rotation stuff)");
1873+
p3.AppendLine();
1874+
p3.AppendPicture(pictureNormal);
1875+
1876+
// Save this document.
1877+
document.Save();
1878+
Console.WriteLine("\tCreated: docs\\HelloWorldAddPictureToWord.docx\n");
1879+
}
1880+
}
18291881

18301882

18311883
}

0 commit comments

Comments
 (0)