Tuesday, January 6, 2015

How to post the data using HTTP Listener in asp.net

How to post the data using HTTP Listener in asp.net

Step1:- Create once sample Empty project name it as “HTTPListener

Step2:- And add the new Generic Handler template and name it as “HandlerListener

This will create one default method is

public void ProcessRequest(HttpContext context)

This method will process the request and resend the request

Step3:- Write below code
public string GetPostDataFromRequest(HttpRequest httpRequest)
        {
            string inputString = string.Empty;
            StreamReader reader = null;
            if (httpRequest.InputStream != null)
            {
                reader = new StreamReader(httpRequest.InputStream);
                inputString = reader.ReadToEnd().Trim();
                reader.Close();
            }
            return inputString;
        }

Step4:- Add the below code in the processrequest method

string strPostData = this.GetPostDataFromRequest(context.Request);
context.Response.ContentType = "text/plain";
context.Response.Write(strPostData);

The complete code is look like

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;

namespace HTTPListener
{
    /// <summary>
    /// Summary description for HandlerListner
    /// </summary>
    public class HandlerListener : IHttpHandler
    {

        public string GetPostDataFromRequest(HttpRequest httpRequest)
        {
            string inputString = string.Empty;
            StreamReader reader = null;
            if (httpRequest.InputStream != null)
            {
                reader = new StreamReader(httpRequest.InputStream);
                inputString = reader.ReadToEnd().Trim();
                reader.Close();
            }
            return inputString;
        }
        public void ProcessRequest(HttpContext context)
        {
            string strPostData = this.GetPostDataFromRequest(context.Request);
            context.Response.ContentType = "text/plain";
            context.Response.Write(strPostData);
        }


        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}

Step5:- Finally run the code and check the URL

Steps to checking the URL working process

Step1:-For testing the URL we use the Mozilla Firefox Browser
In that browser add POSTER Add on

Step2:- To run the poster add on Toolsà Posterà
The poster will look like


Step3:- Type the URL and write some data in the content part and click the POST button
This will replay the message what you typed

E.g.:- I entered “Hi lokesh how r u”
The Listener will the replay the same
Using this code you can customize the and meets you requirements

 JJJJJJJJJ
Happy Coding …………………………………………….

JJJJJJJJJ
Read More »

Hot to Get the Image form the PDF file in WINDOWS C#

Hot to Get the Image form the PDF file in WINDOWS C#

Step1:- Add the reference of the DLL

using iTextSharp.text;
using iTextSharp.text.pdf;

Download the iTextSharp DLL from this link


The full name spaces will look like
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using iTextSharp.text;
using iTextSharp.text.pdf;
using System.IO;
using System.Drawing.Imaging;

Step2:- Create one public class with the name “ExtractImages

And write the below code

public class ExtractImages
    {


        #region ExtractImagesFromPDF
        public static void ExtractImagesFromPDF(string sourcePdf, string outputPath)
        {
            // NOTE:  This will only get the first image it finds per page.
            PdfReader pdf = new PdfReader(sourcePdf);
            RandomAccessFileOrArray raf = new iTextSharp.text.pdf.RandomAccessFileOrArray(sourcePdf);

            try
            {
                for (int pageNumber = 1; pageNumber <= pdf.NumberOfPages; pageNumber++)
                {
                    PdfDictionary pg = pdf.GetPageN(pageNumber);
                    PdfDictionary res =
                      (PdfDictionary)PdfReader.GetPdfObject(pg.Get(PdfName.RESOURCES));
                    PdfDictionary xobj =
                      (PdfDictionary)PdfReader.GetPdfObject(res.Get(PdfName.XOBJECT));
                    if (xobj != null)
                    {
                        foreach (PdfName name in xobj.Keys)
                        {
                            PdfObject obj = xobj.Get(name);
                            if (obj.IsIndirect())
                            {
                                PdfDictionary tg = (PdfDictionary)PdfReader.GetPdfObject(obj);
                                PdfName type =
                                  (PdfName)PdfReader.GetPdfObject(tg.Get(PdfName.SUBTYPE));
                                if (PdfName.IMAGE.Equals(type))
                                {

                                    int XrefIndex = Convert.ToInt32(((PRIndirectReference)obj).Number.ToString(System.Globalization.CultureInfo.InvariantCulture));
                                    PdfObject pdfObj = pdf.GetPdfObject(XrefIndex);
                                    PdfStream pdfStrem = (PdfStream)pdfObj;
                                    byte[] bytes = PdfReader.GetStreamBytesRaw((PRStream)pdfStrem);
                                    if ((bytes != null))
                                    {
                                        using (System.IO.MemoryStream memStream = new System.IO.MemoryStream(bytes))
                                        {
                                            memStream.Position = 0;
                                            // ByteArrayToImage(bytes);

                                            System.Drawing.Image img = System.Drawing.Image.FromStream(memStream, true, true);
                                            // must save the file while stream is open.
                                            if (!Directory.Exists(outputPath))
                                                Directory.CreateDirectory(outputPath);

                                            string path = Path.Combine(outputPath, String.Format(@"{0}.jpg", pageNumber));
                                            System.Drawing.Imaging.EncoderParameters parms = new System.Drawing.Imaging.EncoderParameters(1);
                                            parms.Param[0] = new System.Drawing.Imaging.EncoderParameter(System.Drawing.Imaging.Encoder.Compression, 0);
                                            // GetImageEncoder is found below this method
                                            System.Drawing.Imaging.ImageCodecInfo jpegEncoder = GetImageEncoder("JPEG");
                                            img.Save(path, jpegEncoder, parms);
                                            break;

                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }

            catch
            {
                throw;
            }
            finally
            {
                pdf.Close();
            }


        }
        #endregion

        //public static System.Drawing.Image ByteArrayToImage(byte[] input)
        //{
        //    try
        //    {
        //        var ms = new MemoryStream(input);
        //        var tempByteArrayToImage = System.Drawing.Image.FromStream(ms);
        //        ms.Dispose();
        //        return tempByteArrayToImage;
        //    }
        //    catch (Exception ex)
        //    {
        //        //ShowMessage.ShowError(ex.Message);
        //        return null;
        //    }
        //}

        #region GetImageEncoder
        public static System.Drawing.Imaging.ImageCodecInfo GetImageEncoder(string imageType)
        {
            imageType = imageType.ToUpperInvariant();



            foreach (ImageCodecInfo info in ImageCodecInfo.GetImageEncoders())
            {
                if (info.FormatDescription == imageType)
                {
                    return info;
                }
            }

            return null;
        }
        #endregion

    }


Step3:- Create once windows (Win) form and write the code in the page load event or in the in the button click (as your wish to use where ever).

private void Form1_Load(object sender, EventArgs e)
        {
            string path = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase);
            string localPath = new Uri(path).LocalPath;
            ExtractImages.ExtractImagesFromPDF(localPath + "\\SAMPLE.pdf", localPath);

        }



Note:-

Make sure place once PDF file having images in the debug folder.

I am using the SAMPLE.PDF file in the Tutorial

After execute the code check the debug folder you will find the extracted images in the folder

And also customize the code as you want base on the requirement 
Read More »

How to use simple PIVOT in SQL server(Static Columns)

How to use simple PIVOT in SQL server(Static Columns) 

PIVOT: - Pivot is the concept of converting the rows to the columns & columns to the rows

This is the static columns display (User manually write the columns names )

Declare @SampleTable as table (Sno int,Names nvarchar(50),Subjects nvarchar(50),
Marks int,Location nvarchar(50));
insert into @SampleTable VALUES(1,'Lokesh','Maths',95,'Hyderabad');
insert into @SampleTable VALUES(2,'Lokesh','Science',25,'Hyderabad');
insert into @SampleTable VALUES(3,'Krishna','Maths',59,'Chennai');
insert into @SampleTable VALUES(4,'Krishna','Science',56,'Chennai');
insert into @SampleTable VALUES(5,'Google','Maths',25,'UnNone');
insert into @SampleTable VALUES(6,'Google','Science',80,'UnNone');
--This is the Existing Table Result
SELECT * FROM @SampleTable;
with CTE
as
(
      SELECT * from @SampleTable
      PIVOT
      (
            SUM(Marks) FOR Subjects IN ([Maths],Science)   
      ) as pivottable
)
--This is PIVOT table result

SELECT Names,Location,SUM(Maths) Maths,SUM(Science)Science from CTE group by  Names,Location
Read More »