如何在 C# 中使用 PrintDialog 打印文檔 (How to print a document using PrintDialog in C#)


問題描述

如何在 C# 中使用 PrintDialog 打印文檔 (How to print a document using PrintDialog in C#)

這是我的示例代碼。但它打印一個空頁面

            printDocument1.DocumentName = "C:\a.pbf";// PrintDocument printDocument1
            printDialog1.Document = printDocument1;
            printDialog1.AllowPrintToFile = true;
            printDialog1.AllowSelection = true;
            printDialog1.AllowSomePages = true;
            printDialog1.PrintToFile = true;
            if (printDialog1.ShowDialog() == DialogResult.OK)
                printDocument1.Print();

這有什麼問題嗎?請幫幫我


參考解法

方法 1:

You need to handle the PrintPage event to actually provide the contents; MSDN has a full example. The DocumentName is purely something to show to the user ‑ it is not the path of an existing file to magically print.

For printing an existing PDF, maybe look at this question

方法 2:

do this :

public static void PrintToASpecificPrinter()
        {     
                using (PrintDialog printDialog=new PrintDialog ())
                {
                printDialog.AllowSomePages = true;
                printDialog.AllowSelection = true;
                if (printDialog.ShowDialog() == DialogResult.OK)
                {
                    var StartInfo = new ProcessStartInfo();
                    StartInfo.CreateNoWindow = true;
                    StartInfo.UseShellExecute = true;
                    StartInfo.Verb = "printTo";
                    StartInfo.Arguments = "\"" + printDialog.PrinterSettings.PrinterName + "\"";
                    StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
                    StartInfo.FileName = fileName;

                    Process.Start(StartInfo);
                }

                }


        }

(by Thomas AndersonMarc Gravellbigtheo)

參考文件

  1. How to print a document using PrintDialog in C# (CC BY‑SA 3.0/4.0)

#printing #C#






相關問題

是否可以創建網頁打印視圖的 PDF? (Is it possible to create a PDF of a webpage's print view?)

使用 PHP 打印到共享的 windows 打印機 (Linux PHP Server) (Using PHP to print to a shared windows printer (Linux PHP Server))

printf 和自定義類 (printf and custom class)

Firefox 無法訪問同一域上的 iframe 打印 (Firefox can't access iframe print on the same domain)

在python 3中動態打印一行 (Printing a line dynamically in python 3)

用於打印 div 的 JS 函數 - 在 Chrome 中不起作用? (JS function to print div - Doesn't work in Chrome?)

進程 WaitForExit 不等待 (Process WaitForExit not waiting)

你能給我打印屏幕並在javascript或flash中轉換為jpg的功能嗎 (Could you please give me the function of taking print screen and convert in to jpg in javascript or flash)

當我使用轉換時,我的打印輸出看起來不像打印預覽 (My printout doesn't look like the print preview when I use a Transform)

如何在 C# 中使用 PrintDialog 打印文檔 (How to print a document using PrintDialog in C#)

MKMapView 遮擋是否剔除它的註釋? (Does MKMapView occlusion cull it's annotations?)

在網絡環境中從 Brother TD-4100N 打印機檢索打印機狀態 (Retrieving the printer status from the Brother TD-4100N printer in a network environment)







留言討論