Как вставить изображение в документ указанного размера на C# и .NET

  1. Добавьте SautinSoft.Document из Nuget.
  2. Создайте новый документ.
  3. Создайте новый абзац с изображением.
  4. Сохраните документ в формате DOCX.

Полный код

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

namespace Sample
{
    class Sample
    {

        static void Main(string[] args)
        {
            // Get your free 100 - 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 100 - 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


Если вам нужен пример кода или у вас есть вопрос: напишите нам по адресу support@sautinsoft.ru или спросите в онлайн-чате (правый нижний угол этой страницы) или используйте форму ниже:



Вопросы и предложения всегда приветствуются!

Мы разрабатываем компоненты .Net с 2002 года. Мы знаем форматы PDF, DOCX, RTF, HTML, XLSX и Images. Если вам нужна помощь в создании, изменении или преобразовании документов в различных форматах, мы можем вам помочь. Мы напишем для вас любой пример кода абсолютно бесплатно.