Document .Net - Shapes

How to work with shapes using C# and VB.Net

Complete code

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

namespace Sample
{
    class Sample
    {
       
        static void Main(string[] args)
        {
            Shapes();
        }
        
		/// <summary>
        /// This sample shows how to work with shapes. 
        /// </summary>
        /// <remarks>
        /// Details: https://sautinsoft.com/products/document/examples/shapes.php
        /// </remarks>
        public static void Shapes()
        {
            string documentPath = @"Shapes.docx";

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

            // Create shape 1 with fill and outline.
            Shape shp1 = new Shape(dc, Layout.Floating(
                new HorizontalPosition(25f, LengthUnit.Millimeter, HorizontalPositionAnchor.LeftMargin),
                new VerticalPosition(20f, LengthUnit.Millimeter, VerticalPositionAnchor.TopMargin),
                new Size(200, 100)
                ));

            // Specify outline and fill using a picture.
            shp1.Outline.Fill.SetSolid(Color.DarkGreen);
            shp1.Outline.Width = 2;

            // Set fill for this shape.
            shp1.Fill.SetSolid(Color.Orange);

            // Create shape 2 with some text inside, 100mm*20mm.
            Shape shp2 = new Shape(dc, Layout.Floating(
                new HorizontalPosition(100f, LengthUnit.Millimeter, HorizontalPositionAnchor.LeftMargin),
                new VerticalPosition(20f, LengthUnit.Millimeter, VerticalPositionAnchor.TopMargin),
                new Size(LengthUnitConverter.Convert(100f, LengthUnit.Millimeter, LengthUnit.Point),
                        LengthUnitConverter.Convert(20f, LengthUnit.Millimeter, LengthUnit.Point))
                ));

            // Specify outline and fill using a picture.
            shp2.Outline.Fill.SetSolid(Color.LightGray);
            shp2.Outline.Width = 0.5;

            // Create a new paragraph with a formatted text.
            Paragraph p = new Paragraph(dc);
            Run run1 = new Run(dc, "Welcome to International Software Developer conference!");
            run1.CharacterFormat.FontName = "Helvetica";
            run1.CharacterFormat.Size = 14f;
            run1.CharacterFormat.Italic = true;
            p.Inlines.Add(run1);

            // Add the paragraph into the shp2.Text property.
            shp2.Text.Blocks.Add(p);

            // Create shape 3 with some vertycal text inside.
            Shape shp3 = new Shape(dc, Layout.Floating(
                new HorizontalPosition(180f, LengthUnit.Millimeter, HorizontalPositionAnchor.LeftMargin),
                new VerticalPosition(50f, LengthUnit.Millimeter, VerticalPositionAnchor.TopMargin),
                new Size(LengthUnitConverter.Convert(10f, LengthUnit.Millimeter, LengthUnit.Point),
                        LengthUnitConverter.Convert(110f, LengthUnit.Millimeter, LengthUnit.Point))
                ));

            // Create a new paragraph with a formatted text.
            Paragraph p1 = new Paragraph(dc);
            Run run2 = new Run(dc, "Welcome to International Software Developer conference!");
            run2.CharacterFormat.FontName = "Calibri";
            run2.CharacterFormat.Size = 12f;
            run2.CharacterFormat.Bold = true;
            run2.CharacterFormat.FontColor = Color.Orange;
            
            p1.Inlines.Add(run2);

            // Add the paragraph into the shp3.Text property.
            shp3.Text.Blocks.Add(p1);
            shp3.Text.TextDirection = TextDirection.TopToBottom;

            // Add our shapes into the document.
            dc.Content.End.Insert(shp1.Content);
            dc.Content.End.Insert(shp2.Content);
            dc.Content.End.Insert(shp3.Content);
            // Add our shapes into the document.
            dc.Content.End.Insert(shp1.Content);
            dc.Content.End.Insert(shp2.Content);

            // Save the document to DOCX format.
            dc.Save(documentPath);

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

Download.

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

Module Sample
    Sub Main()
        Shapes()
    End Sub

    ''' <summary>
    ''' This sample shows how to work with shapes. 
    ''' </summary>
    ''' <remarks>
    ''' Details: https://sautinsoft.com/products/document/examples/shapes.php
    ''' </remarks>
      Sub Shapes()
        Dim documentPath As String = "Shapes.docx"

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

        ' Create shape 1 with fill and outline.
        Dim shp1 As New Shape(dc, Layout.Floating(New HorizontalPosition(25.0F, LengthUnit.Millimeter, HorizontalPositionAnchor.LeftMargin), New VerticalPosition(20.0F, LengthUnit.Millimeter, VerticalPositionAnchor.TopMargin), New Size(200, 100)))

        ' Specify outline and fill using a picture.
        shp1.Outline.Fill.SetSolid(Color.DarkGreen)
        shp1.Outline.Width = 2

        ' Set fill for this shape.
        shp1.Fill.SetSolid(Color.Orange)

        ' Create shape 2 with some text inside, 100mm*20mm.
        Dim shp2 As New Shape(dc, Layout.Floating(New HorizontalPosition(100.0F, LengthUnit.Millimeter, HorizontalPositionAnchor.LeftMargin), New VerticalPosition(20.0F, LengthUnit.Millimeter, VerticalPositionAnchor.TopMargin), New Size(LengthUnitConverter.Convert(100.0F, LengthUnit.Millimeter, LengthUnit.Point), LengthUnitConverter.Convert(20.0F, LengthUnit.Millimeter, LengthUnit.Point))))

        ' Specify outline and fill using a picture.
        shp2.Outline.Fill.SetSolid(Color.LightGray)
        shp2.Outline.Width = 0.5

        ' Create a new paragraph with a formatted text.
        Dim p As New Paragraph(dc)
        Dim run1 As New Run(dc, "Welcome to International Software Developer conference!")
        run1.CharacterFormat.FontName = "Helvetica"
        run1.CharacterFormat.Size = 14.0F
        run1.CharacterFormat.Italic = True
        p.Inlines.Add(run1)

        ' Add the paragraph into the shp2.Text property.
        shp2.Text.Blocks.Add(p)

        ' Create shape 3 with some vertical text inside
        Dim shp3 As New Shape(dc, Layout.Floating(New HorizontalPosition(180.0F, LengthUnit.Millimeter, HorizontalPositionAnchor.LeftMargin), New VerticalPosition(50.0F, LengthUnit.Millimeter, VerticalPositionAnchor.TopMargin), New Size(LengthUnitConverter.Convert(10.0F, LengthUnit.Millimeter, LengthUnit.Point), LengthUnitConverter.Convert(110.0F, LengthUnit.Millimeter, LengthUnit.Point))))

        ' Create a new paragraph with a formatted text.
        Dim p1 As New Paragraph(dc)
        Dim run2 As New Run(dc, "Welcome to International Software Developer conference!")
        run2.CharacterFormat.FontName = "Calibri"
        run2.CharacterFormat.Size = 12.0F
        run2.CharacterFormat.Bold = True
        run2.CharacterFormat.FontColor = Color.Orange

        p1.Inlines.Add(run2)

        shp3.Text.Blocks.Add(p1)
        shp3.Text.TextDirection = TextDirection.TopToBottom

        ' Add our shapes into the document.
        dc.Content.End.Insert(shp1.Content)
        dc.Content.End.Insert(shp2.Content)
        dc.Content.End.Insert(shp3.Content)

        ' Save the document to DOCX format.
        dc.Save(documentPath)

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

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.

© SautinSoft 2002 - 2019