2016-11-25 11 views
2

望ましい結果

を入力ファイルとしてファイルパスを提供。プログラムで私はデフォルトでインストールされているWindows 10プリンタ「MicrosoftはPDFに印刷」を使用して新しいPDFにファイルを印刷したい「PDFに印刷マイクロソフト、」プリンタ

デフォルトプリンターとしてこのプリンターを選択し、ファイルのコンテキストメニューを使用して印刷を選択すると、保存ディレクトリと名前のみを尋ねます。その後すぐにPDFに変換してファイルを保存します。

MS Officeがインストールされている限り、これはWord、Excel、PowerPointのファイルタイプで動作します。しかし、一般的な画像タイプと通常のテキストファイルにも。

デフォルトのパスを提供することでこれを自動化したいと思います。

は、私はすでに

StackOverflowのを試してみました何すでにthis related questionを持っていますが、それは私の特定の問題に対処していないし、むしろ不完全と作業ではありません。

しかし、私は設定するにはどうすればよいの問題

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

文字列としての「Hello World」と私のデスクトップ上の新しいPDFを生成するPDFプリンタを使用して、このC#のコンソールプログラムを思い付い内容 - 私のprintDocオブジェクトの入力として、Wordファイルを言いましょうか?

printDocには、印刷したいファイルにfilePathのみを設定するという一般的な方法がありますか?

  • オフィスのファイルタイプ(doc, docx, xls, xlsx, xlsm, ppt, pptxなど)
  • 画像ファイルタイプ(png, bmp, jpg
  • テキストファイル(txt, rtf, ini

答えて

1
:それとも私のようなそれぞれの可能なファイルタイプの家族のためにカスタム関数を作成する必要があります

画像やテキストを印刷する簡単な方法です(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; 
    } 

docxが分かっているように、xlsxはzipファイルと似ていて、解凍してxmlとしてコンテンツを取得できます。それで、あなたがそれらを印刷したいのであれば、多くの仕事があります。

+0

こんにちは、私はwebBrowserコントロールの現在のページをどのように印刷できますか教えてください。 ... doc.PrintPage + =新しいPrintPageEventHandler(this.pd_PrintPage); ... プライベートボイドpd_PrintPage(オブジェクトo、PrintPageEventArgs E) { webBrowser1.Navigate(「https://stackoverflow.com/questions/40812996/programmatically-provide-a-filepath-as-input-file- for-microsoft-print-to-pdf-p "); } 私は空のドキュメントを持っています – user3331122

関連する問題