2016-11-25 65 views
2

期望的結果

我想用它在默認情況下安裝了Windows 10的打印機「微軟打印到PDF」打印文件到一個新的PDF 。編程提供了一個文件路徑作爲輸入文件「微軟打印到PDF」打印機

當您選擇此打印機作爲默認打印機並在文件上使用上下文菜單並選擇打印時,它只會要求保存目錄和名稱。之後,它立即轉換爲PDF並保存文件。

只要安裝了MS Office,這適用於Word,Excel,PowerPoint文件類型。但也適用於常見的圖像類型和正常的文本文件。

我想通過提供默認路徑來自動執行此操作。

我已經嘗試過

#1已經this related question,但它並沒有解決我的具體問題,是相當不完整和不工作。

但我想出了它使用PDF打印機到我的桌面使用的「Hello World」上產生一個新的PDF作爲字符串這個C#控制檯程序

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

問題

如何設置讓我們說一個Word文件的內容 - 作爲我的printDoc對象的輸入內容?

是否有一種通用的方法來設置printDoc通過提供一個文件路徑到我的文件我想打印?還是我必須爲每一個可能的文件類型的家庭一樣創建一個自定義功能:

  • 辦公室的文件類型(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,xl​​sx就像zip文件,你可以解壓並獲得xml的內容。所以,如果你想打印它們的工作很多

+0

嗨,請告訴我如何在webBrowser控件上打印當前頁面? ... doc.PrintPage + = new PrintPageEventHandler(this.pd_PrintPage); ... 私人無效pd_PrintPage(對象o,PrintPageEventArgs E) { WebBrowser1.Navigate時(「https://stackoverflow.com/questions/40812996/programmatically-provide-a-filepath-as-input-file-換微軟打印到PDF-p「); } 我有空文檔 – user3331122