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

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

Давайте создадим простой документ в PNG формате с двумя абзацами, содержащими форматированный текст и графику, как показано на рисунке:

Здесь мы покажем Вам два способа создания одного и того же документа (PNG) с нуля. Используйте любой из них, который Вам более понятен:

  1. Используя DocumentBuilder.

  2. Используя DOM (Объектная Модель Документа) напрямую.

Способ 1: Создание нового документа с помощью DocumentBuilder

DocumentBuilder — это мощный класс, связанный с DocumentCore и позволяющий динамически создавать документы с нуля или добавлять новые элементы в существующий документ.

Все, что возможно с DocumentBuilder, возможно и при непосредственном использовании классов DOM (Объектная Модель Документа), но обычно требует больше строк кода, чем при использовании DocumentBuilder.

        public static void CreateImageUsingDocumentBuilder()
        {
            // Set a path to our document.
            string docPath = @"Result-DocumentBuilder.png";

            // Create a new document and DocumentBuilder.
            DocumentCore dc = new DocumentCore();
            DocumentBuilder db = new DocumentBuilder(dc);

            // Set page size A4.
            Section section = db.Document.Sections[0];
            section.PageSetup.PaperType = PaperType.A4;

            // Add 1st paragraph with formatted text.
            db.CharacterFormat.FontName = "Verdana";
            db.CharacterFormat.Size = 16;
            db.CharacterFormat.FontColor = SautinSoft.Document.Color.Orange;
            db.Write("This is a first line in 1st paragraph!");
            // Add a line break into the 1st paragraph.
            db.InsertSpecialCharacter(SpecialCharacterType.LineBreak);
            // Add 2nd line to the 1st paragraph, create 2nd paragraph.
            db.Writeln("Let's type a second line.");
            // Specify the paragraph alignment.
            (section.Blocks[0] as Paragraph).ParagraphFormat.Alignment = HorizontalAlignment.Center;

            // Add text into the 2nd paragraph.
            db.CharacterFormat.ClearFormatting();
            db.CharacterFormat.Size = 25;
            db.CharacterFormat.FontColor = SautinSoft.Document.Color.Blue;
            db.CharacterFormat.Bold = true;
            db.Write("This is a first line in 2nd paragraph.");
            // Insert a line break into the 2nd paragraph.
            db.InsertSpecialCharacter(SpecialCharacterType.LineBreak);
            // Insert 2nd line with own formatting to the 2nd paragraph.
            db.CharacterFormat.Size = 20;
            db.CharacterFormat.FontColor = SautinSoft.Document.Color.DarkGreen;
            db.CharacterFormat.UnderlineStyle = UnderlineType.Single;
            db.CharacterFormat.Bold = false;
            db.Write("This is a second line.");

            // Add a graphics figure into the paragraph.
            db.CharacterFormat.ClearFormatting();
            Shape shape = db.InsertShape(SautinSoft.Document.Drawing.Figure.SmileyFace, new SautinSoft.Document.Drawing.Size(50, 50, LengthUnit.Millimeter));
            // Specify outline and fill.
            shape.Outline.Fill.SetSolid(new SautinSoft.Document.Color("#358CCB"));
            shape.Outline.Width = 3;
            shape.Fill.SetSolid(SautinSoft.Document.Color.Orange);            

            // Save the 1st document page to the file in PNG format.
            Bitmap page = dc.GetPaginator().Pages[0].Rasterize(300, SautinSoft.Document.Color.White);
            page.Save(docPath);

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

Способ 2: создание нового документа используя DOM

DOM (объектная модель документа) очень проста и понятна, это представление документа Word в памяти.

        public static void CreateImageUsingDOM()
        {
            // Set a path to our document.
            string docPath = @"Result-DocumentCore.png";

            // Create a new document.
            DocumentCore dc = new DocumentCore();

            // Add new section.
            Section section = new Section(dc);
            dc.Sections.Add(section);

            // Let's set page size A4.
            section.PageSetup.PaperType = PaperType.A4;

            // Add two paragraphs            
            Paragraph par1 = new Paragraph(dc);
            par1.ParagraphFormat.Alignment = HorizontalAlignment.Center;
            section.Blocks.Add(par1);

            // Let's create a characterformat for text in the 1st paragraph.
            CharacterFormat cf = new CharacterFormat() { FontName = "Verdana", Size = 16, FontColor = SautinSoft.Document.Color.Orange };
            Run run1 = new Run(dc, "This is a first line in 1st paragraph!");
            run1.CharacterFormat = cf;
            par1.Inlines.Add(run1);

            // Let's add a line break into the 1st paragraph.
            par1.Inlines.Add(new SpecialCharacter(dc, SpecialCharacterType.LineBreak));
            // Copy the formatting.
            Run run2 = run1.Clone();
            run2.Text = "Let's type a second line.";
            par1.Inlines.Add(run2);

            // Add 2nd paragraph.
            Paragraph par2 = new Paragraph(dc, new Run(dc, "This is a first line in 2nd paragraph.", new CharacterFormat() { Size = 25, FontColor = SautinSoft.Document.Color.Blue, Bold = true }));
            section.Blocks.Add(par2);
            SpecialCharacter lBr = new SpecialCharacter(dc, SpecialCharacterType.LineBreak);
            par2.Inlines.Add(lBr);
            Run run3 = new Run(dc, "This is a second line.", new CharacterFormat() { Size = 20, FontColor = SautinSoft.Document.Color.DarkGreen, UnderlineStyle = UnderlineType.Single });
            par2.Inlines.Add(run3);

            // Add a graphics figure into the paragraph.
            Shape shape = new Shape(dc, new InlineLayout(new SautinSoft.Document.Drawing.Size(50, 50, LengthUnit.Millimeter)));
            // Specify outline and fill.
            shape.Outline.Fill.SetSolid(new  SautinSoft.Document.Color("#358CCB"));
            shape.Outline.Width = 3;
            shape.Fill.SetSolid(SautinSoft.Document.Color.Orange);            
            shape.Geometry.SetPreset(Figure.SmileyFace);
            par2.Inlines.Add(shape);

            // Save the 1st document page to the file in PNG format.
            Bitmap page = dc.GetPaginator().Pages[0].Rasterize(300, SautinSoft.Document.Color.White);
            page.Save(docPath);

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

Как видите, в методе Rasterize() мы указали DPI и цвет в качестве параметров. Смотрите другие примеры Сохранить документ как изображение.

Полный код

using SautinSoft.Document;
using SautinSoft.Document.Drawing;
using SkiaSharp;
using System.Drawing;
using System.IO;

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

            // Here we'll show two ways to create Image document from a scratch.
            // Use any of them, which is more clear to you.

            // 1. With help of DocumentBuilder (wizard).
            CreateImageUsingDocumentBuilder();

            // 2. With Document Object Model (DOM) directly.
            CreateImageUsingDOM();
        }
        /// <summary>
        /// Creates a new Image document using DocumentBuilder wizard.
        /// </summary>
        /// <remarks>
        /// Details: https://sautinsoft.com/products/document/help/net/developer-guide/create-image-document-net-csharp-vb.php
        /// </remarks>
        public static void CreateImageUsingDocumentBuilder()
        {
            // Set a path to our document.
            string docPath = @"Result-DocumentBuilder.png";

            // Create a new document and DocumentBuilder.
            DocumentCore dc = new DocumentCore();
            DocumentBuilder db = new DocumentBuilder(dc);

            // Set page size A4.
            Section section = db.Document.Sections[0];
            section.PageSetup.PaperType = PaperType.A4;

            // Add 1st paragraph with formatted text.
            db.CharacterFormat.FontName = "Verdana";
            db.CharacterFormat.Size = 16;
            db.CharacterFormat.FontColor = SautinSoft.Document.Color.Orange;
            db.Write("This is a first line in 1st paragraph!");
            // Add a line break into the 1st paragraph.
            db.InsertSpecialCharacter(SpecialCharacterType.LineBreak);
            // Add 2nd line to the 1st paragraph, create 2nd paragraph.
            db.Writeln("Let's type a second line.");
            // Specify the paragraph alignment.
            (section.Blocks[0] as Paragraph).ParagraphFormat.Alignment = HorizontalAlignment.Center;

            // Add text into the 2nd paragraph.
            db.CharacterFormat.ClearFormatting();
            db.CharacterFormat.Size = 25;
            db.CharacterFormat.FontColor = SautinSoft.Document.Color.Blue;
            db.CharacterFormat.Bold = true;
            db.Write("This is a first line in 2nd paragraph.");
            // Insert a line break into the 2nd paragraph.
            db.InsertSpecialCharacter(SpecialCharacterType.LineBreak);
            // Insert 2nd line with own formatting to the 2nd paragraph.
            db.CharacterFormat.Size = 20;
            db.CharacterFormat.FontColor = SautinSoft.Document.Color.DarkGreen;
            db.CharacterFormat.UnderlineStyle = UnderlineType.Single;
            db.CharacterFormat.Bold = false;
            db.Write("This is a second line.");

            // Add a graphics figure into the paragraph.
            db.CharacterFormat.ClearFormatting();
            Shape shape = db.InsertShape(SautinSoft.Document.Drawing.Figure.SmileyFace, new SautinSoft.Document.Drawing.Size(50, 50, LengthUnit.Millimeter));
            // Specify outline and fill.
            shape.Outline.Fill.SetSolid(new SautinSoft.Document.Color("#358CCB"));
            shape.Outline.Width = 3;
            shape.Fill.SetSolid(SautinSoft.Document.Color.Orange);            

            // Save the 1st document page to the file in PNG format.
            ImageSaveOptions options = new ImageSaveOptions();
            options.DpiX = 300;
            options.DpiY = 300;
            var a = dc.GetPaginator().Pages[0];
            SKBitmap page = dc.GetPaginator().Pages[0].Rasterize(options, SautinSoft.Document.Color.White);
            page.Encode(new FileStream(docPath, FileMode.Create), SKEncodedImageFormat.Png, 100);

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


        /// <summary>
        /// Creates a new Image document using DOM directly.
        /// </summary>
        /// <remarks>
        /// Details: https://sautinsoft.com/products/document/help/net/developer-guide/create-image-document-net-csharp-vb.php
        /// </remarks>
        public static void CreateImageUsingDOM()
        {
            // Set a path to our document.
            string docPath = @"Result-DocumentCore.png";

            // Create a new document.
            DocumentCore dc = new DocumentCore();

            // Add new section.
            Section section = new Section(dc);
            dc.Sections.Add(section);

            // Let's set page size A4.
            section.PageSetup.PaperType = PaperType.A4;

            // Add two paragraphs            
            Paragraph par1 = new Paragraph(dc);
            par1.ParagraphFormat.Alignment = HorizontalAlignment.Center;
            section.Blocks.Add(par1);

            // Let's create a characterformat for text in the 1st paragraph.
            CharacterFormat cf = new CharacterFormat() { FontName = "Verdana", Size = 16, FontColor = SautinSoft.Document.Color.Orange };
            Run run1 = new Run(dc, "This is a first line in 1st paragraph!");
            run1.CharacterFormat = cf;
            par1.Inlines.Add(run1);

            // Let's add a line break into the 1st paragraph.
            par1.Inlines.Add(new SpecialCharacter(dc, SpecialCharacterType.LineBreak));
            // Copy the formatting.
            Run run2 = run1.Clone();
            run2.Text = "Let's type a second line.";
            par1.Inlines.Add(run2);

            // Add 2nd paragraph.
            Paragraph par2 = new Paragraph(dc, new Run(dc, "This is a first line in 2nd paragraph.", new CharacterFormat() { Size = 25, FontColor = SautinSoft.Document.Color.Blue, Bold = true }));
            section.Blocks.Add(par2);
            SpecialCharacter lBr = new SpecialCharacter(dc, SpecialCharacterType.LineBreak);
            par2.Inlines.Add(lBr);
            Run run3 = new Run(dc, "This is a second line.", new CharacterFormat() { Size = 20, FontColor = SautinSoft.Document.Color.DarkGreen, UnderlineStyle = UnderlineType.Single });
            par2.Inlines.Add(run3);

            // Add a graphics figure into the paragraph.
            Shape shape = new Shape(dc, new InlineLayout(new SautinSoft.Document.Drawing.Size(50, 50, LengthUnit.Millimeter)));
            // Specify outline and fill.
            shape.Outline.Fill.SetSolid(new  SautinSoft.Document.Color("#358CCB"));
            shape.Outline.Width = 3;
            shape.Fill.SetSolid(SautinSoft.Document.Color.Orange);            
            shape.Geometry.SetPreset(Figure.SmileyFace);
            par2.Inlines.Add(shape);

            // Save the 1st document page to the file in PNG format.
            ImageSaveOptions options = new ImageSaveOptions();
            options.DpiX = 300;
            options.DpiY = 300;
            SKBitmap page = dc.GetPaginator().Pages[0].Rasterize(options, SautinSoft.Document.Color.White);
            page.Encode(new FileStream(docPath, FileMode.Create), SKEncodedImageFormat.Png, 100);

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

Download

Imports System.IO
Imports SautinSoft.Document
Imports SautinSoft.Document.Drawing
Imports SkiaSharp

Namespace Example
    Friend Class Program
        Shared Sub Main(ByVal args() As String)
            ' Here we'll show two ways to create Image document from a scratch.
            ' Use any of them, which is more clear to you.

            ' 1. With help of DocumentBuilder (wizard).
            CreateImageUsingDocumentBuilder()

            ' 2. With Document Object Model (DOM) directly.
            CreateImageUsingDOM()
        End Sub
        ''' Get your free 100-day key here:   
        ''' https://sautinsoft.com/start-for-free/
        ''' <summary>
        ''' Creates a new Image document using DocumentBuilder wizard.
        ''' </summary>
        ''' <remarks>
        ''' Details: https://sautinsoft.com/products/document/help/net/developer-guide/create-image-document-net-csharp-vb.php
        ''' </remarks>
        Public Shared Sub CreateImageUsingDocumentBuilder()
            ' Set a path to our document.
            Dim docPath As String = "Result-DocumentBuilder.png"

            ' Create a new document and DocumentBuilder.
            Dim dc As New DocumentCore()
            Dim db As New DocumentBuilder(dc)

            ' Set page size A4.
            Dim section As Section = db.Document.Sections(0)
            section.PageSetup.PaperType = PaperType.A4

            ' Add 1st paragraph with formatted text.
            db.CharacterFormat.FontName = "Verdana"
            db.CharacterFormat.Size = 16
            db.CharacterFormat.FontColor = SautinSoft.Document.Color.Orange
            db.Write("This is a first line in 1st paragraph!")
            ' Add a line break into the 1st paragraph.
            db.InsertSpecialCharacter(SpecialCharacterType.LineBreak)
            ' Add 2nd line to the 1st paragraph, create 2nd paragraph.
            db.Writeln("Let's type a second line.")
            ' Specify the paragraph alignment.
            TryCast(section.Blocks(0), Paragraph).ParagraphFormat.Alignment = HorizontalAlignment.Center

            ' Add text into the 2nd paragraph.
            db.CharacterFormat.ClearFormatting()
            db.CharacterFormat.Size = 25
            db.CharacterFormat.FontColor = SautinSoft.Document.Color.Blue
            db.CharacterFormat.Bold = True
            db.Write("This is a first line in 2nd paragraph.")
            ' Insert a line break into the 2nd paragraph.
            db.InsertSpecialCharacter(SpecialCharacterType.LineBreak)
            ' Insert 2nd line with own formatting to the 2nd paragraph.
            db.CharacterFormat.Size = 20
            db.CharacterFormat.FontColor = SautinSoft.Document.Color.DarkGreen
            db.CharacterFormat.UnderlineStyle = UnderlineType.Single
            db.CharacterFormat.Bold = False
            db.Write("This is a second line.")

            ' Add a graphics figure into the paragraph.
            db.CharacterFormat.ClearFormatting()
            Dim shape As Shape = db.InsertShape(SautinSoft.Document.Drawing.Figure.SmileyFace, New SautinSoft.Document.Drawing.Size(50, 50, LengthUnit.Millimeter))
            ' Specify outline and fill.
            shape.Outline.Fill.SetSolid(New SautinSoft.Document.Color("#358CCB"))
            shape.Outline.Width = 3
            shape.Fill.SetSolid(SautinSoft.Document.Color.Orange)

            ' Save the 1st document page to the file in PNG format.
            Dim dpi As ImageSaveOptions = New ImageSaveOptions
            dpi.DpiX = 300
            dpi.DpiY = 300

            Dim page As SKBitmap = dc.GetPaginator().Pages(0).Rasterize(dpi, SautinSoft.Document.Color.White)
            page.Encode(New FileStream(docPath, FileMode.Create), SKEncodedImageFormat.Png, 100)

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


        ''' <summary>
        ''' Creates a new Image document using DOM directly.
        ''' </summary>
        ''' <remarks>
        ''' Details: https://sautinsoft.com/products/document/help/net/developer-guide/create-image-document-net-csharp-vb.php
        ''' </remarks>
        Public Shared Sub CreateImageUsingDOM()
            ' Set a path to our document.
            Dim docPath As String = "Result-DocumentCore.png"

            ' Create a new document.
            Dim dc As New DocumentCore()

            ' Add new section.
            Dim section As New Section(dc)
            dc.Sections.Add(section)

            ' Let's set page size A4.
            section.PageSetup.PaperType = PaperType.A4

            ' Add two paragraphs            
            Dim par1 As New Paragraph(dc)
            par1.ParagraphFormat.Alignment = HorizontalAlignment.Center
            section.Blocks.Add(par1)

            ' Let's create a characterformat for text in the 1st paragraph.
            Dim cf As New CharacterFormat() With {
                .FontName = "Verdana",
                .Size = 16,
                .FontColor = SautinSoft.Document.Color.Orange
            }
            Dim run1 As New Run(dc, "This is a first line in 1st paragraph!")
            run1.CharacterFormat = cf
            par1.Inlines.Add(run1)

            ' Let's add a line break into the 1st paragraph.
            par1.Inlines.Add(New SpecialCharacter(dc, SpecialCharacterType.LineBreak))
            ' Copy the formatting.
            Dim run2 As Run = run1.Clone()
            run2.Text = "Let's type a second line."
            par1.Inlines.Add(run2)

            ' Add 2nd paragraph.
            Dim par2 As Paragraph = New Paragraph(dc, New Run(dc, "This is a first line in 2nd paragraph.", New CharacterFormat() With {
                .Size = 25,
                .FontColor = SautinSoft.Document.Color.Blue,
                .Bold = True
            }))
            section.Blocks.Add(par2)
            Dim lBr As New SpecialCharacter(dc, SpecialCharacterType.LineBreak)
            par2.Inlines.Add(lBr)
            Dim run3 As Run = New Run(dc, "This is a second line.", New CharacterFormat() With {
                .Size = 20,
                .FontColor = SautinSoft.Document.Color.DarkGreen,
                .UnderlineStyle = UnderlineType.Single
            })
            par2.Inlines.Add(run3)

            ' Add a graphics figure into the paragraph.
            Dim shape As New Shape(dc, New InlineLayout(New SautinSoft.Document.Drawing.Size(50, 50, LengthUnit.Millimeter)))
            ' Specify outline and fill.
            shape.Outline.Fill.SetSolid(New SautinSoft.Document.Color("#358CCB"))
            shape.Outline.Width = 3
            shape.Fill.SetSolid(SautinSoft.Document.Color.Orange)
            shape.Geometry.SetPreset(Figure.SmileyFace)
            par2.Inlines.Add(shape)

            ' Save the 1st document page to the file in PNG format.
            Dim dpi As ImageSaveOptions = New ImageSaveOptions
            dpi.DpiX = 300
            dpi.DpiY = 300

            Dim page As SKBitmap = dc.GetPaginator().Pages(0).Rasterize(dpi, SautinSoft.Document.Color.White)
            page.Encode(New FileStream(docPath, FileMode.Create), SKEncodedImageFormat.Png, 100)

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

Download


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



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

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