c# - 从 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();
}
}
解决方案
推荐阅读
- postgresql - 如何将 JSONB 值设置为 null
- python - 如果夹具在 pytest 中失败,则跳过测试套件
- python - 以 mmap 模式从 .npy 文件加载字典
- html - 为什么我的页脚没有从容器继承宽度?
- vue.js - 如何将 bootstrap.min.js 导入 Vue JS
- c++ - isless vs < 或 isgreater vs >
- mysql - 从 Spring JPA 查询与控制台中的原始 SQL 获得不同的结果
- vuejs2 - 如何将 v-slot:activator 和 v-on 与子组件一起使用?
- python - 本地 CSS 文件从 plotly dash 应用程序中删除
- python-3.x - Python:如何以选项居中的方式设置菜单选项间距?