美文网首页
C#图形文字打印预览的实例

C#图形文字打印预览的实例

作者: 大龙10 | 来源:发表于2022-01-15 06:34 被阅读0次
https://docs.microsoft.com/zh-cn/dotnet/desktop/winforms/advanced/windows-forms-print-support?view=netframeworkdesktop-4.8

一、C#窗体打印组件

Windows 窗体中的打印主要包括:

  • 使用PrintDocument 组件来使用户能够打印,
  • PrintPreviewDialog 组件
  • PrintDialog 组件
  • PageSetupDialog 组件

二、C#打印的编程

  • 创建组件的新实例 PrintDocument ,使用和类设置PrinterSettings 描述打印内容的属性, 然后调用 Print 方法以实际打印文档。

  • 如何:创建标准的 Windows 窗体打印作业
    说明如何使用 PrintDocument 组件从 Windows 窗体打印。

  • 如何:在运行时从 PrintDialog 中捕获用户输入
    说明如何使用组件以编程方式修改选定的打印选项 PrintDialog 。

  • 如何:在 Windows 窗体中选择附加到用户计算机的打印机
    描述如何在运行时使用组件更改要打印到的打印机 PrintDialog 。

  • 如何:打印 Windows 窗体中的图形
    介绍如何将图形发送到打印机。

  • 如何:打印 Windows 窗体中的多页文本文件
    介绍如何将文本发送到打印机。

  • 如何:完成 Windows 窗体打印作业
    说明如何提醒用户完成打印作业。

  • 如何:打印 Windows 窗体
    说明如何打印当前窗体的副本。

  • 如何:使用打印预览在 Windows 窗体中进行打印
    演示如何使用 PrintPreviewDialog 来打印文档。

三、获取目录的名称

获取和设置包含该应用程序的目录的名称。
result: X:\xxx\xxx\ (.exe文件所在的目录 + "")

docPath = System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase;

四、选择打印机

DialogResult result = printDialog1.ShowDialog();
if (result == DialogResult.OK)
{
printDocument1.Print();
}

五、打印图形

使用 类的 PrintPage Graphics 属性指示 PrintPageEventArgs 打印机要打印的图形类型。

六、多页打印的处理

  • 在 PrintPage 事件处理程序中,使用 PrintPageEventArgs 类的 Graphics 属性和文档内容来计算行长度和每页行数。
  • 绘制完每一页后,检查它是否是最后一页,并相应地设置 HasMorePages 的 PrintPageEventArgs 属性。
  • 引发 PrintPage 事件,直到 HasMorePages 为 false。
  • 此外,确保 PrintPage 事件与其事件处理方法关联。

七、打印预览

打印预览服务添加到你的应用程序有一个简单的方法,就是将 PrintPreviewDialog 控件与用于打印文件的 PrintPage 事件处理逻辑结合使用。

八、程序

设计器
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.Drawing.Printing;

namespace Ky_PrintPreviewApp
{
    public partial class Form1 : Form
    {
        // Declare a string to hold the entire document contents.
        private string documentContents;

        // Declare a variable to hold the portion of the document that
        // is not printed.
        private string stringToPrint;
        Bitmap memoryImage;

        private void CaptureScreen()
        {
            Graphics myGraphics = this.CreateGraphics();
            Size s = this.Size;
            memoryImage = new Bitmap(s.Width, s.Height, myGraphics);
            Graphics memoryGraphics = Graphics.FromImage(memoryImage);
            memoryGraphics.CopyFromScreen(this.Location.X, this.Location.Y, 0, 0, s);
        }

        public Form1()
        {
            InitializeComponent();
        }

        private void ReadDocument()
        {
            string docName = "testPage.txt";
            string docPath = @"c:\";
            //获取和设置包含该应用程序的目录的名称。 result: X:\xxx\xxx\ (.exe文件所在的目录 + "\")
            docPath = System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase;
           
            printDocument1.DocumentName = docName;
            using (FileStream stream = new FileStream(docPath + docName, FileMode.Open))
            using (StreamReader reader = new StreamReader(stream))
            {
                documentContents = reader.ReadToEnd();
            }
            stringToPrint = documentContents;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            CaptureScreen();
            printPreviewDialog1.Document = printDocument1;
            printPreviewDialog1.ShowDialog();
            //printDocument2.Print();
        }

        private void printDocument2_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(stringToPrint, this.Font,
                e.MarginBounds.Size, StringFormat.GenericTypographic,
                out charactersOnPage, out linesPerPage);

            // Draws the string within the bounds of the page.
            e.Graphics.DrawString(stringToPrint, this.Font, Brushes.Black,
            e.MarginBounds, StringFormat.GenericTypographic);

            // Remove the portion of the string that has been printed.
            stringToPrint = stringToPrint.Substring(charactersOnPage);

            // Check to see if more pages are to be printed.
            e.HasMorePages = (stringToPrint.Length > 0);

            // If there are no more pages, reset the string to be printed.
            if (!e.HasMorePages)
                stringToPrint = documentContents;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            ReadDocument();
            printPreviewDialog1.Document = printDocument2;
            printPreviewDialog1.ShowDialog();
        }

        private void printDocument1_PrintPage(object sender, PrintPageEventArgs e)
        {
            e.Graphics.DrawImage(memoryImage, 0, 0);
        }

        private void printDocument2_EndPrint(object sender, PrintEventArgs e)
        {
           // MessageBox.Show(printDocument2.DocumentName +" 完成打印.");
        }

        private void button3_Click(object sender, EventArgs e)
        {
            DialogResult result = printDialog1.ShowDialog();
            if (result == DialogResult.OK)
            {
                printDocument1.Print();
            }
        }
    }
}

运行的结果

相关文章

网友评论

      本文标题:C#图形文字打印预览的实例

      本文链接:https://www.haomeiwen.com/subject/mdoncrtx.html