nix*_*xda 5 c# pdf-generation windows-10
我想使用默认安装的 Windows 10 打印机“Microsoft Print to PDF”将文件打印为新的 PDF。
当您选择这台打印机作为默认打印机并在文件上使用上下文菜单并选择Print 时,它只要求保存目录和名称。之后,它会立即转换为 PDF 并保存文件。
只要安装了 MS Office,它就适用于 Word、Excel、PowerPoint 文件类型。但也适用于常见的图像类型和普通的文本文件。
我想通过提供默认路径来自动执行此操作。
Stackoverflow 已经有这个相关的问题,但它没有解决我的具体问题,而且相当不完整且无法正常工作。
但是我想出了这个 C# 控制台程序,它使用 PDF 打印机在我的桌面上以“Hello World”为字符串生成新的 PDF
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);
}
}
}
Run Code Online (Sandbox Code Playgroud)
如何将内容(比如说 Word 文件)设置为printDoc对象的输入?
是否有一种通用的方法可以printDoc通过仅提供我要打印的文件的文件路径来设置?或者我是否必须为每个可能的文件类型系列创建一个自定义函数,例如:
doc, docx, xls, xlsx, xlsm, ppt, pptx等)png, bmp, jpg)txt, rtf, ini)这是如何打印图像或文本的简单解决方案(它可以帮助您处理 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;
}
Run Code Online (Sandbox Code Playgroud)
如您所知 docx,xlsx 就像 zip 文件,您可以解压缩并以 xml 格式获取内容。所以,如果你想打印它们,需要做很多工作