C#中调用PDFCreator生成PDF文件

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

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

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

第三步:参见如下代码:

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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
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

 

 

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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
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;
           }
       }

  

posted @   jevan  阅读(511)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 通过 API 将Deepseek响应流式内容输出到前端
点击右上角即可分享
微信分享提示