Set text font to acrofields using pdfStamper (iTextSharp)

We use iTextSharp to generate the custom pdf, one of our client wanted to have custom font for the placeholders. Initially we used below code however it did not work:

//create a bold font
iTextSharp.text.Font bold = FontFactory.GetFont(FontFactory.COURIER, 12f, iTextSharp.text.Font.BOLD);

//set the field to bold
pdfFormFields.SetFieldProperty("studentName", "textfont", bold.BaseFont, null);

The font was not at all getting set, so we used below approach and it worked :)
iTextSharp.text.Font bold = BaseFont.CreateFont(BaseFont.HELVETICA....

Instead of GetFont() we should use CreateFont().

Life Secrets and Tips

One of my friend has shared a very good link which is really useful to increase our strengths.

- Memorize something everyday.
- Constantly try to reduce your attachment to possessions.
- Develop an endless curiosity about this world.
- Remember people’s names
- Learn to focus only on the present.
- Even more specifically, live in THIS moment.
- Smile more often.
- Drink water.
- Don’t take life so seriously!
- Think positive thoughts.
- Read books.
- Help others.
- Be honest at all times.
- Sleep less.
- Figure out what your goals and dreams are.
- Start your day off right.
- Travel.
- Develop the ability to forgive.
- Visualize daily.
- Learn to control your mind.
- Learn to control your emotions.
- Relax!
- Read something inspirational right before bed and after waking.
- Do what you love.

Reference Link: 50 Life Secrets and Tips

C#.Net Attribute parameters types

Today we came across a scenario where we wanted to have a attribute type of property inside attribute class. However after googling we found that we can have only below data types of properties inside the Attributes.

Attribute parameters are restricted to constant values of the following types:

- Simple types (bool, byte, char, short, int, long, float, and double)
- string
- System.Type
- enums
- object (The argument to an attribute parameter of type object must be a constant value of one of the above types.)
- One-dimensional arrays of any of the above types

Comparison between OpenId and OAuth

Most of the time we use terms OAuth and OpenId however do we really understand they are similar or different?

OAuth: OAuth is an authorization framework that enables a third-party application to obtain a limited access to an HTTP service.

OpenId: The main purpose of OpenID is authentication, i.e. sharing single identity with different consumers. For OpenID, the Provider (OpenID Provider) processes the user authentication process.

OpenId: Sharing single identity with different consumers
OAuth: Sharing your data without sharing your password

MVC web grid large data loading performance improvements

There can be different approaches however considering the simple one we can have below approaches:

Custom Paging: Load data from the database every time on page index changed for only current selected page index (of page size) and not the whole data

Data Caching: Get all the filtered data first time of page load and store it in .Net memory cache for current session, and return only current selected page index data to grid. Next time when new page index is selected, don’t go to database server again, get data from cache

If the data is larger then loading all the data at first time and caching is not at all good option, so we should implement paging.

C#.Net - When should we use 'this' keyword?

The this keyword refers to the current instance of the class, following are common usage:

Members hidden by similar names: A class can contain a private variable name "myName" as well as function which can have parameter with same name "myName". In this case if we want to use class variable inside method then we should use "this.myName".

To pass an object as a parameter to method: A method can have parameter of type class in this case we can use 'this'.

To declare indexer: For example
public int this[int param]
{ get { return array[param]; }
set { array[param] = value; }


MVC.Net - Why should we use View Models on view and not Entity models directly?

Why not?

1. Entity model potentially still connected to an active DatabaseContext and if view is user-provided (third party developers) then a view can make malicious changes to your database.
2. In many cases we require more properties on Views than those exists in Entity Model. e.g. Calculated value or calculated value with some extra things appended to it.

What situation we can use?

1. Views are fully trusted i.e. built by you only.
2. Views are made only for view data purpose.
3. You don't need extra properties than Entity model properties

It is totally dependent on you whether you want to use View Models or not.

MVC.Net - Filtering in ASP.Net MVC

Sometimes we want to perform some logic before action executes or after action method executes. To support this ASP.Net MVC provides filters. There different filters provided as below:

Authorization filters: This allows us to make security decisions whether to execute action method. Authorization filter runs before any other filters.

Action filters: It provides OnActionExecuted and OnActionExecuting.

Result filters: It provides OnResultExecuted and OnResultExecuting. It is used for modifying the http response for example out put cache.

Exception filters: It provides a way to handle unhand-led exceptions throws during the execution. It can be used for logging errors or displaying error page.