Click here to Skip to main content
15,889,595 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
This is a program which selects a .ps file and shows the number of records and number of pages that will show on the application however after making a GetPageCount method, how do I call that mrthod in my openCVSFile method?

<pre lang="C#">public void openCVSFile(object sender, EventArgs e)//Opens the csv file
    {
        ofd.Multiselect = false;
        ofd.Filter = "Post Script files (*.ps)|*.ps";
        ofd.FilterIndex = 1;
        if(ofd.ShowDialog() == DialogResult.OK)
        {
            string filePath = ofd.FileName;
            StreamReader streamReader = new StreamReader(filePath);//reads file
            string[] linesOfData = new string[File.ReadAllLines(filePath).Length];
            int recordCount= -1;//title doesnt count towards number of records
            int pageCount = GetPageCount(filePath);
            for(int i = 0; i < linesOfData.Length; i++)//counts how many records there are
            {
                recordCount++;
            }
            

            txtRecordCount.Text = ("Number of Records: "+ recordCount+ " records");
            txtPrintCount.Text = ("Print count: " + pageCount + "pages");
        }
    }
    public static int GetPageCount(PrintDocument printDocument)//counts number of pages ()?
    {
        int count = 0;
        printDocument.PrintController = new PreviewPrintController();
        printDocument.PrintPage += (sender, e) => count++;
        printDocument.Print();
        return count;
    }


What I have tried:

Not too sure what to do with this code because its not working
Posted
Updated 10-Apr-22 22:03pm

How about replacing
string[] linesOfData = new string[File.ReadAllLines(filePath).Length];

by
string[] linesOfData = File.ReadAllLines(filePath);
?

The former reads everything without storing it at all, counts the lines, and creates an array with a null for every line; the latter simply returns the lines.

When in doubt, look in the documentation[^] (just google C# ReadAllLines)
 
Share this answer
 
v2
Comments
Daniel Tsang 11-Apr-22 0:59am    
Thanks but I want to know how to get Page count before the user prints out thr file
You cannot read a PostScript file in that way, it is not a simple text file. See PostScript - Wikipedia[^].
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900