Merge pdf files using C#

Written by jf on October 3, 2008 – 8:00 pm -

Recently I had to create an assembly component using C# to merge multiple PDF files into one file. The specification was pretty straight forward:
1) Merge two of more PDF document into a single output PDF File.
2) Used on an ASP.NET Page

After a few minutes of GOOG, I came up to this “ITextSharp

iText# (iTextSharp) is a port of the iText open source java library written entirely in C# for the .NET platform. iText# is a library that allows you to generate PDF files on the fly. It is implemented as an assembly.

It must be noted that the assembly is coded and compiled using the .NET Framework 1.1. You might want to migrate to the version 2.0 or 3.5 of the .NET Framework.

With a some more research on PDF merging. I was able to create a class that will make use of the ITextSharp assembly and perform as the merge pdf operation as needed.

Here is the code:
Read more »

Subscribe to my RSS feed

Copy-Clone custom object in C#

Written by jf on August 5, 2008 – 6:00 am -

This post relates how to Copy/Clone custom object in C# (Deep and Shallow Clone).

In this example I will be having a base class that all my custom object will be inheriting from.

Updates: After quiet some research on the subject i recently found some interesting articles on this subject and wanted to share them on this post.

Object Cloning Using IL in C#

This subject is inspired on a session I followed on the TechDays 2008,
that addressed the fact that IL (Intermediate Language) can be used to clone objects, among other things, and that it’s not evil at all, and it can be pretty performant also.

You only have to see that you don’t overuse it tho, because otherwise the readability of your code is reduced, which is not a good thing for the maintenance.
And wrong usage of reflection (what IL is, or at least uses) can also result in poor performance.

Read more:
http://whizzodev.blogspot.com/2008/03/object-cloning-using-il-in-c.html

C# Class For Making a Deep Copy Clone of an Arbitrary Object

Here is a C# class that can create a deep copy clone of an arbitrary object. The thing that’s special about it is that it should work for any class that extends it, so that you don’t need to re-write a custom clone() function for every child class (as it seems the C# framework creators would like). This does a deep copy so be careful about members that recursively include one another.

Another way of doing this would be to use serialization . . . I just personally thought the reflection package would be more elegant.

Read more:
http://www.thomashapp.com/node/106

C# Object Clone Wars

Cloning C# objects is one of those things that appears easy but is actually quite complicated with many “gotchas.” This article describes the most common ways to clone a C# object.

Shallow vs. Deep Cloning

There are two types of object cloning: shallow and deep. A shallow clone copies the references but not the referenced objects. A deep clone copies the referenced objects as well.

hallow vs Deep Cloning

shallow vs Deep Cloning

Read more:
http://www.csharp411.com/c-object-clone-wars/

Clone C# Object using Serialization and De-serialization (Deep Clone)

In this example i’ll be using Serialization and De-Serialization to make a deep clone of the Person Class.

You must import the following namespaces to start with.


using System;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
using System.Runtime.Serialization;
using System.IO;

Read more »

Subscribe to my RSS feed

ASP.NET: HyperLink and Label without Id attribute

Written by jf on August 4, 2008 – 6:00 am -

Today, I’ll talk a bit about how ASP.NET handles HTML rendering of controls such as <asp:HyperLink> and <asp:Label>.  If you have developed using ASP.NET you must have noticed that when an <asp:Hyperlink> or <asp:Label> is rendered you will find something like that:

<html>

<a id="ctl00_ContentPlaceHolder1_rptShoppingCart_ctl00_lnkGotoCheckout" href="#" class="someCss">delete</a>

</html>

From an ASP.NET point of view, this is fine, since all object rendered should be identified uniquely on the client side. But many a time these are useless, since these hyperlinks are not used to trigger any server side events. Thus the id attributes just takes up a lot of space on your HTML and Page Size when you have many links like that. If you don’t want to have those, here is a solution I came up with, you can create custom server controls that inherits from the control that you want to remove the id attribute tag from.

Here is sample of the code that i used to remove the id attribute from the Hyperlink Control, without basically removing all the features provided by the Hyperlink control such as being able to handle Resource files for globalization.

[ToolboxData("<{0}:HyperLinkEx runat=server></{0}:HyperLinkEx>")]
public class HyperLinkEx: System.Web.UI.WebControls.HyperLink
{
private string rel;

/// <summary>
/// Gets or sets the rel.
/// </summary>
/// <value>The rel.</value>
public string Rel
{
get { return rel; }
set { rel = value; }
}

/// <summary>
/// Renders the control to the specified HTML writer.
/// </summary>
/// <param name="writer">The <see cref="T:System.Web.UI.HtmlTextWriter"></see> object that receives the control content.</param>
protected override void Render(HtmlTextWriter writer)
{
StringBuilder sb = new StringBuilder();

sb.Append("<a");

if (this.NavigateUrl.StartsWith("~"))
this.NavigateUrl = ResolveClientUrl(this.NavigateUrl);
sb.Append(" href=\"" + this.NavigateUrl + "\"");

if (!string.IsNullOrEmpty(rel)) sb.Append(" rel=\"" + rel + "\"");
if (!string.IsNullOrEmpty(this.CssClass)) sb.Append(" class=\"" + this.CssClass + "\"");
if (!string.IsNullOrEmpty(this.Target)) sb.Append(" target=\"" + this.Target + "\"");
if (this.Attributes["onclick"] != null) sb.Append(" onclick=\"" + this.Attributes["onclick"] + "\"");

sb.Append(">");

if (!string.IsNullOrEmpty(this.ImageUrl)) //Some image were found
{
if (this.ImageUrl.StartsWith("~"))
this.ImageUrl = ResolveClientUrl(this.ImageUrl);
sb.Append("<img src=\"" + this.ImageUrl + "\" alt=\"" + this.Text + "\" title=\"" + this.Text + "\" border=\"0\" />");
}
else
{
sb.Append(this.Text);
}

sb.Append("</a>");
writer.Write(sb.ToString());
}

}

In this source code, i have overridden the render method of the control and outputted a cleaner HTML anchor with no Id attributes. I have also added support for ImageURL attribute of the hyperlink server control, thus maintaining the most of the asp:Hyperlink features.

I have also created a tiny library with some controls: <asp:HyperLink>, <asp:Label>, <asp:Image> which i think are the controls that need not have the id attribute in every scenario.

You can find it for download under GNU Public Licence here.

Subscribe to my RSS feed

Exception: Collection was modified; enumeration operation may not execute.

Written by jf on May 28, 2008 – 7:00 am -

It’s been a while that i didn’t get this Exception, while working, and yesterday, while I was performing some test on  a new module that I implemented i got this exception. I knew i did get it sometimes back last year, and managed to solve it. So here is one solution when you are dealing with this kind of issue:

IList<Product> productList = new List<Product>();
productList.Add(new Product("Some product 1");
productList.Add(new Product("Some product 2");
productList.Add(new Product("Some product 3");
productList.Add(new Product("Some product 4");

for(int i=(productList.Count -1); i >= 0; i--)
{
//Perform Edit, Update, Delete Operation using for .. i loop in reverse order
}

There is a list of other solutions that can be found on the following websites:
http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=372532&SiteID=1
http://forums.asp.net/p/1147145/1861164.aspx
http://www.experts-exchange.com/Programming/Programming_Languages/Dot_Net/ASP_DOT_NET/Q_21585534.html

Subscribe to my RSS feed

Copy custom object in C#

Written by jf on August 9, 2007 – 8:27 am -

There are several ways to clone custom object in .NET.

  • Using reflection to get information about each field and properties in the custom class, create a new instance and assign the proper value.
  • Manually implementing the clone method and assign each field and method and then return a new object
  • Using serialization and deserialization.

I have tried the third method, that can be found here, it seems to be working fine for the time being, more test need to be done for performance overhead.

Subscribe to my RSS feed