Thursday, January 21, 2016

Best method to extract text from PDF in asp.net C#


There are many ways to extract text from PDF.
  • Microsoft IFilter interface and Adobe IFilter implementation
  • iTextSharp
  • PDFBox
Among those solutions using PDFBox is the best according to my experiences because,
  • Using Adobe IFilter implementation need to have lot of other things set up in running machine like,
    • Windows 2000 or later
    • Adobe Acrobat or Reader 7.0.5+
    • IFilter COM wrapper class
  • iTextSharp is opensource but can't be used in commercial products.
So the best solution is using PDFBox. It doesn't need other things installed on your machine.

PDFBox is a java PDF library and there is a .net version of PDFBox (It can be downloaded from here


Here is an example how to use PDFBox


using org.apache.pdfbox.pdmodel;
using org.apache.pdfbox.util;

// ...

private static string ExtractTextFromPdf(string path)
{
  PDDocument doc = null;
  try {
    doc = PDDocument.load(path)
    PDFTextStripper stripper = new PDFTextStripper();
    return stripper.getText(doc);
  }
  finally {
    if (doc != null) {
      doc.close();
    }
  }
}  

NOTE:
  1. Following dll files of PDFBox library should be added to bin file of your project
    • commons-logging.dll
    • fontbox-1.8.9.dll
    • IKVM.OpenJDK.Text.dll
    • IKVM.OpenJDK.Util.dll
    • IKVM.Runtime.dll
  2. Reference for following dll s should be added
    • IKVM.OpenJDK.Core.dll
    • IKVM.OpenJDK.SwingAWT.dll
    • pdfbox-1.8.9.dll

Deleting Files in ASP.NET C#

Simplest way to delete files in asp.net C#

if ((System.IO.File.Exists(fileName)))
                {
                    System.IO.File.Delete(fileName);
}


This is a simple code snippet in asp.net that can be used to delete files from any directory.
In here variable "fileName" is the specific path to the file.

Ex: If want to delete a file called "hello.pdf" in "upload" folder which is in my C drive, file name should be full path to that file "C:\upload\hello.pdf"


Monday, January 11, 2016

What is Windows Power Shell & how it differs from CMD

Windows PowerShell is a task automation and configuration management framework from Microsoft, consisting of a command-line shell and associated scripting language built on the .NET Framework.
If it sounds complicated you can think it as same as the CMD prompt but with more advance tools.
PowerShell is a much more powerful command-line shell and scripting language than the Command Prompt is. It gives Windows system administrators a useful command-line environment.

There are some fabulous things that can be done using Power Shell
  • perform your favorite CMD tasks
  • Kill processes in power shell instead of using task manager
  • Export NTFS file permossions
  • Play with power sell 2.0
  • Background a time consuming task
  • Report all of the USB devices installed
( Read more about these tasks)

There are few main differences between cmd prompt and power shell
  • Power sell uses a different set of commands called cmdlets.Many system administration tasks are exposed via Power Shell cmdlets
  • PowerShell makes use of pipes, just as Linux and other Unix-like systems do. 
  • You can create complex scripts for managing Windows systems much more easily than you could with the Command Prompt.
So here is why people use Power Sell over cmd prompt
  • Cmd prompt is limited, can’t access many Windows system administration features.
  • It is is more difficult to compose complex scripts with 
PowerShell is a new environment for Windows system administrators that allows them to use a more modern command-line environment to manage Windows.

Saturday, December 5, 2015

How to use google fonts in your html

When we creating a web page one of the main problem we face is using fonts we want in our web page. That is not easy because in order to display the correct font in viewer's browser that font should be installed in that person's device.
Otherwise our web page will not be displayed properly in viewer's device.
Using google fonts is the best solution for that.

How to use Google fonts

  1. Type the name of font that you need and find it (In example below finding "Bree Serif" font is shown)
  2. Change the appearance of your font (bold, italic etc)
  3. Click add to collection.
  4. After adding all fonts you want by following step 1 to 3 repeatedly click on use button.
  5. In the next interface scroll down a bit and find the CSS link for your fonts and include it in your html as a style reference (It will render those fonts from that link)


Now you can use font you wanted as <FONT FACE="Bree Serif">Bree Serif font</FONT>
Because of Google fonts you don't need to worry about viewer's installed font styles. You can use what ever font you need and make an attractive web pages.

Feel free to add a comment if something was not clear to you or you have something to add.

Thursday, December 3, 2015

How to use a long URL using bit.ly URL shortner

Some times we have to deal with very long URLs and that is very annoying. Here is a simple solution foe that. This not only shorten your URL but provide many other supports such as keep track of number of clicks on the links.

Here you go.
You can download this cool video by this link

Thursday, November 26, 2015

Algorithm : Maximal possible sum of some consecutive items in an integer array


Lets think of an array like,
[ 2, 3, 8, 1, 12, 2, 4, 5]

In this array maximum sum of any 2 consecutive numbers is 14(12+2) and maximum sum of any 3 consecutive numbers is 21(8+1+12).

Here is a simple algorithm using Java for find the maximum sum of some consecutive numbers in an integer array. This is just a simple solution for this problem.

Feel free to add a comment if anyone has a optimized solution or anything hard to understand.

static int  arrayMaxConsecutiveSum(int[] inputArray, int k) {
 int result = 0; 
 int currentSum = 0; 
 for (int i = 0; i < k - 1; i++) { 
  currentSum += inputArray[i]; 
 } 
 for (int i = k - 1; i < inputArray.length; i++) {
  currentSum += inputArray[i] ; 
  if (currentSum > result) {
   result = currentSum; 
  } 
  currentSum -= inputArray[i - k + 1]; 
 } 
 return result; 
}


Go to this link to find out how to implement this code in Java

Wednesday, November 25, 2015

Useful and Elegant Terminal Emulators for Linux

A terminal emulator is a program that emulates a video terminal within some other display architecture. Though typically synonymous with a shell or text terminal, the term terminal covers all remote terminals, including graphical interfaces.
That is how Wikipedia  define what a terminal emulator is.
It allows the user access to a text terminal and all its applications such as command-line interfaces (CLI) and text user interface (TUI) applications. On Unix-like operating systems, it is common to have one or more terminal windows connected to the local machine.
So people use different types of Terminal Emulators according to their performances and preference of users.
Lets see some of different terminal emulators that can be used in Linux operating systems.

  1. Tilda
    Tilda is a GTK+ Terminal emulator and it is based on the VTE terminal emulator widget underlying GNOME terminal. They say that its design was inspired from consoles which was in computer games like Quake. Running Tilda can be faster than launching a new terminal with a keyboard shortcut because the program is already loaded into memory. So it can be useful to people who frequently find themselves opening and closing terminals.
    It has a nice interface with highly customization options and excellent built in color schemes.

    It is a free software licensed under the terms of the GNU general public license.

    Tilda home page

  2. Guake
    Guake is a drop-down terminal made for the GNOME desktop environment. Guake's style of window is based on an FPS game, and one of its goals is to be easy to reach.
    Guake is mostly written in python and has a little piece in C. Guake is packaged by a number of distributions, among which are Fedora, Debian, Ubuntu or ArchLinux.
    special Features
    • Multi tab.
    • Lightweight
    • Open URL to your browser
    • Save terminal content to a file
    Guake Screenshots

    Guake home page

Optimize you working enviorenment : Single command to create & move to a directory in linux (C Shell, Bash)

Usually move to a directory just after creating is bit of a anxious task specially if the directory name is too long. mkdir long-name-of...