Tuesday 25 February 2014

Read content of pdf using iTextSharp

How to read content from pdf file ?
Here is little code to do this stuff.

using iTextSharp.text.pdf.parser;
using System.Text;

namespace TestApplication
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string Path = @"C:\Users\Ajay\Downloads\Anish jesani.pdf";

            iTextSharp.text.pdf.PdfReader pr = new iTextSharp.text.pdf.PdfReader(Path);
            ITextExtractionStrategy pes = new SimpleTextExtractionStrategy();
            int pageCnt = pr.NumberOfPages; // get number of Pages
            string str = PdfTextExtractor.GetTextFromPage(pr, 1);    // 1 = Page number

            str = Encoding.UTF8.GetString(ASCIIEncoding.Convert(Encoding.Default,
 Encoding.UTF8, Encoding.Default.GetBytes(str)));
        }
    }
}

This is done using iTextSharp.dll

Thursday 20 February 2014

multiple DLL with same namespace in single application

Here is an little article for problem:
Problem : If we have two DLL with same Namespace. Then how can we maintain class if we have add this dll in our project, then what happen ?
Let’s see the solution.
We have 1st DLL : Lib1
In this we have following implementation
namespace Lib1
{
    public class Class1
    {
        public void Method()
        {
       
        }
    }
}

We have 2nd DLL : Lib2
In this we have following implementation
namespace Lib1
{
    public class Class1
    {
        public void Test()
        {
       
        }
    }
}


Now, Add reference of this dlls in other project.



Now if we are calling the method of dll from this application then we got following error.










This type of error can be solved using class alias Name like below.




By this you will not face any error. But new question is How to use this alias class. See below.
extern alias TestAlias;
using System;

public partial class Fullscreen : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Lib1.Class1 obj = new Lib1.Class1();
        obj.Method();

        TestAlias::Lib1.Class1 obj1 = new TestAlias::Lib1.Class1();
        obj1.Test();
    }
}
You must need to write  “extern alias”  on the top of the page otherwise .net reflect error.
This way you can use both or more then 2 dlls with same namespace in single application.