How to insert an image into a document of the specified size using C# and .NET


  1. Install SautinSoft.Document in your .NET application
  2. Create a simple document
  3. Insert image into the document using image file’s path
  4. Save document to your desired format

Complete code

using SautinSoft.Document;
using SautinSoft.Document.Drawing;

namespace Sample
{
    class Sample
    {

        static void Main(string[] args)
        {
            // Get your free 30 - day key here:   
            // https://sautinsoft.com/start-for-free/

            FullPageImage();
        }

        /// <summary>
        /// How to add pictures into a document. 
        /// </summary>
        /// <remarks>
        /// Details: https://sautinsoft.com/products/document/help/net/developer-guide/how-to-map-image-csharp-net.php
        /// </remarks>
        public static void FullPageImage()
        {
            string documentPath = @"pictures.docx";
            string pictPath = @"..\..\..\image-png.png";

            // Let's create a simple document.
            DocumentCore dc = new DocumentCore();

            // Add a new section, A5 Landscape, and custom page margins.
            Section s = new Section(dc);
            s.PageSetup.PaperType = PaperType.A5;
            s.PageSetup.Orientation = Orientation.Landscape;
            s.PageSetup.PageMargins = new PageMargins()
            {
                Top = LengthUnitConverter.Convert(0, LengthUnit.Millimeter, LengthUnit.Point),
                Right = LengthUnitConverter.Convert(0, LengthUnit.Inch, LengthUnit.Point),
                Bottom = LengthUnitConverter.Convert(0, LengthUnit.Millimeter, LengthUnit.Point),
                Left = LengthUnitConverter.Convert(0, LengthUnit.Inch, LengthUnit.Point)
            };
            dc.Sections.Add(s);

            // Create a new paragraph with picture.
            Paragraph par = new Paragraph(dc);
            s.Blocks.Add(par);

            // Our picture has InlineLayout - it doesn't have positioning by coordinates
            // and located as flowing content together with text (Run and other Inline elements).
            Picture pict1 = new Picture(dc, InlineLayout.Inline(new Size(s.PageSetup.PageWidth, s.PageSetup.PageHeight)), pictPath);

            // Add picture to the paragraph.
            par.Inlines.Add(pict1);

            // Save our document into DOCX format.
            dc.Save(documentPath);

            // Save our document into PDF format.
            dc.Save(@"PdfDocument.pdf");

            // Open the result for demonstration purposes.
            System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo(documentPath) { UseShellExecute = true });
            System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo(@"PdfDocument.pdf") { UseShellExecute = true });
        }
    }
}

Download

Imports SautinSoft.Document
Imports SautinSoft.Document.Drawing

Namespace Sample
	Friend Class Sample

		Shared Sub Main(ByVal args() As String)
			' Get your free 30 - day key here:   
			' https://sautinsoft.com/start-for-free/

			FullPageImage()
		End Sub

		''' <summary>
		''' How to add pictures into a document. 
		''' </summary>
		''' <remarks>
		''' Details: https://sautinsoft.com/products/document/help/net/developer-guide/how-to-map-image-csharp-net.php
		''' </remarks>
		Public Shared Sub FullPageImage()
			Dim documentPath As String = "pictures.docx"
			Dim pictPath As String = "..\..\..\image-png.png"

			' Let's create a simple document.
			Dim dc As New DocumentCore()

			' Add a new section, A5 Landscape, and custom page margins.
			Dim s As New Section(dc)
			s.PageSetup.PaperType = PaperType.A5
			s.PageSetup.Orientation = Orientation.Landscape
			s.PageSetup.PageMargins = New PageMargins() With {
				.Top = LengthUnitConverter.Convert(0, LengthUnit.Millimeter, LengthUnit.Point),
				.Right = LengthUnitConverter.Convert(0, LengthUnit.Inch, LengthUnit.Point),
				.Bottom = LengthUnitConverter.Convert(0, LengthUnit.Millimeter, LengthUnit.Point),
				.Left = LengthUnitConverter.Convert(0, LengthUnit.Inch, LengthUnit.Point)
			}
			dc.Sections.Add(s)

			' Create a new paragraph with picture.
			Dim par As New Paragraph(dc)
			s.Blocks.Add(par)

			' Our picture has InlineLayout - it doesn't have positioning by coordinates
			' and located as flowing content together with text (Run and other Inline elements).
			Dim pict1 As New Picture(dc, InlineLayout.Inline(New Size(s.PageSetup.PageWidth, s.PageSetup.PageHeight)), pictPath)

			' Add picture to the paragraph.
			par.Inlines.Add(pict1)

			' Save our document into DOCX format.
			dc.Save(documentPath)

			' Save our document into PDF format.
			dc.Save("PdfDocument.pdf")

			' Open the result for demonstration purposes.
			System.Diagnostics.Process.Start(New System.Diagnostics.ProcessStartInfo(documentPath) With {.UseShellExecute = True})
			System.Diagnostics.Process.Start(New System.Diagnostics.ProcessStartInfo("PdfDocument.pdf") 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.