插件Spire.PDF帮你高效搞定PDF打印

Spire.PDF介绍

Spire.PDF是一个专业的PDF组件,能够独立地创建、编写、编辑、操作和阅读PDF文件,支持 .NET、Java、WPF和Silverlight。Spire.PDF的PDF API拥有丰富的功能,如安全设置(包括数字签名)、PDF文本/附件/图片提取、PDF文件合并/拆分、元数据更新、章节和段落优化、图形/图像描绘和插入、表格创建和处理、数据导入等等。

本文将介绍以下几种常见方式:

  • 使用默认打印机打印PDF文档

  • 使用虚拟打印机(Microsoft XPS Document Writer)打印PDF文档

  • 指定打印机及PDF文档打印页码范围

  • 静默打印PDF文档

  • 双面打印PDF文档

  • 完整打印过程

使用默认打印机

//加载PDF文档
PdfDocument doc = new PdfDocument();
doc.LoadFromFile("Test.pdf");
//使用默认打印机打印文档所有页面
doc.PrintDocument.Print();

使用虚拟打印机(Microsoft XPS Document Writer)

//加载PDF文档
PdfDocument doc = new PdfDocument();
doc.LoadFromFile("Test.pdf");

//选择Microsoft XPS Document Writer打印机
doc.PrintDocument.PrinterSettings.PrinterName = "Microsoft XPS Document Writer";

//打印PDF文档到XPS格式
doc.PrintDocument.PrinterSettings.PrintToFile = true;
doc.PrintDocument.PrinterSettings.PrintFileName = "PrintToXps.xps";
doc.PrintDocument.Print();

指定打印机及文档打印页码范围

//加载PDF文档
PdfDocument doc = new PdfDocument();
doc.LoadFromFile("Test.pdf");

//设置打印对话框属性
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)
{
    //指定打印机及打印页码范围
    doc.PrintFromPage = dialogPrint.PrinterSettings.FromPage;
    doc.PrintToPage = dialogPrint.PrinterSettings.ToPage;
    doc.PrinterName = dialogPrint.PrinterSettings.PrinterName;
    
    //打印文档
    PrintDocument printDoc = doc.PrintDocument;
    dialogPrint.Document = printDoc;
    printDoc.Print();
}

静默打印

//加载PDF文档
var doc = new PdfDocument();
doc.LoadFromFile("Test.pdf");

//静默打印PDF文档
PrintDocument printDoc = doc.PrintDocument;
printDoc.PrintController = new StandardPrintController(); 
printDoc.Print();

双面打印

//加载PDF文档
PdfDocument doc = new PdfDocument();
doc.LoadFromFile("Test.pdf");

//判断打印机是否支持双面打印
bool isDuplex = doc.PrintDocument.PrinterSettings.CanDuplex;
if (isDuplex)
{
    //如果支持则设置双面打印模式,可选:Default/Simplex/Horizontal/Vertical
    doc.PrintDocument.PrinterSettings.Duplex = Duplex.Default;
    //打印文档
    doc.PrintDocument.Print();
}

完整打印过程

        /// <summary>
        /// 打印接口
        /// </summary>
        //入参:订单号、待打印文件全路径、单双面、颜色、纸张大小、纸张克重、打印范围、打印份数等,后续待补充
        public void Print(string filepath, string frompage, string topage, string copies, string duplex, string color, string papersizes, string print)
        {
            try
            {
                if (print == "")
                {
                    MessageBox.Show("请选择打印机!");
                    return;
                }

                if (filepath == "")
                {
                    MessageBox.Show("请选择打印文件路径!");
                    return;
                }

                PdfDocument doc = new PdfDocument();
                doc.LoadFromFile(filepath);

                doc.PrintSettings.PrinterName = print;

                //设置文档打印页码范围
                doc.PrintSettings.SelectPageRange(Convert.ToInt32(frompage), Convert.ToInt32(topage));
                //打印不连续的页面
                //doc.PrintSettings.SelectSomePages(new int[] { 1, 3, 5, 7 });

                //判断打印机是否支持双面打印
                if (doc.PrintSettings.CanDuplex)
                {
                    //如果支持则设置双面打印模式,可选:Default/Simplex/Horizontal/Vertical
                    switch (duplex)
                    {
                        case "双面打印":
                            doc.PrintSettings.Duplex = Duplex.Default;
                            break;
                        case "双面水平打印":
                            doc.PrintSettings.Duplex = Duplex.Horizontal;
                            break;
                        case "单面打印":
                            doc.PrintSettings.Duplex = Duplex.Simplex;
                            break;
                        case "双面垂直打印":
                            doc.PrintSettings.Duplex = Duplex.Vertical;
                            break;
                        default:
                            doc.PrintSettings.Duplex = Duplex.Simplex;
                            break;
                    }
                }

                if (color == "黑白")
                {
                    //黑白打印PDF文档
                    doc.PrintSettings.Color = false;
                }
                else
                {
                    doc.PrintSettings.Color = true;
                }


                //设置打印份数为1份
                doc.PrintSettings.Copies = Convert.ToInt16(copies);

                PaperSize p = null;
                //遍历打印机纸盒里面的纸张,找到需要的A3
                foreach (PaperSize ps in printDoc.PrinterSettings.PaperSizes)
                {
                    if (ps.PaperName.Equals(papersizes))
                    {
                        p = ps;
                        break;
                    }
                }
                //设置打印的纸张大小为A3
                doc.PrintSettings.PaperSize = p;


                //静默打印PDF文档
                doc.PrintSettings.PrintController = new StandardPrintController();

                doc.PrintSettings.EndPrint += PrintSettings_EndPrint;


                //使用默认打印机打印文档所有页面
                doc.Print();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void PrintSettings_EndPrint(object sender, PrintEventArgs e)
        {
            MessageBox.Show("打印成功!");
        }

支持选择打印机、纸张设置、双面打印、彩色打印、打印份数和打印页码范围等等都可以进行控制!

 

posted @ 2020-11-24 16:32  程序员家园  阅读(2644)  评论(0编辑  收藏  举报