Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
250 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 Reply

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...