使用c#windows 10中的“Microsoft Print to PDF”以编程方式打印到PDF

时间:2016-07-02 15:27:33

标签: c# pdf printing windows-10

我正在尝试使用c#中的“Microsoft Print to PDF”打印到pdf。我非常努力地写下了代码。

它正在创建一个空的pdf文件,而不是用我发送到打印机的内容填充它。

有人可以帮助我吗?

try
{
    PrintDocument pd = new PrintDocument();
    ProcessStartInfo info = new ProcessStartInfo("E:\\ba\\Asp.pdf");
    info.Verb = "Print";
    pd.PrinterSettings.PrintToFile = true;
    pd.PrinterSettings.PrinterName = "Microsoft Print to PDF";
    pd.PrinterSettings.PrintToFile = true;
    pd.PrinterSettings.PrintFileName = Path.Combine("e://", "hktespri" + ".pdf");
    pd.Print();
    info.CreateNoWindow = true;
}
catch (Exception ex)
{
    MessageBox.Show(ex.Message);

}

1 个答案:

答案 0 :(得分:0)

您还没有给pdf文档打印什么。 继承我的代码我用它来打印我的面板

 protected void btnPrint_Click(object sender, EventArgs e)
    {
        PrintDialog pd = new PrintDialog();
        PrintDocument pdoc = new PrintDocument();

        pdoc.PrintPage += pdoc_PrintPage;

        if (pd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
        {
            pdoc.Print();
        }
    }

    void pdoc_PrintPage(object sender, PrintPageEventArgs e)
    {
        Bitmap bitmap = new Bitmap(outerPanel.Width, outerPanel.Height);
        outerPanel.DrawToBitmap(bitmap, new System.Drawing.Rectangle(5, 5, outerPanel.Width, outerPanel.Height));
        e.Graphics.DrawImage(bitmap, 5, 5);
        bitmap.Dispose();
    }