How to restrict a content of string to less than 4MB and save that string in DB using C#

Posted by Pranay B on Stack Overflow See other posts from Stack Overflow or by Pranay B
Published on 2014-07-12T07:35:30Z Indexed on 2014/08/19 10:20 UTC
Read the original article Hit count: 262

Filed under:
|
|
|

I'm working on a project where I need to get the Text data from pdf files and dump the whole text in a DB column. With the help of iTextsharp, I got the data and referred it String.

But now I need to check whether the string exceeds the 4MB limit or not and if it is exceeding then accept the string data which is less than 4MB in size.

This is my code:

internal string ReadPdfFiles()
{
        // variable to store file path
        string filePath = null;

        // open dialog box to select file
        OpenFileDialog file = new OpenFileDialog();

        // dilog box title name
        file.Title = "Select Pdf File";

        //files to be accepted by the user.
        file.Filter = "Pdf file (*.pdf)|*.pdf|All files (*.*)|*.*";

        // set initial directory of computer system
        file.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);

        // set restore directory
        file.RestoreDirectory = true;

        // execute if block when dialog result box click ok button
        if (file.ShowDialog() == DialogResult.OK)
        {
            // store selected file path
            filePath = file.FileName.ToString();
        }

        //file path
        /// use a string array and pass all the pdf for searching
        //String filePath = @"D:\Pranay\Documentation\Working on SSAS.pdf";
        try
        {
            //creating an instance of PdfReader class
            using (PdfReader reader = new PdfReader(filePath))
            {
                //creating an instance of StringBuilder class
                StringBuilder text = new StringBuilder();

                //use loop to specify how many pages to read.
                //I started from 5th page as Piyush told
                for (int i = 5; i <= reader.NumberOfPages; i++)
                {
                    //Read the pdf 
                    text.Append(PdfTextExtractor.GetTextFromPage(reader, i));
                }//end of for(i)

                int k = 4096000;
                //Test whether the string exceeds the 4MB
                if (text.Length < k)
                {
                    //return the string
                    text1 = text.ToString();
                } //end of if
            } //end of using
        } //end try
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message, "Please Do select a pdf file!!", MessageBoxButtons.OK, MessageBoxIcon.Warning);
        } //end of catch

        return text1;
} //end of ReadPdfFiles() method

Do help me!

© Stack Overflow or respective owner

Related posts about c#

Related posts about string