【发布时间】:2019-10-28 17:30:18
【问题描述】:
我想静默打印 .docx 文件并能够选择打印机的托盘。
起初我尝试使用Microsoft.Office.Interop.Word 打印 .docx,但单词正在打开...
在我将 .docx 文件转换为图像并使用 ProcessStartInfo 打印后,它向用户显示了一个打印窗口。
ProcessStartInfo info = new ProcessStartInfo(imageFilePath);
info.Verb = "Print";
info.CreateNoWindow = true;
info.WindowStyle = ProcessWindowStyle.Hidden;
Process.Start(info);
我尝试了另一种方法,它静默打印图像,但图像模糊且未正确缩放。
PrinterSettings settings = new PrinterSettings();
string defaultPrinter = settings.PrinterName;
FileInfo fileInfo = new FileInfo(imageFilePath);
PrintDocument pd = new PrintDocument();
pd.DocumentName = fileInfo.Name;
pd.PrintPage += (sender, args) =>
{
Image i = Image.FromFile(imageFilePath);
PrintPageEventArgs arguments = args;
System.Drawing.Rectangle m = new System.Drawing.Rectangle()
{
Y = 0,
X = 0,
Location = new System.Drawing.Point(0, 0),
Height = args.MarginBounds.Height,
Size = args.MarginBounds.Size,
Width = args.MarginBounds.Width
};
if ((double)i.Width / (double)i.Height > (double)m.Width / (double)m.Height)
{
m.Height = (int)((double)i.Height / (double)i.Width * (double)m.Width);
}
else
{
m.Width = (int)((double)i.Width / (double)i.Height * (double)m.Height);
}
args.Graphics.DrawImage(i, m);
};
pd.Print();
那么是否可以静默打印 .docx 并能够选择打印机的托盘?
有没有人遇到过同样的问题。在这方面的任何帮助。提前致谢。
【问题讨论】:
-
一个想法是将 docx 转换为 pdf,然后发送到打印机。也许打印机会更好地管理pdf。这是一个简单的猜测。
-
我尝试将其转换为pdf,但我不知道为什么pdf文档的顶部和左侧有额外的边距。