Posts

Showing posts with the label C#

How to: Add Watermark to PDFs Programmatically using iTextSharp

Being able to add text or image watermark to PDF files is one of those handy things you can archive via program. The only library required for the little program is iTextSharp . For those who haven't used it yet, iTextSharp is a C# version of iText , which allows you to generate or manipulate PDFs. So let's cut the chitchat, and go straight to the code. By calling 'AddTextWatermark' or 'AddImageWatermark' function with required parameters supplied, you are ready to rock! public void CreateTemplate(string watermarkText, string targetFileName){     var document = new Document();     var pdfWriter = PdfWriter.GetInstance(document, new FileStream(targetFileName, FileMode.Create));     var font = new Font(Font.FontFamily.HELVETICA, 60, Font.NORMAL, BaseColor.LIGHT_GRAY);     document.Open();     // Specify font and alignment of text watermark     ColumnText.ShowTextAligned(pdfWriter.DirectContent, Element.ALIGN_CENTER, new ...

How to: Implement Dependency Injection in MVC 4 with Ninject

After creating mvc 4, the first thing I realised was there were slightly framework changes. Most significantly is inside the 'Global.asax.cs', which moves all the configuration setting registers into separate class files in the folder 'App_Start'.  This actually prevents me from doing DI using 'Ninject.MVC3' in the way I used to implement in mvc 3. While Ninject package for mvc 4 is not out yet, I have to come up with an alternative solution. The following is the step by step guide. 1. Add 'Ninject.MVC3' package You will see 'NinjectWebCommon.cs' file is generated inside 'App_Start' folder, which was meant to be part of package adding process. 2. Modify 'Global.asax.cs' * Inherit 'MvcApplication' from 'NinjectHttpApplication' instead of 'HttpApplication' * Implement 'OnApplicationStarted' method inherited from 'NinjectHttpApplication', and move all registers in 'Application_Sta...

How to: Implement URL Rewrite in IIS 7.5

I use ASP.NET and IIS 7.5 for my web development. As I am currently working on dynamic websites, the long-string-unreadable-URL like " /Feeds.aspx?Key=66a718c4-1982-43b1-9e47-afeddea650e4&FeedType=PAGE " has been a major complain from clients. Therefore, I decided to rewrite URL which shortens the raw URL to a menu-name-related form. 1. In order to implement URL rewrite, I create the module  RedirectHttpModule  based on some open sources found, and use a XML for URL mapping. using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Xml.XPath; namespace custom {     public class RedirectHttpModule : IHttpModule     {         public void Dispose()         { }         public void Init(HttpApplication application)         {             application.PostResolveRequestCache += (new Eve...