Vb.net 打印前获取记事本页数

Vb.net 打印前获取记事本页数,vb.net,streamreader,notepad,Vb.net,Streamreader,Notepad,我想在打印前得到记事本上的页数 我正在将记事本设置为wordwrap=true,FontSize=12,MarginRight=750,MarginLeft=750,MarginTop=1000,MarginBottom=1000, 大于70是页面等于1时的列数,51是页面也等于1时的行数 这是可行的,但我的公式中有一些错误,一些记事本页面可以,但有些不行 我希望有人能纠正我的代码 或者,即使更改了记事本设置,是否有适当的代码来完成此操作?如果没有适当的方法获取记事本页面,至少有人可以更正我的代

我想在打印前得到记事本上的页数

我正在将记事本设置为
wordwrap=true,FontSize=12,MarginRight=750,MarginLeft=750,MarginTop=1000,MarginBottom=1000,

大于
70
是页面等于
1时的列数,51
是页面也等于1时的行数

这是可行的,但我的公式中有一些错误,一些记事本页面可以,但有些不行

我希望有人能纠正我的代码

或者,即使更改了记事本设置,是否有适当的代码来完成此操作?如果没有适当的方法获取记事本页面,至少有人可以更正我的代码

谢谢

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
私有函数GetNotepadNumPage(ByVal文件名为字符串)为整数
Dim sr作为新的StreamReader(文件名)
作为字符串的尺寸线=sr.ReadLine
Dim CharL(9999)作为整数
Dim pCount作为整数=0
将pLine设置为整数=0
做而不做sr.EndOfStream
行=高级读取行()
CharL(pLine)=直线长度
pLine+=1
如果pLine=51,则
pCount+=1
对于i,作为整数=0到pLine
如果CharL(i)>70,则
pCount+=1
退出
如果结束
下一个
pLine=0
如果结束
环
高级关闭()
如果(pLine 0)那么
pCount+=1
对于i,作为整数=0到pLine
如果CharL(i)>70,则
pCount+=1
退出
如果结束
下一个
如果结束
pagecount=pCount
返回页数
端函数

这里有一个简单的包装器类,用于在.net中打印文本。它是用C#写的,但我很直截了当。它包括测量每页的计算。您可能可以使用以下选项:

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);
        }
    }
}
你可以用这个

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

您的notepad.exe设置将不会应用于streamreader。实际测试中,有些txt文件是正确的,不会下降或小于实际页码,但在其他txt文件中,它超过了页数。我认为记事本的设置可以改变页面的编号方式,因为如果我将页边距更改为较小,它会将页面的数量更改为较小。所以,我想我必须开发一个代码,扣除一些网页,因为真正的问题是它增加了2到3页的一些其他的TXT文件。我包括代码来计算空白行扣除页码,但仍然不是这样。谢谢,但我正在使用此代码将txt文件打印为String=tp&DbGridPapers.Rows(RowIndex).HeaderCell.Value.ToString Dim printproc作为新进程startinfo printproc.filename=filename printproc.Verb=“print”printproc.WindowStyle=ProcessWindowStyle.Hidden Process.Start(printproc)您只需将我的代码编译为.dll,然后将其作为项目的引用。从.dll导入名称空间后,声明类的实例并设置相应的属性,然后调用print方法。要加载文本文件,请使用file.readalltext方法。如果我不清楚您的意思,很抱歉,但我在这里的步骤是在打印前了解txt文件页面。我想你是在给我一个程序,让我知道打印后打印的页面。如本链接所示:我已经测试了代码,它不起作用,它将页面计数更改为ZeR0s。谢谢如果代码仍然很难获得,那么我想我必须使用MsWORD count页面。也许以后吧。对不起,代码不正确。谢谢,我希望我能做些改变。您好,我停下来,我使用了msword页面计数,然后使用记事本打印,然后将记事本页边距设置为750,现在效果很好。同时,当我提供的代码没有解决方案时。如果我能设法为你们两个得分,我会的,但我不能,因为我只是一个新手在这里,它不允许我给点,为此感到抱歉。不过还是谢谢你。