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.
Full codes:
using Spire.Doc; using Spire.Doc.Documents; using Spire.Doc.Fields; namespace AlignShape { class Program { static void Main(string[] args) { 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:
Full codes:
using Spire.Doc; using Spire.Doc.Documents; using Spire.Doc.Fields; namespace Rotate { class Program { static void Main(string[] args) { 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 transparent using Spire.Doc.
Below screenshot shows an example image with black and white colors:
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:
Full code:
using Spire.Doc; using Spire.Doc.Documents; using Spire.Doc.Fields; using System.Drawing; namespace Transeperant { class Program { static void Main(string[] args) { //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); } } }
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:
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:
Full code:
using Spire.Doc; using Spire.Doc.Documents; using Spire.Doc.Fields; namespace Resize { class Program { static void Main(string[] args) { 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:
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.
Full codes:
using Spire.Doc; using Spire.Doc.Documents; using Spire.Doc.Fields; namespace Reset { class Program { static void Main(string[] args) { 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); } } }
Imports Spire.Doc Imports Spire.Doc.Documents Imports Spire.Doc.Fields Namespace Reset Class Program Private Shared Sub Main(args As String()) 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) End Sub End Class End Namespace
How to remove shape from word document in C# and VB.NET
2017-08-02 09:50:31 Written by support iceblueWe 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:
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:
Full codes:
using Spire.Doc; using Spire.Doc.Documents; using Spire.Doc.Fields; namespace RemoveShape { class Program { static void Main(string[] args) { 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); } } }
Imports Spire.Doc Imports Spire.Doc.Documents Imports Spire.Doc.Fields Namespace RemoveShape Class Program Private Shared Sub Main(args As String()) 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) End Sub End Class End Namespace
WordArt is a feature in MS Word that allows you to insert colorful and stylish text into your document. Apart from that, it can also bend, stretch, or skew the shape of the text, which is a quick way to make the text stand out with special effects. In this article, you will learn how to programmatically insert WordArt in a Word document using Spire.Doc for .NET.
Install Spire.Doc for .NET
To begin with, you need to add the DLL files included in the Spire.Doc for .NET package as references in your .NET project. The DLL files can be either downloaded from this link or installed via NuGet.
PM> Install-Package Spire.Doc
Insert WordArt in Word
The ShapeType enumeration provided by Spire.Doc for .NET defines a variety of WordArt shape types whose names begin with "Text". In order to create a WordArt in Word, you need to initialize an instance of ShapeObject and specify the WordArt type and text content. The detailed steps are as follows:
- Create a Document instance.
- Add a section to the document using Document.AddSection() method, and then add a paragraph to the section using Section.AddParagraph() method.
- Append a shape to the paragraph and specify the shape size and type using Paragraph.AppendShape(float width, float height, ShapeType shapeType) method.
- Set the position of the shape using ShapeObject.VerticalPosition and ShapeObject.HorizontalPosition properties.
- Set the text of WordArt using WordArt.Text property.
- Set the fill color and stroke color of WordArt using ShapeObject.FillColor and ShapeObject.StrokeColor properties.
- Save the document to another file using Document.SaveToFile() method.
- C#
- VB.NET
using Spire.Doc; using Spire.Doc.Documents; using Spire.Doc.Fields; namespace CreatWordArt { class Program { static void Main(string[] args) { //Create a Document instance Document doc = new Document(); //Add a section Section section = doc.AddSection(); //Add a paragraph Paragraph paragraph = section.AddParagraph(); //Append a shape to the paragraph and specify the shape size and type ShapeObject shape = paragraph.AppendShape(400, 150, ShapeType.TextDeflateBottom); //Set the position of the shape shape.VerticalPosition = 60; shape.HorizontalPosition = 60; //Set the text of WordArt shape.WordArt.Text = "Create WordArt in Word"; //Set the fill color and stroke color of WordArt shape.FillColor = System.Drawing.Color.Cyan; shape.StrokeColor = System.Drawing.Color.DarkBlue; //Save the document doc.SaveToFile("CreateWordArt.docx", FileFormat.Docx2013); } } }
Apply for a Temporary License
If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.
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:
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:
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(); } } }
Insert Shape and shape group in Word Document in C#, VB.NET
2015-12-09 09:07:30 Written by support iceblueMS 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:
Full code:
using Spire.Doc; using Spire.Doc.Documents; using Spire.Doc.Fields; using System.Drawing; namespace InsertShape { class Program { static void Main(string[] args) { 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); } } }
Imports Spire.Doc Imports Spire.Doc.Documents Imports Spire.Doc.Fields Imports System.Drawing Namespace InsertShape Class Program Private Shared Sub Main(args As String()) 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) End Sub End Class End Namespace
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:
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); } } }