我想使用默认安装的Windows 10打印机“Microsoft Print to PDF”将文件打印到新PDF。
当您选择此打印机作为默认打印机并使用文件上下文菜单并选择打印时,它只会要求保存目录和名称。之后,它立即转换为PDF并保存文件。
只要安装了MS Office,这适用于Word,Excel,PowerPoint文件类型。但也适用于常见的图像类型和普通文本文件。
我想通过提供默认路径来自动执行此操作。
Stackoverflow已经有this related question,但它没有解决我的具体问题,而且相当不完整且无效。
但我想出了这个C#控制台程序,该程序使用PDF打印机在桌面上生成一个新的PDF,其中“Hello World”为字符串
namespace PrintToPdf_Win10
{
using System;
using System.Drawing;
using System.Drawing.Printing;
class Program
{
public static void Main(string[] args)
{
PrintDocument printDoc = new PrintDocument
{
PrinterSettings = new PrinterSettings
{
PrinterName = "Microsoft Print to PDF",
PrintToFile = true,
PrintFileName = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "/test.pdf"
}
};
printDoc.PrintPage += printDoc_PrintPage;
printDoc.Print();
Console.ReadKey(true);
}
static void printDoc_PrintPage(object sender, PrintPageEventArgs e)
{
e.Graphics.DrawString("Hello World", new Font("Arial", 12), Brushes.Black, 50, 50);
}
}
}
如何设置 - 我们说一个Word文件 - 作为我的printDoc
对象的输入?
是否有一种通用的方法来设置printDoc
,只提供一个filePath到我要打印的文件中?或者我是否必须为每个可能的文件类型系列创建自定义函数,例如:
doc, docx, xls, xlsx, xlsm, ppt, pptx
等)png, bmp, jpg
)txt, rtf, ini
)答案 0 :(得分:4)
以下是如何打印图像或文本的简单解决方案(它可以帮助您使用png,bmp,jpg,txt,ini等格式)
private static StreamReader streamToPrint;
static void Main(string[] args)
{
string printFormat;
printFormat = "txt";
try
{
streamToPrint = new StreamReader(@"D:\TestText.txt");
PrintDocument printDoc = new PrintDocument
{
PrinterSettings = new PrinterSettings
{
PrinterName = "Microsoft Print to PDF",
PrintToFile = true,
PrintFileName = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "/test.pdf"
}
};
printDoc.DefaultPageSettings.PaperSize = new System.Drawing.Printing.PaperSize("A4", 210, 290);
printDoc.PrinterSettings.DefaultPageSettings.Landscape = false;
printDoc.PrinterSettings.DefaultPageSettings.Margins.Top = 0;
printDoc.PrinterSettings.DefaultPageSettings.Margins.Left = 0;
switch (printFormat)
{
case "jpg":
printDoc.PrintPage += printDoc_PrintImage;
break;
case "txt":
printDoc.PrintPage += printDoc_PrintText;
break;
default:
break;
}
printDoc.Print();
}
finally
{
streamToPrint.Close();
}
Console.ReadKey(true);
}
static void printDoc_PrintImage(object sender, PrintPageEventArgs e)
{
Image photo = Image.FromFile(@"D:\TestImage.jpg");
Point pPoint = new Point(0, 0);
e.Graphics.DrawImage(photo, pPoint);
}
static void printDoc_PrintText(object sender, PrintPageEventArgs e)
{
Font printFont;
printFont = new Font("Arial", 10);
float linesPerPage = 0;
// Calculate the number of lines per page.
linesPerPage = e.MarginBounds.Height / printFont.GetHeight(e.Graphics);
float yPos = 0;
int count = 0;
float leftMargin = e.MarginBounds.Left;
float topMargin = e.MarginBounds.Top;
string line = null;
while (count < linesPerPage &&
((line = streamToPrint.ReadLine()) != null))
{
yPos = topMargin + (count *
printFont.GetHeight(e.Graphics));
e.Graphics.DrawString(line, printFont, Brushes.Black,
leftMargin, yPos, new StringFormat());
count++;
}
// If more lines exist, print another page.
if (line != null)
e.HasMorePages = true;
else
e.HasMorePages = false;
}
如你所知docx,xlsx就像是zip文件,你可以解压缩并获取内容为xml。所以,如果要打印它们,需要做很多工作