简体   繁体   English

使用C#打印PDF文件和Doc文件

[英]Print PDF file and Doc file using C#

In my application I'm trying to create a function to print existing PDFs or Doc. 在我的应用程序中,我试图创建一个功能来打印现有的PDF或Doc。 How can I do this in C# and provide a mechanism so the user can select a different printer or other properties. 如何在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. 我已经看过PrintDialog,但是不确定要尝试打印哪个文件,如果有的话,b / c输出总是空白页。 Maybe I'm just missing something there. 也许我只是在那儿想念东西。

Any advice, examples or sample code would be great! 任何建议,示例或示例代码都很棒!

The below is my code 下面是我的代码

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()
         {
         }
     }
 }

This has worked in the past: 过去一直有效:

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();

To print to the default printer, replace printto with print , and leave off the Arguments line. 要使用默认打印机进行打印,请将printto替换为print ,然后保留“ Arguments行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM