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 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…