首页 > 解决方案 > 从 C# 打印时缺少 Form 类

问题描述

我正在尝试了解如何使用“Microsoft Visual Studio Community 2019”从 C# 打印。我有一个示例程序,但在尝试编译时出现错误:

严重性代码描述项目文件行抑制状态错误 CS1069 在命名空间“System.Drawing”中找不到类型名称“字体”。此类型已转发到程序集 'System.Drawing.Common, Version=4.0.2.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51' 考虑添加对该程序集的引用。nemonlabel E:\usr\Nemonlabel\nemonlabel02\nemonlabel\Printroutine.cs 9 活动

严重性代码描述项目文件行抑制状态错误 CS1069 在命名空间“System.Drawing.Printing”中找不到类型名称“PrintPageEventArgs”。此类型已转发到程序集 'System.Drawing.Common, Version=4.0.2.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51' 考虑添加对该程序集的引用。nemonlabel E:\usr\Nemonlabel\nemonlabel02\nemonlabel\Printroutine.cs 19 活动

似乎这些不应该发生任何人都可以建议示例代码(下面)中缺少什么吗?

/************************************/
using System;
using System.IO;
using System.Drawing;
using System.Drawing.Printing;
using System.Windows.Forms;

public class PrintingExample
{
    private Font printFont;   //First error
    private StreamReader streamToPrint;
    static string filePath;

    public PrintingExample()
    {
        Printing();
    }

    // The PrintPage event is raised for each page to be printed.
    private void pd_PrintPage(object sender, PrintPageEventArgs ev)  //Second error
    {
        float linesPerPage = 0;
        float yPos = 0;
        int count = 0;
        float leftMargin = ev.MarginBounds.Left;
        float topMargin = ev.MarginBounds.Top;
        String line = null;

        // Calculate the number of lines per page.
        linesPerPage = ev.MarginBounds.Height /
            printFont.GetHeight(ev.Graphics);

        // Iterate over the file, printing each line.
        while (count < linesPerPage &&
            ((line = streamToPrint.ReadLine()) != null))
        {
            yPos = topMargin + (count * printFont.GetHeight(ev.Graphics));
            ev.Graphics.DrawString(line, printFont, Brushes.Black,
                leftMargin, yPos, new StringFormat());
            count++;
        }

        // If more lines exist, print another page.
        if (line != null)
            ev.HasMorePages = true;
        else
            ev.HasMorePages = false;
    }

    // Print the file.
    public void Printing()
    {
        try
        {
            streamToPrint = new StreamReader(filePath);
            try
            {
                printFont = new Font("Arial", 10);
                PrintDocument pd = new PrintDocument();
                pd.PrintPage += new PrintPageEventHandler(pd_PrintPage);
                // Print the document.
                pd.Print();
            }
            finally
            {
                streamToPrint.Close();
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }

    // This is the main entry point for the application.
    public static void Main(string[] args)
    {
        string sampleName = Environment.GetCommandLineArgs()[0];
        if (args.Length != 1)
        {
            Console.WriteLine("Usage: " + sampleName + " <file path>");
            return;
        }
        filePath = args[0];
        new PrintingExample();
    }
}

标签: c#printingfonts

解决方案


推荐阅读