Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
234 views
in Technique[技术] by (71.8m points)

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

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:PranayDocumentationWorking 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!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

There are several possibilities:

  1. You could read the documentation for the StringBuilder class.
  2. You could init your stringbuilder with a maximum capacity.
  3. You could use StringBuilder.ToString(0, maxlength)
  4. You could use StringBuilder.ToString().Substring(0, maxlength)

BTW: 4MB = 4194304 Bytes


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...