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

2015-04-10 02:05:28 Written by  support iceblue
Rate this item
(0 votes)

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:

using Spire.Doc;
using Spire.Doc.Documents;
using System.IO;
namespace SetTextAlignment
{
 class Program
    {
     
      static void Main(string[] args)
        {
            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");

        }
    }
}

Additional Info

  • tutorial_title: Set paragraph alignment
Last modified on Friday, 03 September 2021 03:34