我有以下代码将C#中的文本文件打印到打印机上,基于this文章它打印纯文本完美但是当我尝试打印 let campaignData = campaignRes.body.data;
和.docx
文件时打印但将内容转换为某种编码字符。如何修复此问题以打印pdf和doc文件?
.pdf
//
private void btnPrint_Click(object sender, EventArgs e)
{
// Select the desired printer. ps.Duplex = Duplex.Simplex; // This works
pdocFile.PrinterSettings.PrinterName = cboPrinter.Text;
pdocFile.PrinterSettings.Duplex = Duplex.Horizontal;
// Print the checked files.
foreach (string filename in clbFiles.CheckedItems)
{
Console.WriteLine("Printing: " + filename);
// Get the file's name without the path.
FileInfo file_into = new FileInfo(filename);
string short_name = file_into.Name;
// Set the PrintDocument's name for use by the printer queue.
pdocFile.DocumentName = short_name;
// Read the file's contents.
try
{
FileContents = File.ReadAllText(filename).Trim();
}
catch (Exception ex)
{
MessageBox.Show("Error reading file " + filename +
".\n" + ex.Message);
return;
}
// Print.
pdocFile.Print();
}
MessageBox.Show("Spooled " + clbFiles.CheckedItems.Count +
" files for printing.");
}
请参阅下面的图片链接
答案 0 :(得分:0)
上面的示例采用二进制文件格式,并尝试使用纯文本方法打印它,这将无法正常工作。你有几个方法可以解决这个问题。