Spire.Doc

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.

How to Insert Text to Word at Exact Position in C#, VB.NET

Full Code:

[C#]
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);
[VB.NET]
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)
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);
    
        }

    }
}

We can set Asian typography for paragraph for .doc files, there are four text alignments: Top, Center, Baseline, Bottom and Auto. In this article let's see how to set alignment when append HTML string code to .doc in C#.

Download Spire.Doc 5.3.83 or upper version, then add reference to your project.

Here are the steps:

Step 1: Create a HTML file contain the following code.

<html>
<body>
<i>f</i>(<i>x</i>)=<img align="middle" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACcAAAAlCAYAAADBa/A+AAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsMAAA7DAcdvqGQAAADvSURBVFhH7ZNbCsUwCES7/03nIkSwRh1rL00+PCCJVaeTPq5xMG2uSpuLuC7fwlZzZOxYc0TZnDwZ76WYzjVRjQnn57oQmdC5x9ses6IHUO7xd3NW8xNzVPPCwrtOLBXdbAlHgpJMX9SzVGQz7fUw55Eo87ZnqVAzh8x5z8jrHpl6pBNPb6bNVWlzVW7m5N+zKyT9Wqu0OYn3fVl8am754IHBz5+cpGxOP3qda9CNLNAMVESmmG3mMjw1lzrwXF0iEap5gUj1zNUlI0Jk+4i05lxNWCR1ysIh0Ixb1SJQCNQJ1pERgRU30uaqHGxujB+eJddCfZBWMwAAAABJRU5ErkJggg==" />

Step 2: Create a new document and add new section.

Document doc2 = new Document();
doc2.AddSection();

Step 3: Create new paragraph p and set its property of TextAlignment as "Center".

p.AppendText("Alignment:Center      ");
p.AppendHTML(File.ReadAllText(@"test.html"));
p.Format.TextAlignment = TextAlignment.Center; 

Step 4: Set other options to make a contrast, Auto is Baseline by default.

Paragraph p1 = doc2.Sections[0].AddParagraph();
p1.AppendText("Alignment:Baseline   ");
p1.AppendHTML(File.ReadAllText(@"test.html"));
p1.Format.TextAlignment = TextAlignment.Baseline;

Paragraph p2 = doc2.Sections[0].AddParagraph();
p2.AppendText("Alignment:Bottom    ");
p2.AppendHTML(File.ReadAllText(@"test.html"));
p2.Format.TextAlignment = TextAlignment.Bottom;

Paragraph p3 = doc2.Sections[0].AddParagraph();
p3.AppendText("Alignment:Top          ");
p3.AppendHTML(File.ReadAllText(@"test.html"));
p3.Format.TextAlignment = TextAlignment.Top;

Paragraph p4 = doc2.Sections[0].AddParagraph();
p4.AppendText("Alignment:Auto        ");
p4.AppendHTML(File.ReadAllText(@"test.html"));
p4.Format.TextAlignment = TextAlignment.Auto;

Step 5: Save and review.

doc2.SaveToFile(@"test.doc", FileFormat.Doc);
System.Diagnostics.Process.Start("test.doc");

The screen shot:

Set text alignment when append HTML string code to .doc in C#

Full Code Here:

Document doc2 = new Document();
doc2.AddSection();
Paragraph p = doc2.Sections[0].AddParagraph();
p.AppendText("Alignment:Center      ");
p.AppendHTML(File.ReadAllText(@"test.html"));
p.Format.TextAlignment = TextAlignment.Center;

Paragraph p1 = doc2.Sections[0].AddParagraph();
p1.AppendText("Alignment:Baseline   ");
p1.AppendHTML(File.ReadAllText(@"test.html"));
p1.Format.TextAlignment = TextAlignment.Baseline;

Paragraph p2 = doc2.Sections[0].AddParagraph();
p2.AppendText("Alignment:Bottom    ");
p2.AppendHTML(File.ReadAllText(@"test.html"));
p2.Format.TextAlignment = TextAlignment.Bottom;

Paragraph p3 = doc2.Sections[0].AddParagraph();
p3.AppendText("Alignment:Top          ");
p3.AppendHTML(File.ReadAllText(@"test.html"));
p3.Format.TextAlignment = TextAlignment.Top;

Paragraph p4 = doc2.Sections[0].AddParagraph();
p4.AppendText("Alignment:Auto        ");
p4.AppendHTML(File.ReadAllText(@"test.html"));
p4.Format.TextAlignment = TextAlignment.Auto;

doc2.SaveToFile(@"test.doc", FileFormat.Doc);
System.Diagnostics.Process.Start("test.doc");
Page 1 of 28