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

标签 vb.net streamreader notepad

我正在尝试在打印之前获取记事本的页数,

我将记事本设置为 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

最佳答案

这是一个用于在 .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);
        }
    }
}

关于vb.net - 打印前获取记事本页数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16882142/

相关文章:

asp.net - Windows 7 无法创建 SSL/TLS 安全通道。"} System.Net.WebException

c# - 生物识别指纹正在提供其他人的信息

c# - 在输入文件中找不到字符串

windows - Windows 中的无扩展名文件

windows - 使用dart:io将结尾写入文件

c# - 如何从某个网页复制所有文本并将其保存到记事本C#

.net - 方法 '' 无法处理事件 '',因为它们没有兼容的签名

c# - StreamReader 与 BinaryReader?

c# - 一种优雅的方式来使用(所有字节的)BinaryReader?

mysql - 使用参数时出现 SQL 语法错误