Thursday, May 30, 2013

Sql function for Proper case

Create function properCase(@string varchar(8000)) returns varchar(8000) as    
begin    
 if len(@string)>0  
 begin  
        set @string = lower(@string)    
        declare @i int    
        set @i = ascii('a')    
        while @i <= ascii('z')    
        begin    
               set @string = replace( @string, ' ' + char(@i), ' ' + char(@i-32))    
               set @i = @i + 1    
        end    
        set @string = char(ascii(left(@string, 1))-32) + right(@string, len(@string)-1)    
 end  
        return @string    
end

call this funcrion as :

select dbo.ProperCase('AJAY')

result is:
Ajay

Wednesday, May 29, 2013

Code for merging serveral PDF into single PDF in asp.net

For merging PDF into single PDF we need to include  namespace  mention below

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

and for it you need to add reference of ItextSharp DLL.

Now use this function for merging PDF.....


public  void MergeFiles(string destinationFile, string[] sourceFiles)
    {
        try
        {
            int f = 0;
            // we create a reader for a certain document
            PdfReader reader = new PdfReader(sourceFiles[f]);
            // we retrieve the total number of pages
            int n = reader.NumberOfPages;
            //Console.WriteLine("There are " + n + " pages in the original file.");
            // step 1: creation of a document-object
            Document document = new Document(reader.GetPageSizeWithRotation(1));
            // step 2: we create a writer that listens to the document
            PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(destinationFile, FileMode.Create));
            // step 3: we open the document
            document.Open();
            PdfContentByte cb = writer.DirectContent;
            PdfImportedPage page;
            int rotation;
            // step 4: we add content
            while (f < sourceFiles.Length)
            {
                int i = 0;
                while (i < n)
                {
                    i++;
                    document.SetPageSize(reader.GetPageSizeWithRotation(i));
                    document.NewPage();
                    page = writer.GetImportedPage(reader, i);
                    rotation = reader.GetPageRotation(i);
                    if (rotation == 90 || rotation == 270)
                    {
                        cb.AddTemplate(page, 0, -1f, 1f, 0, 0, reader.GetPageSizeWithRotation(i).Height);
                    }
                    else
                    {
                        cb.AddTemplate(page, 1f, 0, 0, 1f, 0, 0);
                    }
                    //Console.WriteLine("Processed page " + i);
                }
                f++;
                if (f < sourceFiles.Length)
                {
                    reader = new PdfReader(sourceFiles[f]);
                    // we retrieve the total number of pages
                    n = reader.NumberOfPages;
                    //Console.WriteLine("There are " + n + " pages in the original file.");
                }
            }
            // step 5: we close the document
            document.Close();
           
        }
        catch (Exception e)
        {
            string strOb = e.Message;
        }
    }
    public int CountPageNo(string strFileName)
    {
        // we create a reader for a certain document
        PdfReader reader = new PdfReader(strFileName);
        // we retrieve the total number of pages
        return reader.NumberOfPages;
    }


Calling of above function.....

 MergeFiles(ConfigurationManager.AppSettings["ReportPDFMergepath"].ToString() + "ClientMI-" + filePathName + ".pdf", sourceFiles);