JG Vimalan's Blog

It is all about coding!

Installing font using C# setup project

One of my team member asked about installing font to the system when a C# application is installed. Here are the steps,

a. Create a C# setup project. Open ‘File System’. Right click on ”File System’ and select ‘Add Special Folder’, choose ‘Fonts Folder’.

b.  Right click ‘Fonts Folder’ and select Add->File

c. Browse and select the font file(s) you wish to install.

d. Ensure that, the register property of the font file is set to vsdrfFont.

e. Now, run the setup. Once the setup is completed, you can find that, any font dialog in the system displays the installed font.

If you wish the font should remain in the system even after uninstalling the application, set the permanent’ property of the font file to true.


February 22, 2011 Posted by | C#.NET | 11 Comments

Converting json string to C# object and vice versa

In order to convert a C# generic list to json string and vice versa,

Add reference to System.Web.Extensions in your project.

//Assume that, you have a generic list as shown below,
List<Patient> patientList = GetPatients();

Use Serialize method in JavaScriptSerializer class for serializing generic list to Json string.

System.Web.Script.Serialization.JavaScriptSerializer serializer =  new System.Web.Script.Serialization.JavaScriptSerializer();

//use Serialize method to convert patient list to json string

string JStr = serializer.Serialize(patientList);

//Now, the json string can be set to generic list of type patient as shown below,

List<Patient> nList = new List<Patient>();

nList = serializer.Deserialize<List<Patient> >(JStr);

DOWNLOAD the working sample here https://docs.google.com/file/d/0By3at1G2EGgOal9IMW5pQjNTNXM/edit (File->Download)

February 22, 2011 Posted by | Android, C#.NET, JSON | 7 Comments

Convert sByte [] to string

I got a task where I need to convert a compressed string in sbyte[] back to original string. The example string used here is “Hello World!!!Hello World!!!”

Here is the code,

sbyte[] sbyteData = new sbyte[] {
31, -117, 8, 0, 0, 0, 0, 0, 0, 0, -13, 72, -51, -55, -55, 87, 8, -49, 47, -54, 73, 81, 84, 84, -12, 64, -31, 1, 0, 111, 9, 127, -117, 28, 0, 0, 0 }; //compressed string
//Convert sbyte[] to byte[]
byte[] byteData = Array.ConvertAll(sbyteData, (a) => (byte)a);
System.IO.Stream stream = new System.IO.MemoryStream(byteData);

System.IO.Compression.GZipStream gstream = new System.IO.Compression.GZipStream(stream, System.IO.Compression.CompressionMode.Decompress, true);
byte[] resultBytes = new byte[byteData.Length];

//fill the resultBytes with decompressed byte array
int readResult = gstream.Read(resultBytes, 0, resultBytes.Length);

//you got the actual string here
string stringFromByteData = utf8Encoding.GetString(resultBytes);

February 22, 2011 Posted by | C#.NET | 1 Comment

Running ASP.NET 3.5 web application in IIS 7.0

Recently,one of my colleague asked me the steps to deploy and  run an ASP.NET web application developed targeting .NET framework 3.5 in IIS 7.0

Here are the steps,

a. Launch IIS 7, right click ‘Default web site’ and choose  ‘Add Application’

b. You will get a dialog as shown below,

c. Map your sources using the above dialog. If needed, create your own app pool or set it to one which targets .NET Framework 2.0 classic or integrated.

Now, you will be able to launch you web application in IIS 7

February 17, 2011 Posted by | ASP.NET, IIS 7 | Leave a comment

Make word document uneditable using C#

This is continuation to my last post on dynamically adding rows to table in word document. The post can be found here, https://jgvimalan.wordpress.com/2011/02/08/add-rows-to-table-in-ms-word-document-using-c/

I got a task where, the dynamically generated word document has to be made uneditable. Inorder to achieve that, you need to protect the document by allow only reading. The sample code snippet is given below,

var wordApp = new ApplicationClass();

wordApp.Protect(Microsoft.Office.Interop.Word.WdProtectionType.wdAllowOnlyReading, ref missing, ref missing, ref missing, ref missing);

This will make the word document uneditable.

February 12, 2011 Posted by | ASP.NET, C#.NET | Leave a comment

Add rows to table in MS Word document using C#

I got a task where I need to dynamically add rows to a table in MS Word document using C#. The row count may differ based on user. In order to achieve this, I have added reference to Microsoft.Office.Interop.Word in my ASP.Net web application.

I have added the table head part in the MS Word document to keep the things simple. The MS Word document looks like,


using System;
using Microsoft.Office.Interop.Word;

namespace demo
public partial class Default : System.Web.UI.Page
protected void Page_Load(object sender, EventArgs e)
object missing = System.Reflection.Missing.Value;
object setFalse = false;
object savechanges = true;

var wordApp = new ApplicationClass();

object fileName = @”C:\sample.doc”;

Document adoc = wordApp.Documents.Open(ref fileName,
ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing);


Tables tables = adoc.Tables;
if (tables.Count > 0)
//Get the first table in the document
Table table = tables[1];

int rowsCount = table.Rows.Count;
int coulmnsCount = table.Columns.Count;

for (int i = 0; i < 25; i++)
Row row = table.Rows.Add(ref missing);

for (int j = 1; j<=coulmnsCount; j++)
row.Cells[j].Range.Text = string.Format(“{0}{1}”, i, j);
row.Cells[j].WordWrap = true;
row.Cells[j].Range.Underline = WdUnderline.wdUnderlineNone;
row.Cells[j].Range.Bold = 0;

object sFile = @”C:\result.doc”; //save the updated document
adoc.SaveAs(ref sFile, ref missing, ref missing,
ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing,
ref missing);

adoc.Close(ref savechanges, ref missing, ref missing);
wordApp.Quit(ref savechanges, ref missing, ref missing);

Once the code is executed, the new rows have been added to the document. The result table is as follows,


February 8, 2011 Posted by | ASP.NET, C#.NET | 7 Comments