Reputation: 9
How do I get total PDF pages display in listbox or datagridview then display total number pages in the textbox?
This code only get current page's selected item in listbox or datagridview.
foreach (string file in files) { listBox1.Items.Add(Path.GetFullPath(file)); PdfReader pdfReader = new PdfReader(file); int numberOfPages = pdfReader.NumberOfPages; pdfPages.Text = pdfReader.NumberOfPages.ToString(); }
foreach (string dir in dirs){ listBox1.Items.Add((dir)); }
Upvotes: 0
Views: 537
Reputation: 9055
For one, you are setting numberOfPages
but then you aren't using it all.
If you are trying to get the total number of pages across all of the PDF files that files
contains (as your comment indicates), you should declare numberOfPages
outside of the foreach
and simply add to it within the foreach
:
int numberOfPages = 0;
foreach (string file in files)
{
listBox1.Items.Add(Path.GetFullPath(file));
PdfReader pdfReader = new PdfReader(file);
numberOfPages += pdfReader.NumberOfPages;
pdfPages.Text = pdfReader.NumberOfPages.ToString();
}
You can then simply set the Text
property of the textbox to numberOfPages.ToString()
:
MyTextBox.Text = numberOfPages.ToString();
Upvotes: 0