使用 C# 打印 PDF 文件和 Doc 文件 [英] Print PDF file and Doc file using C#

查看:137
本文介绍了使用 C# 打印 PDF 文件和 Doc 文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,我尝试创建一个函数来打印现有的 PDF 或 Doc.我如何在 C# 中执行此操作并提供一种机制,以便用户可以选择不同的打印机或其他属性.

In my application I'm trying to create a function to print existing PDFs or Doc. How can I do this in C# and provide a mechanism so the user can select a different printer or other properties.

我查看了 PrintDialog 但不确定它正在尝试打印哪个文件,如果有的话,b/c 输出总是一个空白页.也许我只是在那里遗漏了一些东西.

I've looked at the PrintDialog but not sure what file it is attempting to print, if any, b/c the output is always a blank page. Maybe I'm just missing something there.

任何建议、示例或示例代码都会很棒!

Any advice, examples or sample code would be great!

下面是我的代码

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;


namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
         public Form1()
         {
              InitializeComponent();
         }

         private void button1_Click(object sender, EventArgs e)
         {
              string printPath = 
                   System.Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
              System.IO.StreamReader fileToPrint;
              fileToPrint= new System.IO.StreamReader(printPath + @"\myFile.txt");
              System.Drawing.Font printFont;
              printPDF(e);
              printDocument1.Print();
              fileToPrint.Close();
         }

         private void button2_Click(object sender, EventArgs e)
         {
              //printDoc(e);
         }

         public void printPDF(object sender ,  
                              System.Drawing.Printing.PrintPageEventArgs e))
         {       
              printFont = new System.Drawing.Font("Arial", 10);
              float yPos = 0f;
              int count = 0;
              float leftMargin = e.MarginBounds.Left;
              float topMargin = e.MarginBounds.Top;
              string line = null;
              float linesPerPage = e.MarginBounds.Height /     
                                   printFont.GetHeight(e.Graphics);
              while (count < linesPerPage)
              {
                    line = fileToPrint.ReadLine();
                    if (line == null)
                    {
                         break;
                    }
              yPos = topMargin + count * printFont.GetHeight(e.Graphics);
              e.Graphics.DrawString(line, printFont, Brushes.Black, leftMargin, yPos,
                                  new StringFormat());
              count++;
              }

              if (line != null)
              {
                   e.HasMorePages = true;
              }

              fileToPrint.Close();        
         }

         public void printDoc()
         {
         }
     }
 }

推荐答案

这在过去有效:

using System.Diagnostics.Process;

...

Process process = new Process();

process.StartInfo.FileName = pathToPdfOrDocFile; 
process.UseShellExecute = true;
process.StartInfo.Verb = "printto";
process.StartInfo.Arguments = "\"" + printerName + "\""; 
process.Start();

process.WaitForInputIdle();
process.Kill();

要打印到默认打印机,请将 printto 替换为 print,并去掉 Arguments 行.

To print to the default printer, replace printto with print, and leave off the Arguments line.

这篇关于使用 C# 打印 PDF 文件和 Doc 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆