Add Pictures to Word Documents in C# and VB.NET

A Picture element is a type of Shape element that contains an image. GemBox.Document supports pictures of various PictureFormats like PNG, JPEG, GIF, TIFF, SVG, EMF and WMF. This element allows you to add pictures to your Word documents using C# and VB.NET

Pictures can be inline (of InlineLayout type), in which case the position of an image is constrained to the lines of text on the page. Alternatively, they can be floating (of FloatingLayout type), in which case they can be positioned anywhere on the page.

You can read more about this on the Shapes and Layout help page.

You can replace an image data using the Picture.PictureStream property. Images in SVG format are rendered in PDF files as vector graphics, resulting in smaller file sizes and better quality than bitmap images.

The example below shows how to create pictures of different formats, sizes, and positions.

Word document with Picture elements
Screenshot of Word file with pictures
using GemBox.Document;

class Program
{
    static void Main()
    {
        // If using the Professional version, put your serial key below.
        ComponentInfo.SetLicense("FREE-LIMITED-KEY");

        var document = new DocumentModel();

        var section = new Section(document);
        document.Sections.Add(section);

        var paragraph = new Paragraph(document);
        section.Blocks.Add(paragraph);

        // Create and add an inline picture with GIF image.
        Picture picture1 = new Picture(document, "%#Zahnrad.gif%", 61, 53, LengthUnit.Pixel);
        paragraph.Inlines.Add(picture1);

        // Create and add a floating picture with PNG image.
        Picture picture2 = new Picture(document, "%#Dices.png%");
        FloatingLayout layout2 = new FloatingLayout(
            new HorizontalPosition(HorizontalPositionType.Left, HorizontalPositionAnchor.Page),
            new VerticalPosition(2, LengthUnit.Inch, VerticalPositionAnchor.Page),
            picture2.Layout.Size);
        layout2.WrappingStyle = TextWrappingStyle.InFrontOfText;

        picture2.Layout = layout2;
        paragraph.Inlines.Add(picture2);

        // Create and add a floating picture with SVG image.
        Picture picture3 = new Picture(document, "%#Graphics1.svg%", 400, 200, LengthUnit.Pixel);
        FloatingLayout layout3 = new FloatingLayout(
            new HorizontalPosition(3.5, LengthUnit.Inch, HorizontalPositionAnchor.Page),
            new VerticalPosition(2, LengthUnit.Inch, VerticalPositionAnchor.Page),
            picture3.Layout.Size);
        layout3.WrappingStyle = TextWrappingStyle.BehindText;

        picture3.Layout = layout3;
        paragraph.Inlines.Add(picture3);

        document.Save("Pictures.%OutputFileType%");
    }
}
Imports GemBox.Document

Module Program

    Sub Main()

        ' If using the Professional version, put your serial key below.
        ComponentInfo.SetLicense("FREE-LIMITED-KEY")

        Dim document As New DocumentModel()

        Dim section As New Section(document)
        document.Sections.Add(section)

        Dim paragraph As New Paragraph(document)
        section.Blocks.Add(paragraph)

        ' Create and add an inline picture with GIF image.
        Dim picture1 As New Picture(document, "%#Zahnrad.gif%", 61, 53, LengthUnit.Pixel)
        paragraph.Inlines.Add(picture1)

        ' Create and add a floating picture with PNG image.
        Dim picture2 As New Picture(document, "%#Dices.png%")
        Dim layout2 As New FloatingLayout(
            New HorizontalPosition(HorizontalPositionType.Left, HorizontalPositionAnchor.Page),
            New VerticalPosition(2, LengthUnit.Inch, VerticalPositionAnchor.Page),
            picture2.Layout.Size)
        layout2.WrappingStyle = TextWrappingStyle.InFrontOfText

        picture2.Layout = layout2
        paragraph.Inlines.Add(picture2)

        ' Create and add a floating picture with SVG image.
        Dim picture3 As New Picture(document, "%#Graphics1.svg%", 400, 200, LengthUnit.Pixel)
        Dim layout3 As New FloatingLayout(
            New HorizontalPosition(3.5, LengthUnit.Inch, HorizontalPositionAnchor.Page),
            New VerticalPosition(2, LengthUnit.Inch, VerticalPositionAnchor.Page),
            picture3.Layout.Size)
        layout3.WrappingStyle = TextWrappingStyle.BehindText

        picture3.Layout = layout3
        paragraph.Inlines.Add(picture3)

        document.Save("Pictures.%OutputFileType%")

    End Sub
End Module

Even though GemBox.Document provides quite a few Picture constructors that can enable you to specify the element's size and layout, you can also resize the element after inserting it into a document with the Picture.Layout.Size property.

The following example shows how you can add large images and resize them to fit the document's page.

Word document with large picture that is resized to fit the page
Screenshot of large picture resized to fit Word page
using System;
using GemBox.Document;

class Program
{
    static void Main()
    {
        // If using the Professional version, put your serial key below.
        ComponentInfo.SetLicense("FREE-LIMITED-KEY");

        var document = new DocumentModel();

        var section = new Section(document);
        document.Sections.Add(section);

        var picture = new Picture(document, "%#Jellyfish.jpg%");
        section.Blocks.Add(new Paragraph(document, picture));

        var pictureLayout = picture.Layout;
        var pictureSize = pictureLayout.Size;

        var pageSetup = section.PageSetup;
        var pageSize = new Size(
            pageSetup.PageWidth - pageSetup.PageMargins.Left - pageSetup.PageMargins.Right,
            pageSetup.PageHeight - pageSetup.PageMargins.Top - pageSetup.PageMargins.Bottom);

        double ratioX = pageSize.Width / pictureSize.Width;
        double ratioY = pageSize.Height / pictureSize.Height;
        double ratio = Math.Min(ratioX, ratioY);

        // Resize picture element's size.
        if (ratio < 1)
            pictureLayout.Size = new Size(pictureSize.Width * ratio, pictureSize.Height * ratio);

        document.Save("LargePicture.%OutputFileType%");
    }
}
Imports System
Imports GemBox.Document

Module Program

    Sub Main()

        ' If using the Professional version, put your serial key below.
        ComponentInfo.SetLicense("FREE-LIMITED-KEY")

        Dim document As New DocumentModel()

        Dim section As New Section(document)
        document.Sections.Add(section)

        Dim picture As New Picture(document, "%#Jellyfish.jpg%")
        section.Blocks.Add(New Paragraph(document, picture))

        Dim pictureLayout = picture.Layout
        Dim pictureSize = pictureLayout.Size

        Dim pageSetup = section.PageSetup
        Dim pageSize = New Size(
            pageSetup.PageWidth - pageSetup.PageMargins.Left - pageSetup.PageMargins.Right,
            pageSetup.PageHeight - pageSetup.PageMargins.Top - pageSetup.PageMargins.Bottom)

        Dim ratioX As Double = pageSize.Width / pictureSize.Width
        Dim ratioY As Double = pageSize.Height / pictureSize.Height
        Dim ratio As Double = Math.Min(ratioX, ratioY)

        ' Resize picture element's size.
        If ratio < 1 Then
            pictureLayout.Size = New Size(pictureSize.Width * ratio, pictureSize.Height * ratio)
        End If

        document.Save("LargePicture.%OutputFileType%")

    End Sub
End Module

See also


Next steps

GemBox.Document is a .NET component that enables you to read, write, edit, convert, and print document files from your .NET applications using one simple API. How about testing it today?

Download Buy