c# - 从 .NET 应用程序打印时,符号字体被光栅化
问题描述
我通过打印到设置为打印到文件的 Postscript 打印机从 C# 创建 Postscript 文件。我正在使用 .NET PrintDocument 类和 Graphics.DrawString 来呈现文本。
如果我使用符号或象形文字字体(例如 Symbol 或 WingDings),文本会在 Postscript 文件中被光栅化。如果我使用例如 Arial,那么文本不会被光栅化。我的目标是从我的应用程序中生成一个 Postscript 文件,其中使用符号字体的文本没有被光栅化。
如果我使用记事本中的符号字体打印相同的文本,则文本不会被光栅化,因此乍一看似乎不是打印机驱动程序的限制。
我在做什么错/不同导致光栅化?
- 打印机的字体替换表设置为不替换字体。
- 符号字体在我尝试过的打印机上可用。
- 选择另一个 Postscript 打印机驱动程序没有任何区别。
- 使用 TextRenderer.DrawText 会产生相同的结果。
- 使用其他应用程序(记事本、Word 等)的 Symbol 字体打印不会导致光栅化输出。
#region Usings
using System;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Printing;
using System.IO;
using System.Windows.Forms;
#endregion
public class PrintingExample
{
private string _fontName;
private string _printerName;
private string _textToPrint;
//private bool endPrintFired = false;
// private bool keepGoing = true;
private Font printFont;
public PrintingExample(string fontName, string printerName, string textToPrint)
{
_fontName = fontName;
_printerName = printerName;
_textToPrint = textToPrint;
}
// The PrintPage event is raised for each page to be printed.
private void pd_PrintPage(object sender, PrintPageEventArgs ev)
{
const float fontSize = 14.0f;
var fFamily = new FontFamily(_fontName);
printFont = new Font(fFamily, fontSize, FontStyle.Regular, GraphicsUnit.Point);
var leftMargin = ev.MarginBounds.Left;
var topMargin = ev.MarginBounds.Top;
ev.Graphics.DrawString(_textToPrint, printFont, Brushes.Black, new Point(leftMargin, topMargin), new StringFormat());
// Only printing one page
ev.HasMorePages = false;
}
// Print the file.
public void Printing()
{
try
{
var outputFile = string.Empty;
try
{
outputFile = Path.Combine(@"c:\temp\print\", Path.GetRandomFileName() + ".ps");
var pd = new PrintDocument {PrinterSettings = {
PrinterName = _printerName,
PrintToFile = true,
PrintFileName = outputFile}
};
pd.PrintPage += pd_PrintPage;
PrintController pc = new StandardPrintController();
PrintController printController = new PrintControllerWithStatusDialog(pc);
pd.PrintController = printController;
// Print the document.
pd.Print();
}
finally
{
// For debugging, loads in default associated application
if (File.Exists(outputFile))
Process.Start(outputFile);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
// This is the main entry point for the application.
public static void Main(string[] args)
{
const string fontName = "Symbol";
const string printerName = "<Choose an installed Postscript printer here>";
const string textToPrint = "1234567890\r\nabcdefghijklmnopqrstuvwxyz";
var p = new PrintingExample(fontName, printerName, textToPrint);
p.Printing();
}
}
解决方案
这原来是 Windows 2012R2、10、Server 2016 和 Server 2019 中的一个 bug(server 2008R2 不受影响,这导致我们打开了一个 MS 案例)。只有一些字体受到影响。它已在 2021 年 3 月的 Windows 10、Server 2016 和 Server 2019 操作系统更新中得到解决
推荐阅读
- html - 带有文件扩展名的文本溢出省略号
- laravel - 相同的功能,但我在某些页面上无法读取未定义的属性“access_token”,只有离子角度 laravel
- php - PHP客户端IP无效
- python - 如何处理 RuntimeError:CUDA 错误:在 PyTorch 中遇到非法内存访问?
- tensorflow-lite - 尝试将 Hexagon Delegate 用于 tflite 项目
- swift - SwiftUI MVVM 如何设置从 ViewModel 函数到 View 的回调
- android - 如何获取存储在 android 中的 PDF 文件列表?
- javascript - 如何执行 if 语句 toString() ,如果它彼此相等,则不必在 Dart Flutter 中显示输出(android 模拟器,移动应用程序)
- mysql - 在mysql while循环中分配用户声明的变量会导致null
- java - InputStream 有问题