C# tips and tricks 18 – How to generate word file with paragraph, table, image, hyperlink using DocX



C# tips and tricks 18 – How to generate word file with paragraph, table, image, hyperlink using DocX

C# tips and tricks 18 - How to generate word file with paragraph, table, image, hyperlink using DocX

How to generate word file with paragraph, table, picture and hyperlink using docx library (Open office XML).
Like our facebook page www.facebook.com/ankprotraining
C# program to generate word file :

How to generate word file using c#

DocX nuget package

DocX is a .NET library
Manipulate Microsoft Word files
DocX is fast, lightweight
DocX does not require Microsoft Word or Office to be installed
DocX does not use COM libraries

Environment.NewLine: Gets the newline string defined for this environment.

How to generate word using c#?

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xceed.Words.NET;

namespace WorkingWithWord
{
class Program
{
static void Main(string[] args)
{
#region one
string fileName = “exempleWord.docx”;
var doc = DocX.Create(fileName);
doc.InsertParagraph(“Hi Everyone”);
#endregion

#region two
//Formatting Title
string title = “Who is Virat Kohli?”;

//Formatting Title
// like using this we can set font family, font size, position, font color etc

Formatting titleFormat = new Formatting();
//Specify font family
titleFormat.FontFamily = new Xceed.Words.NET.Font(“Times new roman”);
//Specify font size
titleFormat.Size = 20D;
titleFormat.Position = 40;
titleFormat.FontColor =Color.BlueViolet;
titleFormat.UnderlineColor = Color.Gray;
titleFormat.Italic = true;

//Text
string textParagraph = “Dear Friends, ” + Environment.NewLine +
“Virat Kohli – (born 5 November 1988) is an Indian international cricketer who currently captains the India national team. A right-handed batsman, often regarded as one of the best batsmen in the world. He plays for the Royal Challengers Bangalore in the Indian Premier League (IPL), & has been the team’s captain since 2013.”;

//Formatting Text Paragraph
Formatting textParagraphFormat = new Formatting();
//font family
textParagraphFormat.FontFamily = new Xceed.Words.NET.Font(“Century Gothic”);
//font size
textParagraphFormat.Size = 12D;
//Spaces between characters
textParagraphFormat.Spacing = 2;
//Insert title
Paragraph paragraphTitle = doc.InsertParagraph(title, false, titleFormat);
paragraphTitle.Alignment = Alignment.center;
//Insert text
doc.InsertParagraph(textParagraph, false, textParagraphFormat);
#endregion

#region three
//Create a picture
Xceed.Words.NET.Image img = doc.AddImage(@”viratkohli.jpg”);
Picture p = img.CreatePicture();
//Create a new paragraph
Paragraph par = doc.InsertParagraph(“Virat’s picture – “);
par.AppendPicture(p);
#endregion

#region four
//Create Table with 2 rows and 4 columns.
Table t = doc.AddTable(2, 4);
t.Alignment = Alignment.center;
t.Design = TableDesign.ColorfulList;
// Fill cells by adding text.
// First row
t.Rows[0].Cells[0].Paragraphs.First().Append(“Name”);
t.Rows[0].Cells[1].Paragraphs.First().Append(“Age”);
t.Rows[0].Cells[2].Paragraphs.First().Append(“City”);
t.Rows[0].Cells[3].Paragraphs.First().Append(“Mobile”);

// Second row details
t.Rows[1].Cells[0].Paragraphs.First().Append(“Ram”);
t.Rows[1].Cells[1].Paragraphs.First().Append(“21”);
t.Rows[1].Cells[2].Paragraphs.First().Append(“Bangalore”);
t.Rows[1].Cells[3].Paragraphs.First().Append(“9845098450”);
doc.InsertTable(t);
#endregion

#region five
// Hyperlink
Hyperlink url = doc.AddHyperlink(“Ankpro home page”, new Uri(“http://www.ankpro.com”));
Paragraph p1 = doc.InsertParagraph();
p1.AppendLine(“Please visit “).Bold().AppendHyperlink(url).Color(Color.Blue); // Hyperlink in blue color
#endregion

#region part of one
doc.Save();
Process.Start(“WINWORD.EXE”, fileName);
#endregion
}
}
}

DocX can be freely used in commercial software

Why I’ve not selected Microsoft.Office.Interop.Excel?
Word file generation would fail if the user didn’t have Word installed.
Version of the assembly we’re using may become incompatible with the latest version of Word.
A software application shouldn’t rely on an Word installation in order to create a word file, which is why we should not use Microsoft.Office.Interop.Word.

Comments are closed.