Spire.Doc for .NET

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");

        }
    }
}

Now Spire.Doc support using uninstalled font when converting Doc to PDF to diversity text content. In this article, we'll talk about how to realize this function:

Step 1: Download a font uninstalled in system.

How to use uninstalled font when converting Doc to PDF via Spire.Doc

Step 2: Create a new blank Word document.

Document document = new Document();

Step 3: Add a section and create a new paragraph.

Section section = document.AddSection();
Paragraph paragraph = section.Paragraphs.Count > 0 ? section.Paragraphs[0] : section.AddParagraph();

Step 4: Append text for a txtRange.

TextRange txtRange = paragraph.AppendText(text);

Step 5: Create an example for class ToPdfParameterList named to pdf, and create a new PrivateFontPathlist for property PrivateFontPaths, instantiate one PrivateFontPath with name and path of downloaded font.

ToPdfParameterList toPdf = new ToPdfParameterList()
{
    PrivateFontPaths = new List()
        {
          new PrivateFontPath("DeeDeeFlowers",@"D:\DeeDeeFlowers.ttf")
        }
};

Step 6: Set the new font for the txtaRange.

txtRange.CharacterFormat.FontName = "DeeDeeFlowers";

Step 7: Convert the Doc to PDF.

document.SaveToFile("result.pdf", toPdf);

Step 8: Review converted PDF files.

System.Diagnostics.Process.Start("result.pdf");

Result screenshot:

How to use uninstalled font when converting Doc to PDF via Spire.Doc

Full Code Below:

Document document = new Document();
           
//Add the first secition
Section section = document.AddSection();
//Create a new paragraph and get the first paragraph
Paragraph paragraph
    = section.Paragraphs.Count > 0 ? section.Paragraphs[0] : section.AddParagraph();

//Append Text
String text
    = "This paragraph is demo of text font and color. "
    + "The font name of this paragraph is Tahoma. "
    + "The font size of this paragraph is 20. "
    + "The under line style of this paragraph is DotDot. "
    + "The color of this paragraph is Blue. ";
 TextRange txtRange = paragraph.AppendText(text);

//Import the font
 ToPdfParameterList toPdf = new ToPdfParameterList()
 {
     PrivateFontPaths = new List<PrivateFontPath>()
         {
          new PrivateFontPath("DeeDeeFlowers",@"D:\DeeDeeFlowers.ttf")
         }
};
//Make use of the font.
txtRange.CharacterFormat.FontName = "DeeDeeFlowers";

document.SaveToFile("result.pdf", toPdf);

System.Diagnostics.Process.Start("result.pdf");

In our daily work, we may receive Word documents that will sometimes contain embedded Excel object (sheet) and we need to convert embedded Excel sheet to Word table so that we can easily change the date or format the table with style. In this article, you will learn how to convert embedded Excel sheet to Word table using Spire.Doc and Spire.XLS in C#, VB.NET.

Firstly, you need to download Spire.Office because Spire.Doc and Spire.XLS will be used in the same program. Add Spire.Doc.dll and Spire.XLS.dll as references in your VS project. Then follow the program guidance below to finish this work.

Step 1: Create a new Word document, load the sample file. Get the paragraph that contains the Excel object from the section. Initialize a new datatable.

Document doc = new Document("Sample.docx", Spire.Doc.FileFormat.Docx2010);
Section section = doc.Sections[0];
Paragraph para = section.Paragraphs[2];
DataTable dt = new DataTable();

Step 2: Traverse every DocumentObject in the paragraph, use IF statement to test if DocumentObject is OLE object, use another IF statement to test if OLE object type is Excel.Sheet.12. If yes, save the data of OLE object to a workbook through LoadFromStrem(). Then export data from worksheet to datatable.

foreach (DocumentObject obj in para.ChildObjects)
{
    if (DocumentObjectType.OleObject == obj.DocumentObjectType)
    {
        DocOleObject dObj = obj as DocOleObject;
        if (dObj.ObjectType == "Excel.Sheet.12")
        {
            Workbook wb = new Workbook();
            wb.LoadFromStream(new MemoryStream(dObj.NativeData));
            Worksheet ws = wb.Worksheets[0];
            dt = ws.ExportDataTable(ws.AllocatedRange, false);
        }
    }
}

Step 3: Create a new Word table and set row number and column number according to rows and columns of datatable. Export data from datatable to Word table.

Table table = section.AddTable(true);
 table.ResetCells(dt.Rows.Count, dt.Columns.Count);

 for (int i = 0; i < dt.Rows.Count; i++)
 {
     for (int j = 0; j < dt.Columns.Count; j++)
     {
         string text = dt.Rows[i][j] as string;
         table.Rows[i].Cells[j].AddParagraph().AppendText(text);
     }
 }

Step 4: Save the file.

doc.SaveToFile("Result.docx", Spire.Doc.FileFormat.Docx2010);

Result:

How to Convert Embedded Excel Sheet to Word Table in C#, VB.NET

Full Code:

[C#]
using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;
using Spire.Xls;
using System.Data;
using System.IO;
namespace ApplyTableStyles
{
    class Program
    {

        static void Main(string[] args)
        {

            Document doc = new Document("Sample.docx", Spire.Doc.FileFormat.Docx2010);
            Section section = doc.Sections[0];
            Paragraph para = section.Paragraphs[2];
            DataTable dt = new DataTable();

            foreach (DocumentObject obj in para.ChildObjects)
            {
                if (DocumentObjectType.OleObject == obj.DocumentObjectType)
                {
                    DocOleObject dObj = obj as DocOleObject;
                    if (dObj.ObjectType == "Excel.Sheet.12")
                    {
                        Workbook wb = new Workbook();
                        wb.LoadFromStream(new MemoryStream(dObj.NativeData));
                        Worksheet ws = wb.Worksheets[0];
                        dt = ws.ExportDataTable(ws.AllocatedRange, false);
                    }
                }
            }

            Table table = section.AddTable(true);
            table.ResetCells(dt.Rows.Count, dt.Columns.Count);

            for (int i = 0; i < dt.Rows.Count; i++)
            {
                for (int j = 0; j < dt.Columns.Count; j++)
                {
                    string text = dt.Rows[i][j] as string;
                    table.Rows[i].Cells[j].AddParagraph().AppendText(text);
                }
            }

            doc.SaveToFile("Result.docx", Spire.Doc.FileFormat.Docx2010);
        }
    }
}
[VB.NET]
Imports Spire.Doc
Imports Spire.Doc.Documents
Imports Spire.Doc.Fields
Imports Spire.Xls
Imports System.Data
Imports System.IO
Namespace ApplyTableStyles
	Class Program

		Private Shared Sub Main(args As String())

			Dim doc As New Document("Sample.docx", Spire.Doc.FileFormat.Docx2010)
			Dim section As Section = doc.Sections(0)
			Dim para As Paragraph = section.Paragraphs(2)
			Dim dt As New DataTable()

			For Each obj As DocumentObject In para.ChildObjects
				If DocumentObjectType.OleObject = obj.DocumentObjectType Then
					Dim dObj As DocOleObject = TryCast(obj, DocOleObject)
					If dObj.ObjectType = "Excel.Sheet.12" Then
						Dim wb As New Workbook()
						wb.LoadFromStream(New MemoryStream(dObj.NativeData))
						Dim ws As Worksheet = wb.Worksheets(0)
						dt = ws.ExportDataTable(ws.AllocatedRange, False)
					End If
				End If
			Next

			Dim table As Table = section.AddTable(True)
			table.ResetCells(dt.Rows.Count, dt.Columns.Count)

			For i As Integer = 0 To dt.Rows.Count - 1
				For j As Integer = 0 To dt.Columns.Count - 1
					Dim text As String = TryCast(dt.Rows(i)(j), String)
					table.Rows(i).Cells(j).AddParagraph().AppendText(text)
				Next
			Next

			doc.SaveToFile("Result.docx", Spire.Doc.FileFormat.Docx2010)
		End Sub
	End Class
End Namespace
page 32