大家好,这是我的代码,我尝试使用Open File Dialog获取文件并打印文件 但它打印一个空页:( :(
private void tsmprint_Click(object sender, EventArgs e)
{
try
{
if (openFileDialog1.ShowDialog()==DialogResult.OK)
{
PrintDocument Pd = new PrintDocument();
Pd.DocumentName = openFileDialog1.FileName;
printDialog1.Document = Pd;
if (printDialog1.ShowDialog()==DialogResult.OK)
{
Pd.Print();
}
}
}
catch (Exception)
{
}
}
答案 0 :(得分:1)
最简单的方法是使用外部库,使用以下msdn example,您可以使用默认打印机或任何其他网络连接的打印机打印PDF文件,以及选择要打印的页面:
PdfDocument doc = new PdfDocument();
doc.LoadFromFile(FilePathandFileName);
//Use the default printer to print all the pages
//doc.PrintDocument.Print();
//Set the printer and select the pages you want to print
PrintDialog dialogPrint = new PrintDialog();
dialogPrint.AllowPrintToFile = true;
dialogPrint.AllowSomePages = true;
dialogPrint.PrinterSettings.MinimumPage = 1;
dialogPrint.PrinterSettings.MaximumPage = doc.Pages.Count;
dialogPrint.PrinterSettings.FromPage = 1;
dialogPrint.PrinterSettings.ToPage = doc.Pages.Count;
if (dialogPrint.ShowDialog() == DialogResult.OK)
{
//Set the pagenumber which you choose as the start page to print
doc.PrintFromPage = dialogPrint.PrinterSettings.FromPage;
//Set the pagenumber which you choose as the final page to print
doc.PrintToPage = dialogPrint.PrinterSettings.ToPage;
//Set the name of the printer which is to print the PDF
doc.PrinterName = dialogPrint.PrinterSettings.PrinterName;
PrintDocument printDoc = doc.PrintDocument;
dialogPrint.Document = printDoc;
printDoc.Print();
}
答案 1 :(得分:-1)
PrintDocument不会自行打印PDF。试试this other SO post,它解释了如何使用与PDF文件和“打印”动词相关的任何应用程序。