Knowledgebase (2330)
Children categories
One of our customers has a requirement to insert text to a Word document in an exact location (horizontal and vertical coordinates). Generally, people position text in a Word document by using other tools such as tables and text boxes, since the positioning of tables and text boxes is much easier to control. In this article, we'll introduce how to position text in Word through text box in C#, VB.NET.
Firstly download Spire.Doc for .NET and reference the dll file to your project. Before we start to code, we also need following namespaces to be extra added.
using System.Drawing; using Spire.Doc; using Spire.Doc.Documents; using Spire.Doc.Fields; using Spire.Doc.Formatting;
Code Snippet:
Step 1: Create a new Word document, add a section for it, and then add a paragraph on the section.
Document doc = new Document(); Section sec = doc.AddSection(); Paragraph par = sec.AddParagraph();
Step 2: Append a new text box to the paragraph. Here we logically set VerticalOrigin and HorizontalOrigin as Margin because horizontal and vertical coordinate is a relative value to his reference object. By giving the variable VerticalPosition and HorizontalPosition a certain value, the text box will be fixed at the position.
TextBox textBox = par.AppendTextBox(180, 30); textBox.Format.VerticalOrigin = VerticalOrigin.Margin; textBox.Format.VerticalPosition = 100; textBox.Format.HorizontalOrigin = HorizontalOrigin.Margin; textBox.Format.HorizontalPosition = 50; textBox.Format.NoLine = true;
Step 3: Define a new format style.
CharacterFormat format = new CharacterFormat(doc); format.FontName = "Calibri"; format.FontSize = 15; format.Bold = true;
Step 4: Add text to text box, and apply the preset format to the text.
Paragraph par1 = textBox.Body.AddParagraph();
par1.AppendText("This is my new string").ApplyCharacterFormat(format);
Step 5: Save the file.
doc.SaveToFile("result.docx", FileFormat.Docx);
Result:
The border line of text box has been set as invisible, therefore we only see the text being added at the specified horizontal and vertical coordinates.

Full Code:
using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;
using Spire.Doc.Formatting;
using System.Collections.Generic;
namespace InsertText
{
class Program
{
static void Main(string[] args)
{
Document doc = new Document();
Section sec = doc.AddSection();
Paragraph par = sec.AddParagraph();
TextBox textBox = par.AppendTextBox(180, 30);
textBox.Format.VerticalOrigin = VerticalOrigin.Margin;
textBox.Format.VerticalPosition = 100;
textBox.Format.HorizontalOrigin = HorizontalOrigin.Margin;
textBox.Format.HorizontalPosition = 50;
textBox.Format.NoLine = true;
CharacterFormat format = new CharacterFormat(doc);
format.FontName = "Calibri";
format.FontSize = 15;
format.Bold = true;
Paragraph par1 = textBox.Body.AddParagraph();
par1.AppendText("This is my new string").ApplyCharacterFormat(format);
doc.SaveToFile("result.docx", FileFormat.Docx);
}
}
}
Imports Spire.Doc
Imports Spire.Doc.Documents
Imports Spire.Doc.Fields
Imports Spire.Doc.Formatting
Imports System.Collections.Generic
Namespace InsertText
Class Program
Private Shared Sub Main(args As String())
Dim doc As New Document()
Dim sec As Section = doc.AddSection()
Dim par As Paragraph = sec.AddParagraph()
Dim textBox As TextBox = par.AppendTextBox(180, 30)
textBox.Format.VerticalOrigin = VerticalOrigin.Margin
textBox.Format.VerticalPosition = 100
textBox.Format.HorizontalOrigin = HorizontalOrigin.Margin
textBox.Format.HorizontalPosition = 50
textBox.Format.NoLine = True
Dim format As New CharacterFormat(doc)
format.FontName = "Calibri"
format.FontSize = 15
format.Bold = True
Dim par1 As Paragraph = textBox.Body.AddParagraph()
par1.AppendText("This is my new string").ApplyCharacterFormat(format)
doc.SaveToFile("result.docx", FileFormat.Docx)
End Sub
End Class
End Namespace
Insert audio to PowerPoint document at specified position in C#, VB.NET
2015-04-30 07:58:39 Written by KoohjiAudio can be used in PowerPoint document to create a more interesting and dynamic PowerPoint effect. Using the Spire.presention you can now use the Shapes.AppendVideoMedia() method to insert audio in your PowerPoint document at specific position with C#, VB.NET. There is guide will introduce the method.
Here are the steps:
Step 1: Create a new PowerPoint document first.
Presentation presentation = new Presentation();
Step 2: Then set the backgroung image before inserting Audio file.
string ImageFile = "2.jpg"; RectangleF rect = new RectangleF(0, 0, presentation.SlideSize.Size.Width, presentation.SlideSize.Size.Height); presentation.Slides[0].Shapes.AppendEmbedImage(ShapeType.Rectangle, ImageFile, rect); presentation.Slides[0].Shapes[0].Line.FillFormat.SolidFillColor.Color = Color.FloralWhite;
Step 3: Load the Audio file from disk and set properties of AutoShape
presentation.Slides[0].Shapes.AppendAudioMedia(@"1.mp3",500,100,true); IAutoShape shape = presentation.Slides[0].Shapes.AppendShape(ShapeType.Rectangle, new RectangleF(500, 100, 100, 150)); shape.ShapeStyle.LineColor.Color = Color.White; shape.Fill.FillType = Spire.Presentation.Drawing.FillFormatType.None;
Step 4: Save the PowerPoint file and review.
presentation.SaveToFile("audio.pptx", FileFormat.Pptx2010);
System.Diagnostics.Process.Start("audio.pptx");
Effect Screenshot:

Full code:
using Spire.Presentation;
using System.Drawing;
namespace InsertAudio
{
class Program
{
static void Main(string[] args)
{
Presentation presentation = new Presentation();
string ImageFile = "2.jpg";
RectangleF rect = new RectangleF(0, 0, presentation.SlideSize.Size.Width, presentation.SlideSize.Size.Height);
presentation.Slides[0].Shapes.AppendEmbedImage(ShapeType.Rectangle, ImageFile, rect);
presentation.Slides[0].Shapes[0].Line.FillFormat.SolidFillColor.Color = Color.FloralWhite;
presentation.Slides[0].Shapes.AppendAudioMedia(@"1.mp3", 500, 100, true);
IAutoShape shape = presentation.Slides[0].Shapes.AppendShape(ShapeType.Rectangle, new RectangleF(500, 100, 100, 150));
shape.ShapeStyle.LineColor.Color = Color.White;
shape.Fill.FillType = Spire.Presentation.Drawing.FillFormatType.None;
presentation.SaveToFile("audio.pptx", FileFormat.Pptx2010);
System.Diagnostics.Process.Start("audio.pptx");
}
}
}
Imports Spire.Presentation
Imports System.Drawing
Namespace InsertAudio
Class Program
Private Shared Sub Main(args As String())
Dim presentation As New Presentation()
Dim ImageFile As String = "2.jpg"
Dim rect As New RectangleF(0, 0, presentation.SlideSize.Size.Width, presentation.SlideSize.Size.Height)
presentation.Slides(0).Shapes.AppendEmbedImage(ShapeType.Rectangle, ImageFile, rect)
presentation.Slides(0).Shapes(0).Line.FillFormat.SolidFillColor.Color = Color.FloralWhite
presentation.Slides(0).Shapes.AppendAudioMedia("1.mp3", 500, 100, True)
Dim shape As IAutoShape = presentation.Slides(0).Shapes.AppendShape(ShapeType.Rectangle, New RectangleF(500, 100, 100, 150))
shape.ShapeStyle.LineColor.Color = Color.White
shape.Fill.FillType = Spire.Presentation.Drawing.FillFormatType.None
presentation.SaveToFile("audio.pptx", FileFormat.Pptx2010)
System.Diagnostics.Process.Start("audio.pptx")
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);
}
}
}