zoukankan      html  css  js  c++  java
  • (转)C# 打印PDF文件使用第三方DLL

      本文为转载,原文:http://www.cnblogs.com/Yesi/p/5066835.html

         DLL地址:https://freepdf.codeplex.com

          

    下面是该解决方案的详细代码分步解析:

    第一步:添加项目引用。

    安装控件后,创建一个新的项目,找到控件的安装目录,在项目的“解决方案”窗口右击->添加引用,选择和项目.NET Framework版本对应的dll文件进行添加;

    如下图:

                         

    第二步:使用命名空间。

    在该方案中,我使用的命名空间如下:

    1
    2
    3
    using Spire.Pdf;
    using System.Windows.Forms;
    using System.Drawing.Printing;

    第三步:创建一个新的PDF文档,并加载待打印的PDF文件。

    1
    2
    PdfDocument doc = new PdfDocument();
    doc.LoadFromFile("sample.pdf");

    如果需要使用默认打印机打印所有页面,请看第四步。如果需要使用其他打印机并设置打印页面范围,请看第五步。

    第四步:使用默认打印机打印所有页面。

    1
    doc.PrintDocument.Print();

    第五步:选择打印机和设置打印页面范围。 

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    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();
        }

    运行项目,输出的效果图如下(打印机和打印页面范围可以自己选择):

     

     

    全部代码如下:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    using Spire.Pdf;
    using System.Windows.Forms;
    using System.Drawing.Printing;
     
    namespace PrintPDF
    {
        class Program
        {
            static void Main(string[] args)
            {
                PdfDocument doc = new PdfDocument();
                doc.LoadFromFile("sample.pdf");
      
                //选择默认打印机打印所有页面
                //doc.PrintDocument.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)
                {
                    doc.PrintFromPage = dialogPrint.PrinterSettings.FromPage;
                    doc.PrintToPage = dialogPrint.PrinterSettings.ToPage;
                    doc.PrinterName = dialogPrint.PrinterSettings.PrinterName;
     
                    PrintDocument printDoc = doc.PrintDocument;
                    dialogPrint.Document = printDoc;
                    printDoc.Print();
                }
            }
        }
    }
  • 相关阅读:
    fastapi教程进阶
    fastapi快速入门
    Linux yum安装PostgreSQL9.6
    harbor helm仓库使用
    Dockfile文件解析
    K8S概念理解
    转载---Beats:如何使用Filebeat将MySQL日志发送到Elasticsearch
    Elasticsearch中text与keyword的区别
    filebeat知识点
    logstash知识点
  • 原文地址:https://www.cnblogs.com/hhhh2010/p/6083840.html
Copyright © 2011-2022 走看看