• C#中调用PDFCreator生成PDF文件


    在C#中如何调用PDFCreator生成PDF文件呢?

    第一步:安装PDFCreator (这个好像是废话)

    第二步:创建C#项目,加入PDFCreator.exe COM引用

    第三步:参见如下代码:

    public override void Process(ITask task, int index)
            {
                if (!File.Exists(Path.Combine(FromLocation, FromFilename)))
                {
                    throw new FileNotFoundException("File: " + Path.Combine(FromLocation, FromFilename) + " does not exists");
                }
     
                Monitor.Enter(lockObject);
     
                clsPDFCreator creator = null;
     
                try
                {
                    creator = new clsPDFCreator();
                    creator.eError += new __clsPDFCreator_eErrorEventHandler(creator_eError);
                    creator.eReady += new __clsPDFCreator_eReadyEventHandler(creator_eReady);
                    String paramters = "/NoProcessingAtStartup";
     
                    if (!creator.cStart())
                    {
                        throw new Exception("Cannot launch PDFCreator. Error: " + Error);
                    }
     
                    var opt = creator.cOptions;
                    opt.UseAutosave = 1;
                    opt.UseAutosaveDirectory = 1;
                    opt.AutosaveDirectory = this.ToLocation;
                    opt.AutosaveFormat = 0;
                    opt.AutosaveFilename = this.ToFilename;
                    creator.cOptions = opt;
     
                    creator.cClearCache();
     
                    creator.cDefaultPrinter = "PDFCreator";
     
                    if (!creator.cIsPrintable(Path.Combine(FromLocation, FromFilename)))
                    {
                        throw new Exception("File: " + Path.Combine(FromLocation, FromFilename) + " is not printable.");
                    }
     
                    creator.cPrintFile(Path.Combine(FromLocation, FromFilename));
                    creator.cPrinterStop = false;
     
                    Ready = false;
                    var duration = new TimeSpan(0, 0, 0, TimeoutInSec);
     
                    DateTime lastCheck = DateTime.Now;
                    DateTime startTime = lastCheck;
                  
                    while (!Ready && ((lastCheck - startTime) < duration))
                    {
                        System.Threading.Thread.Sleep(500);
                        lastCheck = DateTime.Now;
                    }
     
                    creator.cPrinterStop = true;
                    Thread.Sleep(1000);
                    creator.cClose();
     
                    if (!Ready)
                    {
                        throw new Exception("PDF creation failed. This maybe due to timeout.");
                    }
                }
                finally
                {
                    Monitor.Exit(lockObject);
                }
            }
     
            void creator_eReady()
            {
                this.Ready = true;
            }
     
            void creator_eError()
            {
                Error = creator.cError.Description;
            }
    

      代码很好懂,自己很懒,没有把代码重新写成一个Project,直接把Project中的代码贴上了,大家要用的话,还要稍微修改一下。要说的一点是PDFCreator不支持多线程,所以如果程序本身是多线程的话,需要在代码中增加一个锁机制,保证PDF生成的调用是单线程的。还有打印机的名字用的是默认值,没有修改,不过这些问题是难不倒各位看官的。

    转:https://blog.csdn.net/mcai4gl2/article/details/7294625

     private bool PrintIt(int FileTyp, string wordfilepath, string pdfdir, out string pdfnameorerr)
            {
     
                try
                {
                    string fname="";
                    string DefaultPrinter = _PDFCreator.cDefaultPrinter;
                    FileInfo fi;
                    PDFCreator.clsPDFCreatorOptions opt;
     
                    //for (int i = 0; i < filepath.Length; i++)
                    //{
                    fi = new FileInfo(wordfilepath.ToString());
                    if (fi.Name.Length > 0)
                    {
                        if (fi.Name.IndexOf(".") > 1)
                        {
                            fname = fi.Name.Substring(0, fi.Name.IndexOf("."));
                        }
                        else
                        {
                            fname = fi.Name;
                        }
                        
     
                        if (!_PDFCreator.cIsPrintable(fi.FullName))
                        {
                            MessageBox.Show("File '" + fi.FullName + "' is not printable!", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
     
                        }
                        opt = _PDFCreator.cOptions;
                        opt.UseAutosave = 1;
                        opt.UseAutosaveDirectory = 1;
                        opt.AutosaveDirectory = pdfdir;
     
                        opt.AutosaveFormat = FileTyp;                    
     
                        if (FileTyp == 5)
                        {
                            //opt.BitmapResolution = 72;
                        }
                        
                        opt.AutosaveFilename = fname;
                        _PDFCreator.cOptions = opt;
                        _PDFCreator.cClearCache();
                        DefaultPrinter = _PDFCreator.cDefaultPrinter;
                        _PDFCreator.cDefaultPrinter = "PDFCreator";
                        //_PDFCreator.cVisible = false;
                        _PDFCreator.cPrintFile(fi.FullName);
                        _PDFCreator.cPrinterStop = false;
     
                    }
                    ReadyState = false;
                    timer2.Interval = maxTime * 1000;
                    timer2.Enabled = true;
                    while (!ReadyState && timer2.Enabled)
                    {
                        Application.DoEvents();
                    }
                    timer2.Enabled = false;
     
                    _PDFCreator.cPrinterStop = true;
                    _PDFCreator.cDefaultPrinter = DefaultPrinter;
                    //System.Threading.Thread.Sleep(5000);
                    //}
                    pdfnameorerr = fname;
                    return true;
                }
                catch (Exception ex)
                {
                    //IsWordToPdf = false;
                    pdfnameorerr = ex.Message;
                    return false;
                }
            } private bool PrintIt(int FileTyp, string wordfilepath, string pdfdir, out string pdfnameorerr)
            {
     
                try
                {
                    string fname="";
                    string DefaultPrinter = _PDFCreator.cDefaultPrinter;
                    FileInfo fi;
                    PDFCreator.clsPDFCreatorOptions opt;
     
                    //for (int i = 0; i < filepath.Length; i++)
                    //{
                    fi = new FileInfo(wordfilepath.ToString());
                    if (fi.Name.Length > 0)
                    {
                        if (fi.Name.IndexOf(".") > 1)
                        {
                            fname = fi.Name.Substring(0, fi.Name.IndexOf("."));
                        }
                        else
                        {
                            fname = fi.Name;
                        }
                        
     
                        if (!_PDFCreator.cIsPrintable(fi.FullName))
                        {
                            MessageBox.Show("File '" + fi.FullName + "' is not printable!", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
     
                        }
                        opt = _PDFCreator.cOptions;
                        opt.UseAutosave = 1;
                        opt.UseAutosaveDirectory = 1;
                        opt.AutosaveDirectory = pdfdir;
     
                        opt.AutosaveFormat = FileTyp;                    
     
                        if (FileTyp == 5)
                        {
                            //opt.BitmapResolution = 72;
                        }
                        
                        opt.AutosaveFilename = fname;
                        _PDFCreator.cOptions = opt;
                        _PDFCreator.cClearCache();
                        DefaultPrinter = _PDFCreator.cDefaultPrinter;
                        _PDFCreator.cDefaultPrinter = "PDFCreator";
                        //_PDFCreator.cVisible = false;
                        _PDFCreator.cPrintFile(fi.FullName);
                        _PDFCreator.cPrinterStop = false;
     
                    }
                    ReadyState = false;
                    timer2.Interval = maxTime * 1000;
                    timer2.Enabled = true;
                    while (!ReadyState && timer2.Enabled)
                    {
                        Application.DoEvents();
                    }
                    timer2.Enabled = false;
     
                    _PDFCreator.cPrinterStop = true;
                    _PDFCreator.cDefaultPrinter = DefaultPrinter;
                    //System.Threading.Thread.Sleep(5000);
                    //}
                    pdfnameorerr = fname;
                    return true;
                }
                catch (Exception ex)
                {
                    //IsWordToPdf = false;
                    pdfnameorerr = ex.Message;
                    return false;
                }
            }
    

      

  • 相关阅读:
    php4的一个创建xml的类
    ecshop在模板里调用广告位的代码
    文件导出dbf功能实现
    [转] 处事22计、心态24条、伤心50句、礼仪73、学会长大20!
    在线客服 qq,msn,skype, outlook 链接
    解决动网论坛Bug缺少对象: 'Application(...).documentElement.selectSingleNode(...)'
    创建一个SharePoint 样式(Creating a SharePoint Theme)
    Sharepoint Portal Services 2003开发环境
    [译]Hour 7 Teach.Yourself.WPF.in.24.Hours
    远程桌面连接无法最大化的解决办法
  • 原文地址:https://www.cnblogs.com/DoNetCShap/p/14919381.html
Copyright © 2020-2023  润新知