Use DocumentBuilder to inserting an Image and Shape within C# and .NET

  1. Add SautinSoft.Document from Nuget.
  2. Create a new document.
  3. Create a Builder based on the document.
  4. Use the methods InsertImage and InsertShape.

   Method DocumentBuilderInsertImage allows you to insert from a byte array (stream, file) an inline or floating image with specified position and size. This method returns a Picture object that was just created and inserted so you can further modify properties of the Image.
   Method DocumentBuilderInsertShape allows you to insert inline shape with specified figure and size, inserts floating shape with specified position, size and text wrap style. This method returns a Shape object that was just created and inserted so you can further modify properties of the Shape.

Complete code

using System;
using SautinSoft.Document;
using System.Text;
using SautinSoft.Document.Drawing;

namespace Example
{
    class Program
    {
        static void Main(string[] args)
        {
            // Get your free 30-day key here:   
            // https://sautinsoft.com/start-for-free/
            InsertingImage();
        }
        /// <summary>
        /// Insert an image and shape inline or in the specified position using DocumentBuilder.
        /// </summary>
        /// <remarks>
        /// Details: https://www.sautinsoft.com/products/document/help/net/developer-guide/documentbuilder-inserting-image.php
        /// </remarks>

        static void InsertingImage()
        {
            DocumentCore dc = new DocumentCore();
            DocumentBuilder db = new DocumentBuilder(dc);

            string resultPath = @"Result.docx";
            string pictPath = @"..\..\..\logo.png";

            // Insert the formatted text into the document using DocumentBuilder.
            db.CharacterFormat.FontName = "Courier";
            db.CharacterFormat.Size = 17f;
            db.CharacterFormat.Italic = true;
            db.CharacterFormat.FontColor = Color.Orange;
            db.Writeln("Insert an Image and Shape using DocumentBuilder.");

            // Images:
            // 1st way: Insert an Inline image into the document.
            // Specify the image size and rotation (if required).
            Picture pict1 = db.InsertImage(pictPath, new Size(100, 30, LengthUnit.Millimeter));
            pict1.Rotation = -3;

            // 2nd way: Insert a Floating image from a file at the specified position.
            Picture pict2 = db.InsertImage(pictPath, new HorizontalPosition(1, LengthUnit.Centimeter, HorizontalPositionAnchor.LeftMargin),
                 new VerticalPosition(8, LengthUnit.Centimeter, VerticalPositionAnchor.TopMargin), WrappingStyle.InFrontOfText);

            // Shapes:
            // 1st way: Insert an Inline shape.
            Shape shp1 = db.InsertShape(Figure.SmileyFace, new Size(3, 3, LengthUnit.Centimeter));

            // 2nd way: Insert a Floating shape with specified position, size and text wrap style.
            Size size1 = new Size(7, 6, LengthUnit.Centimeter);
            Shape shp2 = db.InsertShape(Figure.RoundRectangle, new HorizontalPosition(8, LengthUnit.Centimeter,HorizontalPositionAnchor.LeftMargin), 
                new VerticalPosition(10, LengthUnit.Centimeter, VerticalPositionAnchor.TopMargin), WrappingStyle.InFrontOfText, new Size(7, 6, LengthUnit.Centimeter));

            shp2.Fill.SetSolid(Color.White);

            // Move the "cursor" position inside the shape content.
            db.MoveTo(shp2.Text.Blocks.Content.Start);
            db.CharacterFormat.FontColor = Color.Green;
            db.CharacterFormat.Size = 26f;
            db.Writeln("Text inside Shape.");

            // Move the "cursor" back to the paragraph with "shp1".
            db.MoveTo(shp1.Content.End);            

            // Save our document into DOCX format.
            dc.Save(resultPath, new DocxSaveOptions());

            // Open the result for demonstration purposes.
            System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo(resultPath) { UseShellExecute = true });
        }
    }
}

Download

Imports System
Imports SautinSoft.Document
Imports System.Text
Imports SautinSoft.Document.Drawing

Namespace Example
	Friend Class Program
		Shared Sub Main(ByVal args() As String)
			InsertingImage()
		End Sub
                ''' Get your free 30-day key here:   
                ''' https://sautinsoft.com/start-for-free/
		''' <summary>
		''' Insert an image and shape inline or in the specified position using DocumentBuilder.
		''' </summary>
		''' <remarks>
		''' Details: https://www.sautinsoft.com/products/document/help/net/developer-guide/documentbuilder-inserting-image.php
		''' </remarks>

		Private Shared Sub InsertingImage()
			Dim dc As New DocumentCore()
			Dim db As New DocumentBuilder(dc)

			Dim resultPath As String = "result.docx"
			Dim pictPath As String = "..\..\..\logo.png"

			' Insert the formatted text into the document using DocumentBuilder.
			db.CharacterFormat.FontName = "Courier"
			db.CharacterFormat.Size = 17.0F
			db.CharacterFormat.Italic = True
			db.CharacterFormat.FontColor = Color.Orange
			db.Writeln("Insert an Image and Shape using DocumentBuilder.")

			' Images:
			' 1st way: Insert an Inline image into the document.
			' Specify the image size and rotation (if required).
			Dim pict1 As Picture = db.InsertImage(pictPath, New Size(100, 30, LengthUnit.Millimeter))
			pict1.Rotation = -3

			' 2nd way: Insert a Floating image from a file at the specified position.
			Dim pict2 As Picture = db.InsertImage(pictPath, New HorizontalPosition(1, LengthUnit.Centimeter, HorizontalPositionAnchor.LeftMargin), New VerticalPosition(8, LengthUnit.Centimeter, VerticalPositionAnchor.TopMargin), WrappingStyle.InFrontOfText)

			' Shapes:
			' 1st way: Insert an Inline shape.
			Dim shp1 As Shape = db.InsertShape(Figure.SmileyFace, New Size(3, 3, LengthUnit.Centimeter))

			' 2nd way: Insert a Floating shape with specified position, size and text wrap style.
			Dim size1 As New Size(7, 6, LengthUnit.Centimeter)
			Dim shp2 As Shape = db.InsertShape(Figure.RoundRectangle, New HorizontalPosition(8, LengthUnit.Centimeter, HorizontalPositionAnchor.LeftMargin), New VerticalPosition(10, LengthUnit.Centimeter, VerticalPositionAnchor.TopMargin), WrappingStyle.InFrontOfText, New Size(7, 6, LengthUnit.Centimeter))

			shp2.Fill.SetSolid(Color.White)

			' Move the "cursor" position inside the shape content.
			db.MoveTo(shp2.Text.Blocks.Content.Start)
			db.CharacterFormat.FontColor = Color.Green
			db.CharacterFormat.Size = 26.0F
			db.Writeln("Text inside Shape.")

			' Move the "cursor" back to the paragraph with "shp1".
			db.MoveTo(shp1.Content.End)

			' Save our document into DOCX format.
			dc.Save(resultPath, New DocxSaveOptions())

			' Open the result for demonstration purposes.
			System.Diagnostics.Process.Start(New System.Diagnostics.ProcessStartInfo(resultPath) With {.UseShellExecute = True})
		End Sub
	End Class
End Namespace

Download


If you need a new code example or have a question: email us at support@sautinsoft.com or ask at Online Chat (right-bottom corner of this page) or use the Form below:



Questions and suggestions from you are always welcome!

We are developing .Net components since 2002. We know PDF, DOCX, RTF, HTML, XLSX and Images formats. If you need any assistance with creating, modifying or converting documents in various formats, we can help you. We will write any code example for you absolutely free.