Quellcode durchsuchen

打印优化,新增清除临时文件

tags/录制修改前
zhangxueyang vor 4 Jahren
Ursprung
Commit
7d2e7ba7ad

+ 2
- 2
XHWK.WKTool/App.config Datei anzeigen

@@ -33,8 +33,8 @@
33 33
     <!--上传每片大小 Mb-->
34 34
     <add key="UploadSliceLen" value="1" />
35 35
     <!--版本号-->
36
-    <add key="VersionCode" value="20" />
37
-    <add key="VersionName" value="1.1.9" />
36
+    <add key="VersionCode" value="21" />
37
+    <add key="VersionName" value="1.2.0" />
38 38
     <add key="ClientSettingsProvider.ServiceUri" value="" />
39 39
   </appSettings>
40 40
   <system.web>

+ 10
- 1
XHWK.WKTool/App.cs Datei anzeigen

@@ -239,7 +239,16 @@ namespace XHWK.WKTool
239 239
                 //}
240 240
                 #endregion
241 241
 
242
-
242
+                try
243
+                {
244
+                    if(Directory.Exists(AppDomain.CurrentDomain.BaseDirectory+"temp"))//清除临时文件
245
+                    {
246
+                        Directory.Delete(AppDomain.CurrentDomain.BaseDirectory + "temp",true);
247
+                    }
248
+                }
249
+                catch (Exception ex)
250
+                {
251
+                }
243 252
 
244 253
                 //如果本地文件为空,弹出弹窗 输密匙
245 254
                 //本地文件不为空, 解析

+ 169
- 1
XHWK.WKTool/PrintWindow.xaml.cs Datei anzeigen

@@ -3,6 +3,8 @@
3 3
 using System;
4 4
 using System.Collections.Generic;
5 5
 using System.Data;
6
+using System.Drawing;
7
+using System.IO;
6 8
 using System.Threading;
7 9
 using System.Windows;
8 10
 using System.Windows.Input;
@@ -54,8 +56,12 @@ namespace XHWK.WKTool
54 56
                 cmbClass.SelectedIndex = 0;
55 57
             }
56 58
         }
57
-        public void Initialize(string _imgPath)
59
+        double wit = 0;
60
+        double hei = 0;
61
+        public void Initialize(string _imgPath, double _wit,double _hei)
58 62
         {
63
+            wit = _wit;
64
+            hei = _hei;
59 65
             if (!string.IsNullOrWhiteSpace(_imgPath))
60 66
             {
61 67
                 imgPri.Source = new BitmapImage(new Uri(_imgPath));
@@ -136,6 +142,111 @@ namespace XHWK.WKTool
136 142
         /// </summary>
137 143
         void StartPrint(object obj)
138 144
         {
145
+          
146
+                try
147
+            {
148
+                iTextSharp.text.Document document = new iTextSharp.text.Document(iTextSharp.text.PageSize.A4, 0, 0, 0, 0);
149
+                //iTextSharp.text.Rectangle page = new iTextSharp.text.Rectangle(iTextSharp.text.PageSize.A4., 250f);//cs
150
+                ////设置纸张横向
151
+                //document.SetPageSize(iTextSharp.text.PageSize.A4.Rotate());
152
+                int ipdf = 102;
153
+                string imgPath = FileToolsCommon.GetFileAbsolutePath("temp/");
154
+                string tempImgPath = imgPath;
155
+                FileToolsCommon.CreateDirectory(imgPath);
156
+                imgPath += "101.pdf";
157
+                while(File.Exists(imgPath))
158
+                {
159
+                    imgPath = tempImgPath + ipdf.ToString() + ".pdf";
160
+                    ipdf++;
161
+                }
162
+                iTextSharp.text.pdf.PdfWriter.GetInstance(document, new FileStream(imgPath, FileMode.Create, FileAccess.ReadWrite));
163
+                document.Open();
164
+                iTextSharp.text.Image image;
165
+                for (int i = 0; i < APP.PageDrawList.Count; i++)
166
+                {
167
+                    long ii = Timestamp();
168
+                    string directoryPath = AppDomain.CurrentDomain.BaseDirectory + "Temp\\";
169
+                    string filePathOutPut = Path.Combine(directoryPath, string.Format("print{0}{1}.jpg", ii, i));
170
+                    RectangleF rectangleFs = new RectangleF();
171
+                    MergerImg("", filePathOutPut, rectangleFs, out string errmsg);
172
+                    image = iTextSharp.text.Image.GetInstance(filePathOutPut);
173
+                    if (string.IsNullOrEmpty(APP.PageDrawList[i].PageImagePath))//没有图片
174
+                    {
175
+
176
+                    }
177
+                    else if (APP.PageDrawList[i].ImgDocumentation == true && !APP.PageDrawList[i].Type.Equals("ppt"))
178
+                    {
179
+                        image = iTextSharp.text.Image.GetInstance(APP.PageDrawList[i].PageImagePath);
180
+                    }
181
+                    else
182
+                    {
183
+                        RectangleF rectangleF = new RectangleF(0,0,0,0) ;
184
+                        Dispatcher.Invoke(() =>
185
+                        {
186
+                             rectangleF = new RectangleF
187
+                            {
188
+                                Width = (float)APP.PageDrawList[i].ImageSizes.CenterX,
189
+                                Height = (float)APP.PageDrawList[i].ImageSizes.CenterY,
190
+                                X = (float)APP.PageDrawList[i].ImageLocation.X,
191
+                                Y = (float)APP.PageDrawList[i].ImageLocation.Y
192
+                            };
193
+                        });
194
+                   
195
+
196
+
197
+                        string msgs = string.Empty;
198
+
199
+               
200
+                            bool isImg = MergerImg(APP.PageDrawList[i].PageImagePath, filePathOutPut, rectangleF, out msgs);
201
+                            if (isImg)
202
+                            {
203
+                                image = iTextSharp.text.Image.GetInstance(filePathOutPut);
204
+                            }
205
+                            else
206
+                            {
207
+                                image = iTextSharp.text.Image.GetInstance(APP.PageDrawList[i].PageImagePath);
208
+                            }
209
+                   
210
+                     
211
+                    }
212
+                    if (image.Height > iTextSharp.text.PageSize.A4.Height)
213
+                    {
214
+                        image.ScaleToFit(iTextSharp.text.PageSize.A4.Width, iTextSharp.text.PageSize.A4.Height);
215
+                    }
216
+                    else if (image.Width > iTextSharp.text.PageSize.A4.Width)
217
+                    {
218
+                        image.ScaleToFit(iTextSharp.text.PageSize.A4.Width, iTextSharp.text.PageSize.A4.Height);
219
+                    }
220
+                    image.Alignment = iTextSharp.text.Image.ALIGN_MIDDLE;
221
+                    document.NewPage();
222
+                    document.Add(image);
223
+                    //iTextSharp.text.Chunk c1 = new iTextSharp.text.Chunk("Hello World");
224
+                    //iTextSharp.text.Phrase p1 = new iTextSharp.text.Phrase();
225
+                    //p1.Leading = 150;      //行间距
226
+                    //document.Add(p1);
227
+                }
228
+                Console.WriteLine("转换成功!");
229
+                document.Close();
230
+            }
231
+            catch (Exception ex)
232
+            {
233
+
234
+
235
+
236
+                Dispatcher.Invoke(() =>
237
+                {
238
+                    btnClose.IsEnabled = true;
239
+                    btnPrint.IsEnabled = true;
240
+                    times.Stop();
241
+                    tip_outer.Visibility = Visibility.Collapsed;
242
+                    MessageWindow.Show(ex.Message);
243
+                    return;
244
+                });
245
+                LogHelper.WriteErrLog("打印转换PDF失败,原因:" + ex.Message,ex);
246
+            }
247
+
248
+
249
+
139 250
             List<string> objStr = (List<string>)obj;
140 251
             string SourcePath= objStr[0];
141 252
             int PrinterNum=int.Parse(objStr[1]);
@@ -183,7 +294,64 @@ namespace XHWK.WKTool
183 294
                 });
184 295
             }
185 296
         }
297
+        /// <summary>
298
+        /// 返回一个时间戳到毫秒
299
+        /// </summary>
300
+        /// <returns></returns>
301
+        public static long Timestamp()
302
+        {
303
+            TimeSpan ts = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0);
304
+            long timestr = Convert.ToInt64(ts.TotalMilliseconds);
305
+            return timestr;
306
+        }
307
+        /// <summary>
308
+        /// 生成图片
309
+        /// </summary>
310
+        /// <param name="_path">图片地址</param>
311
+        /// <param name="_saveimg">保存位置</param>
312
+        /// <param name="_rectangle">图片位置</param>
313
+        /// <param name="errmsg">错误消息</param>
314
+        /// <returns></returns>
315
+        private bool MergerImg(string _path, string _saveimg, RectangleF _rectangle, out string errmsg)
316
+        {
317
+            errmsg = null;
318
+            try
319
+            {
320
+                Bitmap bitmap = null;
186 321
 
322
+                //创建要显示的图片对象,根据参数的个数设置宽度
323
+                Bitmap backgroudImg = new Bitmap((int)/*gridM.ActualWidth*/wit, (int)/*gridM.ActualHeight*/hei);
324
+                Graphics g = Graphics.FromImage(backgroudImg);
325
+                //清除画布,背景设置为白色
326
+                g.Clear(System.Drawing.Color.White);
327
+
328
+                if (!string.IsNullOrWhiteSpace(_path))
329
+                {
330
+                    bitmap = ImageHelper.ReadBitmapFile(_path);
331
+                    g.DrawImage(bitmap, _rectangle);
332
+                }
333
+
334
+
335
+                backgroudImg.Save(_saveimg);
336
+
337
+                g.Dispose();
338
+                backgroudImg.Dispose();
339
+                if (bitmap != null)
340
+                {
341
+                    bitmap.Dispose();
342
+                }
343
+
344
+                GC.Collect();
345
+                return true;
346
+            }
347
+            catch (Exception ex)
348
+            {
349
+                errmsg = ex.Message;
350
+                LogHelper.WriteErrLog("【截图合成】(MergerImg)图片合成失败:" + ex.Message, ex);
351
+                return false;
352
+            }
353
+
354
+        }
187 355
         /// <summary>
188 356
         /// 打印-开始
189 357
         /// </summary>

+ 2
- 2
XHWK.WKTool/Properties/AssemblyInfo.cs Datei anzeigen

@@ -49,5 +49,5 @@ using System.Windows;
49 49
 //可以指定所有这些值,也可以使用“生成号”和“修订号”的默认值
50 50
 //通过使用 "*",如下所示:
51 51
 // [assembly: AssemblyVersion("1.0.*")]
52
-[assembly: AssemblyVersion("1.1.9.0")]
53
-[assembly: AssemblyFileVersion("1.1.9.0")]
52
+[assembly: AssemblyVersion("1.2.0.0")]
53
+[assembly: AssemblyFileVersion("1.2.0.0")]

+ 67
- 70
XHWK.WKTool/XHMicroLessonSystemWindow.xaml.cs Datei anzeigen

@@ -3384,80 +3384,77 @@ namespace XHWK.WKTool
3384 3384
                 }
3385 3385
 
3386 3386
 
3387
-                iTextSharp.text.Document document = new iTextSharp.text.Document(iTextSharp.text.PageSize.A4, 0, 0, 0, 0);
3388
-                //iTextSharp.text.Rectangle page = new iTextSharp.text.Rectangle(iTextSharp.text.PageSize.A4., 250f);//cs
3389
-                ////设置纸张横向
3390
-                //document.SetPageSize(iTextSharp.text.PageSize.A4.Rotate());
3391
-
3392
-                string imgPath = FileToolsCommon.GetFileAbsolutePath("temp/");
3393
-                FileToolsCommon.CreateDirectory(imgPath);
3394
-                imgPath += "101.pdf";
3395
-                iTextSharp.text.pdf.PdfWriter.GetInstance(document, new FileStream(imgPath, FileMode.Create, FileAccess.ReadWrite));
3396
-                document.Open();
3397
-                iTextSharp.text.Image image;
3398
-                for (int i = 0; i < APP.PageDrawList.Count; i++)
3399
-                {
3400
-                    long ii = Timestamp();
3401
-                    string directoryPath = AppDomain.CurrentDomain.BaseDirectory + "Temp\\";
3402
-                    string filePathOutPut = Path.Combine(directoryPath, string.Format("print{0}{1}.jpg", ii, i));
3403
-                    RectangleF rectangleFs = new RectangleF();
3404
-                    MergerImg("", filePathOutPut, rectangleFs, out string errmsg);
3405
-                    image = iTextSharp.text.Image.GetInstance(filePathOutPut);
3406
-                    if (string.IsNullOrEmpty(APP.PageDrawList[i].PageImagePath))//没有图片
3407
-                    {
3408
-
3409
-                    }
3410
-                    else if (APP.PageDrawList[i].ImgDocumentation == true&& !APP.PageDrawList[i].Type.Equals("ppt"))
3411
-                    {
3412
-                        image = iTextSharp.text.Image.GetInstance(APP.PageDrawList[i].PageImagePath);
3413
-                    }
3414
-                    else
3415
-                    {
3416
-                        RectangleF rectangleF = new RectangleF
3417
-                        {
3418
-                            Width = (float)APP.PageDrawList[i].ImageSizes.CenterX,
3419
-                            Height = (float)APP.PageDrawList[i].ImageSizes.CenterY,
3420
-                            X = (float)APP.PageDrawList[i].ImageLocation.X,
3421
-                            Y = (float)APP.PageDrawList[i].ImageLocation.Y
3422
-                        };
3423
-
3424
-
3425
-                        string msg = string.Empty;
3426
-                        bool isImg = MergerImg(APP.PageDrawList[i].PageImagePath, filePathOutPut, rectangleF, out msg);
3427
-                        if (isImg)
3428
-                        {
3429
-                            image = iTextSharp.text.Image.GetInstance(filePathOutPut);
3430
-                        }
3431
-                        else
3432
-                        {
3433
-                            image = iTextSharp.text.Image.GetInstance(APP.PageDrawList[i].PageImagePath);
3434
-                        }
3435
-                    }
3436
-
3387
+                //iTextSharp.text.Document document = new iTextSharp.text.Document(iTextSharp.text.PageSize.A4, 0, 0, 0, 0);
3388
+                ////iTextSharp.text.Rectangle page = new iTextSharp.text.Rectangle(iTextSharp.text.PageSize.A4., 250f);//cs
3389
+                //////设置纸张横向
3390
+                ////document.SetPageSize(iTextSharp.text.PageSize.A4.Rotate());
3391
+
3392
+                //string imgPath = FileToolsCommon.GetFileAbsolutePath("temp/");
3393
+                //FileToolsCommon.CreateDirectory(imgPath);
3394
+                //imgPath += "101.pdf";
3395
+                //iTextSharp.text.pdf.PdfWriter.GetInstance(document, new FileStream(imgPath, FileMode.Create, FileAccess.ReadWrite));
3396
+                //document.Open();
3397
+                //iTextSharp.text.Image image;
3398
+                //for (int i = 0; i < APP.PageDrawList.Count; i++)
3399
+                //{
3400
+                //    long ii = Timestamp();
3401
+                //    string directoryPath = AppDomain.CurrentDomain.BaseDirectory + "Temp\\";
3402
+                //    string filePathOutPut = Path.Combine(directoryPath, string.Format("print{0}{1}.jpg", ii, i));
3403
+                //    RectangleF rectangleFs = new RectangleF();
3404
+                //    MergerImg("", filePathOutPut, rectangleFs, out string errmsg);
3405
+                //    image = iTextSharp.text.Image.GetInstance(filePathOutPut);
3406
+                //    if (string.IsNullOrEmpty(APP.PageDrawList[i].PageImagePath))//没有图片
3407
+                //    {
3437 3408
 
3409
+                //    }
3410
+                //    else if (APP.PageDrawList[i].ImgDocumentation == true&& !APP.PageDrawList[i].Type.Equals("ppt"))
3411
+                //    {
3412
+                //        image = iTextSharp.text.Image.GetInstance(APP.PageDrawList[i].PageImagePath);
3413
+                //    }
3414
+                //    else
3415
+                //    {
3416
+                //        RectangleF rectangleF = new RectangleF
3417
+                //        {
3418
+                //            Width = (float)APP.PageDrawList[i].ImageSizes.CenterX,
3419
+                //            Height = (float)APP.PageDrawList[i].ImageSizes.CenterY,
3420
+                //            X = (float)APP.PageDrawList[i].ImageLocation.X,
3421
+                //            Y = (float)APP.PageDrawList[i].ImageLocation.Y
3422
+                //        };
3438 3423
 
3439 3424
 
3440
-                    if (image.Height > iTextSharp.text.PageSize.A4.Height)
3441
-                    {
3442
-                        image.ScaleToFit(iTextSharp.text.PageSize.A4.Width, iTextSharp.text.PageSize.A4.Height);
3443
-                    }
3444
-                    else if (image.Width > iTextSharp.text.PageSize.A4.Width)
3445
-                    {
3446
-                        image.ScaleToFit(iTextSharp.text.PageSize.A4.Width, iTextSharp.text.PageSize.A4.Height);
3447
-                    }
3448
-                    image.Alignment = iTextSharp.text.Image.ALIGN_MIDDLE;
3449
-                    document.NewPage();
3450
-                    document.Add(image);
3451
-                    //iTextSharp.text.Chunk c1 = new iTextSharp.text.Chunk("Hello World");
3452
-                    //iTextSharp.text.Phrase p1 = new iTextSharp.text.Phrase();
3453
-                    //p1.Leading = 150;      //行间距
3454
-                    //document.Add(p1);
3455
-                }
3456
-                Console.WriteLine("转换成功!");
3457
-                document.Close();
3425
+                //        string msg = string.Empty;
3426
+                //        bool isImg = MergerImg(APP.PageDrawList[i].PageImagePath, filePathOutPut, rectangleF, out msg);
3427
+                //        if (isImg)
3428
+                //        {
3429
+                //            image = iTextSharp.text.Image.GetInstance(filePathOutPut);
3430
+                //        }
3431
+                //        else
3432
+                //        {
3433
+                //            image = iTextSharp.text.Image.GetInstance(APP.PageDrawList[i].PageImagePath);
3434
+                //        }
3435
+                //    }
3436
+                //    if (image.Height > iTextSharp.text.PageSize.A4.Height)
3437
+                //    {
3438
+                //        image.ScaleToFit(iTextSharp.text.PageSize.A4.Width, iTextSharp.text.PageSize.A4.Height);
3439
+                //    }
3440
+                //    else if (image.Width > iTextSharp.text.PageSize.A4.Width)
3441
+                //    {
3442
+                //        image.ScaleToFit(iTextSharp.text.PageSize.A4.Width, iTextSharp.text.PageSize.A4.Height);
3443
+                //    }
3444
+                //    image.Alignment = iTextSharp.text.Image.ALIGN_MIDDLE;
3445
+                //    document.NewPage();
3446
+                //    document.Add(image);
3447
+                //    //iTextSharp.text.Chunk c1 = new iTextSharp.text.Chunk("Hello World");
3448
+                //    //iTextSharp.text.Phrase p1 = new iTextSharp.text.Phrase();
3449
+                //    //p1.Leading = 150;      //行间距
3450
+                //    //document.Add(p1);
3451
+                //}
3452
+                //Console.WriteLine("转换成功!");
3453
+                //document.Close();
3458 3454
             }
3459 3455
             catch (Exception ex)
3460 3456
             {
3457
+                MessageWindow.Show(ex.Message);
3461 3458
                 Console.WriteLine("转换失败,原因:" + ex.Message);
3462 3459
             }
3463 3460
             //document.Close();
@@ -3472,11 +3469,11 @@ namespace XHWK.WKTool
3472 3469
             }
3473 3470
             if (APP.PageDrawList.Count > 0 && !string.IsNullOrWhiteSpace(APP.PageDrawList[0].PageImagePath))
3474 3471
             {
3475
-                APP.W_PrintWindow.Initialize(APP.PageDrawList[0].PageImagePath);
3472
+                APP.W_PrintWindow.Initialize(APP.PageDrawList[0].PageImagePath, gridM.ActualWidth, gridM.ActualHeight);
3476 3473
             }
3477 3474
             else
3478 3475
             {
3479
-                APP.W_PrintWindow.Initialize("");
3476
+                APP.W_PrintWindow.Initialize("", gridM.ActualWidth,gridActHeight);
3480 3477
             }
3481 3478
             APP.W_PrintWindow.ShowDialog();
3482 3479
             //int pr = 1;

Laden…
Abbrechen
Speichern