Image and Shape

Image and Shape (14)

Spire.Doc offers the property ShapeObject. HorizontalAlignment and ShapeObject. Vertical Alignment to enable developers to align the shapes horizontally or vertically. This article will show you to how to align the shapes on the word document in C#.

Step 1: Create a new instance of word document and load the document from file.

Document doc = new Document();
doc.LoadFromFile("Sample.docx");

Step 2: Get the first section from file.

Section section = doc.Sections[0];

Step 3: Traverse the word document and set the horizontal alignment as left.

foreach (Paragraph para in section.Paragraphs)
{
    foreach (DocumentObject obj in para.ChildObjects)
    {
        if (obj is ShapeObject)
        {
          (obj as ShapeObject).HorizontalAlignment = ShapeHorizontalAlignment.Left;

        }
    }
}

Step 4: Save the document to file.

doc.SaveToFile("Result.docx", FileFormat.Docx);

Effective screenshot after setting the alignment of the shapes.

How to align the shape on word document in C#

How to align the shape on word document in C#

Full codes:

Document doc = new Document();
doc.LoadFromFile("Sample.docx");

Section section = doc.Sections[0];

foreach (Paragraph para in section.Paragraphs)
{
    foreach (DocumentObject obj in para.ChildObjects)
    {
        if (obj is ShapeObject)
        {
            (obj as ShapeObject).HorizontalAlignment = ShapeHorizontalAlignment.Left;

            ////Set the vertical alignment as top
            //(obj as ShapeObject).VerticalAlignment = ShapeVerticalAlignment.Top;
        }
    }
}

doc.SaveToFile("Result.docx", FileFormat.Docx2013);

Spire.Doc supports to insert new shapes to the word document and from version 6.4.11, Spire.Doc public the property ShapeObject.Rotation to enable developers to rotate the shapes. This article will show you to how to rotate the shapes on the word document in C#.

Step 1: Create a new instance of word document and load the document from file.

Document doc = new Document();
doc.LoadFromFile("Sample.docx");

Step 2: Get the first section from file.

Section section = doc.Sections[0];

Step 3: Traverse the word document and set the shape rotation as 10.

foreach (Paragraph para in section.Paragraphs)
{
    foreach (DocumentObject obj in para.ChildObjects)
    {
        if (obj is ShapeObject)
        {
            (obj as ShapeObject).Rotation = 10.0;
        }
    }
}

Step 4: Save the document to file.

doc.SaveToFile("Result.docx", FileFormat.Docx);

Effective screenshot after rotate the shapes:

How to rotate the shape on word document in C#

Full codes:

Document doc = new Document();
doc.LoadFromFile("Sample.docx");

Section section = doc.Sections[0];

foreach (Paragraph para in section.Paragraphs)
{
    foreach (DocumentObject obj in para.ChildObjects)
    {
        if (obj is ShapeObject)
        {
            (obj as ShapeObject).Rotation = 10.0;
        }
    }
}

doc.SaveToFile("Result.docx", FileFormat.Docx);

This tutorial is going to show you how to make a color of a image transeperant using Spire.Doc.

Below screenshot shows an example image with black and white colors:

Set Transeperant Color for Images in Word in C#

Detail steps:

Step 1: Instantiate a Document object and Load the Word file.

Document doc = new Document();
doc.LoadFromFile("Input.docx");

Step 2: Get the first Paragraph in the first section.

Paragraph paragraph = doc.Sections[0].Paragraphs[0];

Step 3: Set the black color of the image(s) in the paragraph to transperant.

foreach (DocumentObject obj in paragraph.ChildObjects)
{
    if (obj is DocPicture)
    {
        (obj as DocPicture).TransparentColor = Color.Black;
    }
}

Step 4: Save the file.

doc.SaveToFile("Result.docx", FileFormat.Docx2013);

Screenshot:

Set Transeperant Color for Images in Word in C#

Full code:

//Instantiate a Document object
Document doc = new Document();
//Load the Word file
doc.LoadFromFile("Input.docx");

//Get the first paragraph in the first section
Paragraph paragraph = doc.Sections[0].Paragraphs[0];

//Set the black color of the image(s) in the paragraph to transperant
foreach (DocumentObject obj in paragraph.ChildObjects)
{
    if (obj is DocPicture)
    {
        (obj as DocPicture).TransparentColor = Color.Black;
    }
}

//Save the file
doc.SaveToFile("Result.docx", FileFormat.Docx2013);
Tuesday, 06 February 2018 07:32

Resize Images in a Word document in C#

Written by support iceblue

We often need to resize images to a desired size after we insert them into a Word document. This article demonstrates how to programmatically resize images in a Word document using Spire.Doc and C#.

Below is the screenshot of the example document we used for demonstration:

Resize Images in a Word document in C#

Detail steps:

Step 1: Load the Word document.

Document document = new Document("Input.docx");

Step 2: Get the first section and the first paragraph in the section.

Section section = document.Sections[0];
Paragraph paragraph = section.Paragraphs[0];

Step 3: Resize images in the paragraph.

foreach (DocumentObject docObj in paragraph.ChildObjects)
{
    if (docObj is DocPicture)
    {
        DocPicture picture = docObj as DocPicture;
        picture.Width = 50f;
        picture.Height = 50f;
    }
} 

Step 4: Save the document.

document.SaveToFile("ResizeImages.docx");

Output:

Resize Images in a Word document in C#

Full code:

Document document = new Document("Input.docx");
Section section = document.Sections[0];
Paragraph paragraph = section.Paragraphs[0];
 
foreach (DocumentObject docObj in paragraph.ChildObjects)
{
    if (docObj is DocPicture)
    {
        DocPicture picture = docObj as DocPicture;
        picture.Width = 50f;
        picture.Height = 50f;
    }
} 
document.SaveToFile("ResizeImages.docx");

Spire.Doc supports to add new shape and shape group to the word document, it also supports to remove the shapes from the word. This article will show you how to reset the size of shape on an existing word document via Spire.Doc.

Firstly, view the original shape on the word document:

C# reset the size of shape on the word

Step 1: Create a new instance of word document and load the document from file.

Document doc = new Document();
doc.LoadFromFile("Sample.docx",FileFormat.Docx2010);

Step 2: Get the first section and the third paragraph that contains the shape.

Section section = doc.Sections[0]; 
Paragraph para = section.Paragraphs[2];

Step 3: Get the shape and reset the width and height for the shape.

ShapeObject shape = para.ChildObjects[0] as ShapeObject;

shape.Width = 400;
shape.Height = 300;

Step 4: Save the document to file.

doc.SaveToFile("result.docx",FileFormat.Docx2010);

Effective screenshot of the shape after reset the size.

C# reset the size of shape on the word

Full codes:

[C#]
Document doc = new Document();
doc.LoadFromFile("Sample.docx",FileFormat.Docx2010);

Section section = doc.Sections[0]; 
Paragraph para = section.Paragraphs[2];

ShapeObject shape = para.ChildObjects[0] as ShapeObject;

shape.Width = 400;
shape.Height = 300;

doc.SaveToFile("result.docx",FileFormat.Docx2010);
[VB.NET]
Dim doc As New Document()
doc.LoadFromFile("Sample.docx", FileFormat.Docx2010)

Dim section As Section = doc.Sections(0)
Dim para As Paragraph = section.Paragraphs(2)

Dim shape As ShapeObject = TryCast(para.ChildObjects(0), ShapeObject)

shape.Width = 400
shape.Height = 300

doc.SaveToFile("result.docx", FileFormat.Docx2010)

We have already demonstrated how to use Spire.Doc to add shapes to word document from code. Spire.Doc also supports to remove a single shape by index or clear all the shapes from the word document. This article will illustrates how to remove the shape from word document in C# and VB.NET.

Sample word document with shapes:

How to remove shape from word document in C# and VB.NET

Step 1: Initialize a new instance of Document class and load the document from file.

Document doc = new Document();
doc.LoadFromFile("Shapes.docx",FileFormat.Docx2010);

Step 2: Get the first section from the document and the first paragraph from the section.

Section section = doc.Sections[0];
Paragraph para = section.Paragraphs[0];

Step 3: Get shapes from the first paragraph.

ShapeObject shape = para.ChildObjects[0] as ShapeObject;

Step 4: Remove the shape or all the shapes.

//remove the third shape.
para.ChildObjects.RemoveAt(2);

////clear all the shapes.
//para.ChildObjects.Clear();

Step 5: Save the document to file.

doc.SaveToFile("Removeshape.docx",FileFormat.Docx2010);

Effective screenshot after removing one shape from the word document:

How to remove shape from word document in C# and VB.NET

Full codes:

[C#]
Document doc = new Document();
doc.LoadFromFile("Shapes.docx",FileFormat.Docx2010);

Section section = doc.Sections[0];

Paragraph para = section.Paragraphs[0];
ShapeObject shape = para.ChildObjects[0] as ShapeObject;

//remove the third shape.
para.ChildObjects.RemoveAt(2);

////clear all the shapes.
//para.ChildObjects.Clear();

doc.SaveToFile("Removeshape.docx",FileFormat.Docx2010);
[VB.NET]
Dim doc As New Document()
doc.LoadFromFile("Shapes.docx", FileFormat.Docx2010)
Dim section As Section = doc.Sections(0)
Dim para As Paragraph = section.Paragraphs(0)
Dim shape As ShapeObject = TryCast(para.ChildObjects(0), ShapeObject)
'remove the third shape.
para.ChildObjects.RemoveAt(2)
'''/clear all the shapes.
'para.ChildObjects.Clear();

doc.SaveToFile("Removeshape.docx", FileFormat.Docx2010)
Tuesday, 18 July 2017 07:20

How to Create WordArt in Word in C#, VB.NET

Written by support iceblue

Spire.Doc included a variety of WordArt types in the ShapeType enum. In order to create WordArt in Word, initialize an instance of ShapeObject and specify the WordArt type and content. The following sections show you how to create WordArt using Spire.Doc step by step.

Step 1: Create an instance of Document class.

Document doc = new Document();

Step 2: Add a section, add a paragraph in section.

Section section = doc.AddSection();
Paragraph paragraph = section.AddParagraph();

Step 3: Create a ShapeObject instance and specify TextWave4 as the ShapeType. Specify the content of WordArt.

ShapeObject shape= paragraph.AppendShape(120, 30, ShapeType.TextWave4);
shape.WordArt.Text = "Thanks for reading.";

Step 4: Set the position, fill color and stroke color of the shape (WordArt).

shape.VerticalPosition = 80;
shape.HorizontalPosition = 170;
shape.FillColor = System.Drawing.Color.Red;
shape.StrokeColor = System.Drawing.Color.Yellow;

Step 5: Save the file.

doc.SaveToFile("output.docx", FileFormat.Docx2013);

Output:

How to Create WordArt in Word in C#, VB.NET

Full Code:

[C#]
Document doc = new Document();
Section section = doc.AddSection();
Paragraph paragraph = section.AddParagraph();

ShapeObject shape= paragraph.AppendShape(120, 30, ShapeType.TextWave4);
shape.VerticalPosition = 80;
shape.HorizontalPosition = 170;
shape.WordArt.Text = "Thanks for reading.";
shape.FillColor = System.Drawing.Color.Red;
shape.StrokeColor = System.Drawing.Color.Yellow;

doc.SaveToFile("output.docx", FileFormat.Docx2013);
[VB.NET]
Dim doc As New Document()
Dim section As Section = doc.AddSection()
Dim paragraph As Paragraph = section.AddParagraph()

Dim shape As ShapeObject = paragraph.AppendShape(120, 30, ShapeType.TextWave4)
shape.VerticalPosition = 80
shape.HorizontalPosition = 170
shape.WordArt.Text = "Thanks for reading."
shape.FillColor = System.Drawing.Color.Red
shape.StrokeColor = System.Drawing.Color.Yellow

doc.SaveToFile("output.docx", FileFormat.Docx2013)

Replacing image with text or image in a Word document is possible in Spire.Doc. We've already illustrated how to replace image with text, in this tutorial, we will show you how to replace a specified image with another image using Spire.Doc and C#.

Below is the original Word document before replacing image:

Replace Image with new Image in Word using C#

Code Snippet:

Step 1: Load the Word document.

Document document = new Document("Input.docx");

Step 2: Replace the image which title is "Figure 1" with new image.

//Loop through the paragraphs of the section
foreach (Paragraph paragraph in document.Sections[0].Paragraphs)
{
    //Loop through the child elements of paragraph
    foreach (DocumentObject docObj in paragraph.ChildObjects)
    {
        if (docObj.DocumentObjectType == DocumentObjectType.Picture)
        {
            DocPicture picture = docObj as DocPicture;
            if (picture.Title == "Figure 1")
            {
                //Replace the image
                picture.LoadImage(Image.FromFile("PinkRoses.jpg"));
            }
        }
    }
}

Step 3: Save and close the document.

document.SaveToFile("ReplaceImage.docx");
document.Close();

The resultant document looks as follows:

Replace Image with new Image in Word using C#

Full code:

using System.Drawing;
using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;

namespace Replace_Image
{
    class Program
    {        
        static void Main(string[] args)
        {
            //Load the Word document
            Document document = new Document("Input.docx");

            //Loop through the paragraphs of the section
            foreach (Paragraph paragraph in document.Sections[0].Paragraphs)
            {
                //Loop through the child elements of paragraph
                foreach (DocumentObject docObj in paragraph.ChildObjects)
                {
                    if (docObj.DocumentObjectType == DocumentObjectType.Picture)
                    {
                        DocPicture picture = docObj as DocPicture;
                        if (picture.Title == "Figure 1")
                        {
                            //Replace the image
                            picture.LoadImage(Image.FromFile("PinkRoses.jpg"));
                        }
                    }
                }
            }

            //Save and close the document
            document.SaveToFile("ReplaceImage.docx");
            document.Close();            
        }
    }
}

MS Word allows users to select a shape from shapes menu, drag and place it to any desired location on the page. From Spire.Doc Version 6.0 or above, we added a new feature to work with shape using code. The following section will present how to insert shapes and shape group in a Word document at the specified locations using Spire.Doc.

Code Snippets:

Step 1: Initialize a new instance of Document class.

Document doc = new Document();

Step 2: Add a new section to Word document, and add a paragraph to the section.

Section sec = doc.AddSection();
Paragraph para1 =sec.AddParagraph();

Step 3: Add shapes to the paragraph by calling AppendShape() method. In order to locate where the shape will be placed, you can just set the HorizontalPosition and VerticalPosition properties of ShapeObject class. We can also format the shape by set the FillColor,StrokeColor and LineStyle properties.

ShapeObject shape1 = para1.AppendShape(50, 50, ShapeType.Heart);

shape1.FillColor = Color.Red;
shape1.StrokeColor = Color.Red;
shape1.HorizontalPosition = 200;
shape1.VerticalPosition = 100;
    
ShapeObject shape2 = para1.AppendShape(100, 100, ShapeType.Arrow);
                            
shape2.FillColor = Color.Purple;
shape2.StrokeColor = Color.Black;
shape2.LineStyle = ShapeLineStyle.Double;
shape2.StrokeWeight = 3;
shape2.HorizontalPosition = 200;
shape2.VerticalPosition = 200;

Step 4: Add a new paragraph and insert a shape group to the paragraph by calling AppendShapeGroup() method.

Paragraph para2 = sec.AddParagraph();
ShapeGroup shapegr = para2.AppendShapeGroup(200, 400);

shapegr.ChildObjects.Add(new ShapeObject(doc, ShapeType.Rectangle)
{
    Width = 500,
    Height = 300,
    LineStyle = ShapeLineStyle.ThickThin,
    StrokeColor = System.Drawing.Color.Blue,

    StrokeWeight = 1.5,
});
shapegr.ChildObjects.Add(new ShapeObject(doc, ShapeType.RightTriangle)
{
    Width = 500,
    Height = 300,
    VerticalPosition = 301,
    LineStyle = ShapeLineStyle.ThickThin,
    StrokeColor = System.Drawing.Color.Green,
    StrokeWeight = 1.5,
});
shapegr.ChildObjects.Add(new ShapeObject(doc, ShapeType.QuadArrow)
{
    Width = 500,
    Height = 300,
    VerticalPosition = 601,
    LineStyle = ShapeLineStyle.ThickThin,
    StrokeColor = System.Drawing.Color.Blue,
    StrokeWeight = 1.5,
});

Step 5: Save the document to file.

doc.SaveToFile("InsertShapes.docx", FileFormat.Docx2010);

Result:

How to Insert Shape and shape group in Word Document in C#, VB.NET

Full code:

[C#]
Document doc = new Document();
Section sec = doc.AddSection();
Paragraph para1 =sec.AddParagraph();

ShapeObject shape1 = para1.AppendShape(50, 50, ShapeType.Heart);

shape1.FillColor = Color.Red;
shape1.StrokeColor = Color.Red;
shape1.HorizontalPosition = 200;
shape1.VerticalPosition = 100;
    
ShapeObject shape2 = para1.AppendShape(100, 100, ShapeType.Arrow);
                            
shape2.FillColor = Color.Purple;
shape2.StrokeColor = Color.Black;
shape2.LineStyle = ShapeLineStyle.Double;
shape2.StrokeWeight = 3;
shape2.HorizontalPosition = 200;
shape2.VerticalPosition = 200;

Paragraph para2 = sec.AddParagraph();
ShapeGroup shapegr = para2.AppendShapeGroup(200, 400);

shapegr.ChildObjects.Add(new ShapeObject(doc, ShapeType.Rectangle)
{
    Width = 500,
    Height = 300,
    LineStyle = ShapeLineStyle.ThickThin,
    StrokeColor = System.Drawing.Color.Blue,

    StrokeWeight = 1.5,
});
shapegr.ChildObjects.Add(new ShapeObject(doc, ShapeType.RightTriangle)
{
    Width = 500,
    Height = 300,
    VerticalPosition = 301,
    LineStyle = ShapeLineStyle.ThickThin,
    StrokeColor = System.Drawing.Color.Green,
    StrokeWeight = 1.5,
});
shapegr.ChildObjects.Add(new ShapeObject(doc, ShapeType.QuadArrow)
{
    Width = 500,
    Height = 300,
    VerticalPosition = 601,
    LineStyle = ShapeLineStyle.ThickThin,
    StrokeColor = System.Drawing.Color.Blue,
    StrokeWeight = 1.5,
});

doc.SaveToFile("InsertShapes.docx", FileFormat.Docx2010);
[VB.NET]
Dim doc As New Document()
Dim sec As Section = doc.AddSection()
Dim para1 As Paragraph = sec.AddParagraph()

Dim shape1 As ShapeObject = para1.AppendShape(50, 50, ShapeType.Heart)

shape1.FillColor = Color.Red
shape1.StrokeColor = Color.Red
shape1.HorizontalPosition = 200
shape1.VerticalPosition = 100

Dim shape2 As ShapeObject = para1.AppendShape(100, 100, ShapeType.Arrow)

shape2.FillColor = Color.Purple
shape2.StrokeColor = Color.Black
shape2.LineStyle = ShapeLineStyle.[Double]
shape2.StrokeWeight = 3
shape2.HorizontalPosition = 200
shape2.VerticalPosition = 200

Dim para2 As Paragraph = sec.AddParagraph()
Dim shapegr As ShapeGroup = para2.AppendShapeGroup(200, 400)


shapegr.ChildObjects.Add(New ShapeObject(doc, ShapeType.Rectangle) With { _
	Key .Width = 500, _
	Key .Height = 300, _
	Key .LineStyle = ShapeLineStyle.ThickThin, _
	Key .StrokeColor = System.Drawing.Color.Blue, _
	Key .StrokeWeight = 1.5 _
})
shapegr.ChildObjects.Add(New ShapeObject(doc, ShapeType.RightTriangle) With { _
	Key .Width = 500, _
	Key .Height = 300, _
	Key .VerticalPosition = 301, _
	Key .LineStyle = ShapeLineStyle.ThickThin, _
	Key .StrokeColor = System.Drawing.Color.Green, _
	Key .StrokeWeight = 1.5 _
})
shapegr.ChildObjects.Add(New ShapeObject(doc, ShapeType.QuadArrow) With { _
	Key .Width = 500, _
	Key .Height = 300, _
	Key .VerticalPosition = 601, _
	Key .LineStyle = ShapeLineStyle.ThickThin, _
	Key .StrokeColor = System.Drawing.Color.Blue, _
	Key .StrokeWeight = 1.5 _
})

doc.SaveToFile("InsertShapes.docx", FileFormat.Docx2010)
Wednesday, 29 April 2015 08:41

How to wrap text around image in C#

Written by support iceblue

When we add image into word document, of course we want to move it exactly where we want to make our page tidy and beautiful. With the help of Spire.Doc, we can set the wrapping style to adjust the position of the image. Usually there are seven kinds of wrapping styles: In Line with Text, Square, Tight, Through, Top and Bottom, Behind the Text, In Front of Text and Spire.Doc supports all of them. This article will show you how to wrap text around image in C#. Here comes to the steps:

Step 1: Create a new word document and load the document from the file.

Document document = new Document();
document.LoadFromFile("Sample.docx");

Step 2: Add a paragraph for the first section.

Paragraph paragraph = document.Sections[0].AddParagraph();

Step 3: Add a picture in the paragraph.

DocPicture picture = paragraph.AppendPicture(Image.FromFile("image.jpg"));

Step 4: Set text wrapping style to Square.

picture.TextWrappingStyle = TextWrappingStyle.Square;

Step 5: Set text wrapping type to both.

picture.TextWrappingType = TextWrappingType.Both;

Step 6: Save the document to file and process it.

document.SaveToFile(output,FileFormat.Docx);
System.Diagnostics.Process.Start(output);

Effective screenshot of warp text around image:

How to wrap text around image in C#

Full codes:

using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;
using System.Drawing;

namespace wrap_text_around_image
{
    class Program
    {
        static void Main(string[] args)
        {
            Document document = new Document();
            document.LoadFromFile("Sample.docx");
            Paragraph paragraph = document.Sections[0].AddParagraph();
             
            DocPicture picture = paragraph.AppendPicture(Image.FromFile("image.jpg"));
            picture.TextWrappingStyle = TextWrappingStyle.Square;
            picture.TextWrappingType = TextWrappingType.Both;
            string output = "output.docx";
            document.SaveToFile(output,FileFormat.Docx);
            System.Diagnostics.Process.Start(output);
    
        }

    }
}