2

I'm trying to get the number of pages notepad before print,

I'm setting the notepad to wordwrap=true, FontSize=12, MarginRight=750, MarginLeft=750, MarginTop=1000, MarginBottom=1000,

Greater than 70 is the number of column for page is equal to 1 and 51 is the number of line when page is equal to 1 also.

It's working But some mistakes on the formula I have, some notepad pages gets Ok, but some are not.

I hope someone could correct the code what I have.

Or is there any proper code to get this done even if notepad settings are changed, if there is no proper way of getting the notepad pages, at-least someone could correct the code I have.

Thanks.

Private Function GetNotepadNumPage(ByVal filename as string) as Integer    
Dim sr As New StreamReader(filename)
        Dim line As String = sr.ReadLine
        Dim CharL(9999) As Integer
        Dim pCount As Integer = 0
        Dim pLine As Integer = 0
        Do While Not sr.EndOfStream
            line = sr.ReadLine()
            CharL(pLine) = line.Length
            pLine += 1
            If pLine = 51 Then
                pCount += 1
                For i As Integer = 0 To pLine
                    If CharL(i) > 70 Then
                        pCount += 1
                        Exit For
                    End If
                Next
                pLine = 0
            End If
        Loop
        sr.Close()

        If (pLine <> 0) Then
            pCount += 1
            For i As Integer = 0 To pLine
                If CharL(i) > 70 Then
                    pCount += 1
                    Exit For
                End If
            Next
        End If

        pagecount = pCount
Return pagecount
End Function
matzone
  • 5,703
  • 3
  • 17
  • 20
XXXXXXXXXXXXXX
  • 155
  • 1
  • 3
  • 15
  • your notepad.exe setting will not applied in your streamreader .. – matzone Jun 02 '13 at 11:37
  • Actually tested there are some txt files that are correct and doesn't go down or less than the real page number, but in other txt files it exceeds the number of pages. I think the notepad settings can change the way of numbering the pages, because if I change the Margin to lesser, it changes the number of pages to less. So I think I must develop a code that deducts a number of pages because the real problem is it adds 2 to 3 pages of some other txt files. – XXXXXXXXXXXXXX Jun 02 '13 at 16:45
  • I included the code to count the blank lines to deduct page number, but still not the way. – XXXXXXXXXXXXXX Jun 02 '13 at 18:43

2 Answers2

1

Here's a simple wrapper class for printing text in .net. It's written in C# but ifairly straight forward. It includes calculations for measuring each page. You might be able to use this:

using System;
using System.Drawing;
using System.Drawing.Printing;
namespace PrintDocClass
{
    public class PrintDoc
    {
        private PrintDocument pd1 = new PrintDocument();
        private Font _pdfont = new Font("Microsoft Sans Serif", 8.25f);
        private string _PrintString = "";
        private string _Name = "";
        private bool _Landscape = false;
        public PrintDoc(string PrintString, bool Landscape = false)
        {
            _PrintString = PrintString;
            _Landscape = Landscape;
        }
        public PrintDoc(string PrintString, string DocName, bool Landscape = false)
        {
            _PrintString = PrintString;
            _Name = DocName;
            _Landscape = Landscape;
        }
        public PrintDoc(string PrintString, string DocName, Font PrintFont, bool Landscape = false)
        {
            _PrintString = PrintString;
            _Name = DocName;
            _pdfont = PrintFont;
            _Landscape = Landscape;
        }
        public void Print()
        {
            pd1.DefaultPageSettings.Landscape = _Landscape;
            pd1.PrintPage += new PrintPageEventHandler(pd1_PrintPage);
            if (_Name != "")
                _PrintString = _Name + "\n\n" + _PrintString;
            pd1.Print();
        }
        private void pd1_PrintPage(object sender, PrintPageEventArgs e)
        {
            int charactersOnPage = 0;
            int linesPerPage = 0;
            // Sets the value of charactersOnPage to the number of characters
            // of stringToPrint that will fit within the bounds of the page.
            e.Graphics.MeasureString(_PrintString, _pdfont,
            e.MarginBounds.Size, StringFormat.GenericTypographic,
            out charactersOnPage, out linesPerPage);
            // Draws the string within the bounds of the page
            e.Graphics.DrawString(_PrintString, _pdfont, Brushes.Black,
            e.MarginBounds, StringFormat.GenericTypographic);
            // Remove the portion of the string that has been printed.
            _PrintString = _PrintString.Substring(charactersOnPage);
            // Check to see if more pages are to be printed.
            e.HasMorePages = (_PrintString.Length > 0);
        }
    }
}
tinstaafl
  • 6,908
  • 2
  • 15
  • 22
  • thanks but I'm using this code to print a txt file > Dim filename As String = tp & DbGridPapers.Rows(RowIndex).HeaderCell.Value.ToString Dim printproc As New ProcessStartInfo printproc.FileName = filename printproc.Verb = "Print" printproc.WindowStyle = ProcessWindowStyle.Hidden Process.Start(printproc) – XXXXXXXXXXXXXX Jun 03 '13 at 06:40
  • All you need to do is compile my code as a .dll, then iadd it as a reference to your project. Once you Import the namespace from the .dll you declare an instance of the class setting the appropriate properties then call the print method. To load a text file use file.readalltext method. – tinstaafl Jun 03 '13 at 07:19
  • Sorry if I don't understand you clearly, but my procedure here is to know the txt files pages before print. I think you are giving me a procedure to know the pages printed after print. Like shown in this link: [How to print txt files](http://msdn.microsoft.com/en-us/library/system.drawing.printing.printdocument%28v=vs.80%29.aspx) – XXXXXXXXXXXXXX Jun 03 '13 at 07:43
1

You can use this ..

Private Function GetNotepadNumPage(ByVal filename As String) As Integer
        Dim sr As New StreamReader(filename)
        Dim sLine As String
        Dim sBuff As String
        Dim nPage As Integer = 0
        Dim nLine As Integer = 0
        Dim n As Integer

        Do While Not sr.EndOfStream
            sLine = sr.ReadLine()

            sBuff = sBuff & sLine

            If sBuff.Length > 70 Then
                n = sBuff.Length \ 70
                nLine += n
                sBuff = Mid(sBuff, 70 * n + 1)
                'MsgBox(sBuff)
            End If

            If nLine > 51 Then
                nPage += 1
                nLine = nLine - 51
            End If
        Loop
        'probably sBuff not empty
        If sBuff.Length > 0 Then
            nLine += 1
            If nLine = 1 Or nPage = 0 Then nPage += 1
        End If

        Return nPage
    End Function
End Class
matzone
  • 5,703
  • 3
  • 17
  • 20
  • I've tested the code, it doesn't work, it changes page count to ZeR0s. thanks – XXXXXXXXXXXXXX Jun 03 '13 at 11:40
  • If still the code is hard to get, then I think I would have to use MsWORD count page. Maybe Later. – XXXXXXXXXXXXXX Jun 03 '13 at 11:41
  • I'm sorry, the code doesn't do right. thanks, I'll try to make some changes, I hope. – XXXXXXXXXXXXXX Jun 03 '13 at 13:12
  • hello, I stop, I used the msword page count, then use notepad to print, then sets the notepad margins to all 750, and it works good now. for the mean time when there is no solution about the codes I provide. If only I can managed to hit points for both of you, I will, but I can't because I'm just a newbie here and it doesn't allow me to give points, sorry for that. But thanks anyways. – XXXXXXXXXXXXXX Jun 05 '13 at 17:30