Browse Source

合并gif合并为PDF效果差修复

liyijie 1 năm trước cách đây
mục cha
commit
848032f249

+ 64 - 0
PDF Office/Helper/PictureConverter.cs

@@ -1,9 +1,11 @@
 using System;
 using System.Collections.Generic;
 using System.Drawing;
+using System.IO;
 using System.Linq;
 using System.Text;
 using System.Threading.Tasks;
+using System.Windows.Media.Imaging;
 
 namespace PDF_Master.Helper
 {
@@ -130,5 +132,67 @@ namespace PDF_Master.Helper
             }
         }
 
+        public static byte[] GetImageByteFromPath(string ImagePath, string fileExt, out double width, out double height)
+        {
+            try
+            {
+                #region 跟图章一样的转换方法。
+                if (ImagePath != null && ImagePath.Length > 0 && File.Exists(ImagePath))
+                {
+                    FileStream fileData = File.OpenRead(ImagePath);
+                    BitmapFrame frame = null;
+                    width = 0;
+                    height = 0;
+
+                    if (fileExt.Contains(".jpg") || fileExt.Contains(".jpeg"))
+                    {
+                        JpegBitmapDecoder decoder = (JpegBitmapDecoder)JpegBitmapDecoder.Create(fileData, BitmapCreateOptions.None, BitmapCacheOption.Default);
+                        frame = decoder.Frames[0];
+                        width = frame.Width;
+                        height = frame.Height;
+                    }
+                    else if (fileExt.Contains(".png"))
+                    {
+                        PngBitmapDecoder decoder = (PngBitmapDecoder)PngBitmapDecoder.Create(fileData, BitmapCreateOptions.None, BitmapCacheOption.Default);
+                        frame = decoder.Frames[0];
+                        width = frame.Width;
+                        height = frame.Height;
+                    }
+                    else if (fileExt.Contains(".bmp"))
+                    {
+                        BmpBitmapDecoder decoder = (BmpBitmapDecoder)BmpBitmapDecoder.Create(fileData, BitmapCreateOptions.None, BitmapCacheOption.Default);
+                        frame = decoder.Frames[0];
+                        width = frame.Width;
+                        height = frame.Height;
+                    }
+                    else//默认采用png的解码方式
+                    {
+                        BitmapDecoder decoder = BitmapDecoder.Create(fileData, BitmapCreateOptions.None, BitmapCacheOption.Default);
+                        frame = decoder.Frames[0];
+                        width = frame.Width;
+                        height = frame.Height;
+                    }
+
+                    if (frame != null)
+                    {
+                        var ImageArray = new byte[frame.PixelWidth * frame.PixelHeight * 4];
+                        frame.CopyPixels(ImageArray, frame.PixelWidth * 4, 0);
+                        return ImageArray;
+                    }
+                    return null;
+                }
+                width = 0;
+                height = 0;
+                return null;
+                #endregion
+            }
+            catch (Exception ex)
+            {
+                width = 0;
+                height = 0;
+                return null;
+            }
+        }
+
     }
 }

+ 91 - 67
PDF Office/ViewModels/Dialog/ToolsDialogs/MergeDialogViewModel.cs

@@ -2,6 +2,7 @@
 using ComPDFKit.PDFPage;
 using ComPDFKitViewer.PdfViewer;
 using Dropbox.Api.TeamLog;
+using Microsoft.Office.Interop.Word;
 using Microsoft.Win32;
 using PDF_Master.CustomControl;
 using PDF_Master.EventAggregators;
@@ -19,6 +20,7 @@ using System.Collections.Generic;
 using System.Collections.ObjectModel;
 using System.IO;
 using System.Linq;
+using System.Reflection;
 using System.Text;
 using System.Threading.Tasks;
 using System.Windows;
@@ -67,7 +69,7 @@ namespace PDF_Master.ViewModels.Dialog.ToolsDialogs
         public void OnDialogOpened(IDialogParameters parameters)
         {
             CPDFViewer pdfViewer = null;
-            if (parameters.TryGetValue<CPDFViewer>(ParameterNames.PDFViewer, out pdfViewer)&&pdfViewer!=null&&pdfViewer.Document!=null)
+            if (parameters.TryGetValue<CPDFViewer>(ParameterNames.PDFViewer, out pdfViewer) && pdfViewer != null && pdfViewer.Document != null)
             {
                 VerifyPasswordResult condition = SecurityHelper.VerifyPasswordByPasswordKind(pdfViewer.Document, EnumPasswordKind.StatusPermissionsPassword, dialogs);
                 if (condition.IsDiscryptied)
@@ -83,12 +85,12 @@ namespace PDF_Master.ViewModels.Dialog.ToolsDialogs
                     if (parameters.TryGetValue<string>(ParameterNames.Unicode, out unicode))
                     {
                         this.eventAggregator.GetEvent<ShowTipEvent>().Publish(new ShowTipEventArgs() { enumTipKind = EnumTipKind.StatusNone, Unicode = unicode });
-                    } 
+                    }
                 }
                 currentLoadedPassword = pdfViewer.Document.Password;
             }
 
-            if (pdfViewer != null&&pdfViewer.Document!=null)
+            if (pdfViewer != null && pdfViewer.Document != null)
             {
                 CurrentFilePath = pdfViewer.Document.FilePath;
 
@@ -219,8 +221,8 @@ namespace PDF_Master.ViewModels.Dialog.ToolsDialogs
             {
                 return null;
             }
-            DirectoryInfo tempfolder = new DirectoryInfo(openFile.SelectedPath);
-            
+            DirectoryInfo tempfolder = new DirectoryInfo(openFile.SelectedPath);
+
             FileInfo[] fileInfos = tempfolder.GetFiles();
             List<string> list = new List<string>();
             foreach (FileInfo item in fileInfos)
@@ -228,7 +230,7 @@ namespace PDF_Master.ViewModels.Dialog.ToolsDialogs
                 list.Add(item.FullName);
             }
 
-            DirectoryInfo[] directoryInfos= tempfolder.GetDirectories();
+            DirectoryInfo[] directoryInfos = tempfolder.GetDirectories();
             foreach (DirectoryInfo fileInfo in directoryInfos)
             {
                 FileInfo[] fileinfo = fileInfo.GetFiles();
@@ -259,27 +261,49 @@ namespace PDF_Master.ViewModels.Dialog.ToolsDialogs
             for (int i = 0; i < MergeObjectlist.Count; i++)
             {
                 //图片
-                if (Path.GetExtension(MergeObjectlist[i].FilePath).Trim().ToLower() != ".pdf")
-                {
-                    BitmapSource frame = MergeObjectlist[i].DocThumbnail;
-                    byte[] imageData = new byte[frame.PixelWidth * frame.PixelHeight * 4];
-                    if (frame.Format != PixelFormats.Bgra32)
-                    {
-                        FormatConvertedBitmap covert = new FormatConvertedBitmap(frame, PixelFormats.Bgra32, frame.Palette, 0);
-                        covert.CopyPixels(imageData, frame.PixelWidth * 4, 0);
-                    }
-                    else
-                    {
-                        frame.CopyPixels(imageData, frame.PixelWidth * 4, 0);
-                    }
-                    frame.CopyPixels(imageData, frame.PixelWidth * 4, 0);
-                    result = SaveDoc.InsertPage(0, frame.PixelWidth, frame.PixelHeight, imageData, CPDFDocumentImageMode.CPDFDocumentImageModeScaleToFill);
-                    if (!result)
-                    {
-                        SaveDoc.Release();
-                        return;
-                    }
-                    continue;
+                if (Path.GetExtension(MergeObjectlist[i].FilePath).Trim().ToLower() != ".pdf")
+                {
+                    if (Path.GetExtension(MergeObjectlist[i].FilePath).Trim().ToLower() == ".gif")
+                    {//GIF下面方法产生虚影改成与图片转PDF一致
+                        System.Drawing.Image img = System.Drawing.Image.FromFile(MergeObjectlist[i].FilePath);
+                        string tempFileName = Path.GetTempPath() + "pngtemp.jpg";
+                        if (!PictureConverter.SaveJpeg(MergeObjectlist[i].FilePath, tempFileName))
+                        {
+                            MessageBoxEx.Show("图片格式有问题");
+                        }
+                        result = SaveDoc.InsertPage(0, img.Width, img.Height, tempFileName);
+                        try { if (File.Exists(tempFileName)) File.Delete(tempFileName); }
+                        catch
+                        { }
+                            if (!result)
+                        {
+                            SaveDoc.Release();
+                            return;
+                        }
+                        continue;
+                    }
+                    else
+                    {
+                        BitmapSource frame = MergeObjectlist[i].DocThumbnail;
+                        byte[] imageData = new byte[frame.PixelWidth * frame.PixelHeight * 4];
+                        if (frame.Format != PixelFormats.Bgra32)
+                        {
+                            FormatConvertedBitmap covert = new FormatConvertedBitmap(frame, PixelFormats.Bgra32, frame.Palette, 0);
+                            covert.CopyPixels(imageData, frame.PixelWidth * 4, 0);
+                        }
+                        else
+                        {
+                            frame.CopyPixels(imageData, frame.PixelWidth * 4, 0);
+                        }
+                        frame.CopyPixels(imageData, frame.PixelWidth * 4, 0);
+                        result = SaveDoc.InsertPage(0, frame.PixelWidth, frame.PixelHeight, imageData, CPDFDocumentImageMode.CPDFDocumentImageModeScaleToFill);
+                        if (!result)
+                        {
+                            SaveDoc.Release();
+                            return;
+                        }
+                        continue;
+                    }
                 }
                 else
                 {
@@ -506,13 +530,13 @@ namespace PDF_Master.ViewModels.Dialog.ToolsDialogs
                     ///解锁或取消->
                     ///
 
-                    if(CurrentFilePath == doc.FilePath)
+                    if (CurrentFilePath == doc.FilePath)
                     {
 
-                        if ((!(!doc.IsLocked&&(SecurityHelper.CheckHaveAllPermissions(doc)))) && 
-                            (!(!string.IsNullOrEmpty(currentLoadedPassword) && 
-                            doc.UnlockWithPassword(currentLoadedPassword) && 
-                            (doc.CheckOwnerPassword(currentLoadedPassword)||SecurityHelper.CheckHaveAllPermissions(doc)))))
+                        if ((!(!doc.IsLocked && (SecurityHelper.CheckHaveAllPermissions(doc)))) &&
+                            (!(!string.IsNullOrEmpty(currentLoadedPassword) &&
+                            doc.UnlockWithPassword(currentLoadedPassword) &&
+                            (doc.CheckOwnerPassword(currentLoadedPassword) || SecurityHelper.CheckHaveAllPermissions(doc)))))
                         {
                             doc.Release();
                             continue;
@@ -531,10 +555,10 @@ namespace PDF_Master.ViewModels.Dialog.ToolsDialogs
                         {
                             if (condition.Password != null)
                             {
-                                mergeObject.Password = condition.Password;
-                               if(doc.UnlockWithPassword(condition.Password)&& doc.CheckOwnerPassword(condition.Password))
-                                {
-                                    
+                                mergeObject.Password = condition.Password;
+                                if (doc.UnlockWithPassword(condition.Password) && doc.CheckOwnerPassword(condition.Password))
+                                {
+
                                 }
                             }
                         }
@@ -543,37 +567,37 @@ namespace PDF_Master.ViewModels.Dialog.ToolsDialogs
                             doc.Release();
                             continue;
                         }
-                    }
-                
-                mergeObject.DocName = doc.FileName;
-                mergeObject.DocPageCount = doc.PageCount.ToString() + " " + App.MainPageLoader.GetString("Merge_ItemPages");
-                if (doc.PageCount > 1) 
-                { 
-                    mergeObject.IsEvenPageIsEnabled = true; 
-                }
-                else 
-                { 
-                    mergeObject.IsEvenPageIsEnabled=false;
+                    }
+
+                    mergeObject.DocName = doc.FileName;
+                    mergeObject.DocPageCount = doc.PageCount.ToString() + " " + App.MainPageLoader.GetString("Merge_ItemPages");
+                    if (doc.PageCount > 1)
+                    {
+                        mergeObject.IsEvenPageIsEnabled = true;
+                    }
+                    else
+                    {
+                        mergeObject.IsEvenPageIsEnabled = false;
+                    }
+                    mergeObject.SDKPageCount = doc.PageCount;
+                    mergeObject.DocSize = CommonHelper.GetFileSize(mergeObject.FilePath);
+
+                    //获取第一页缩略图
+                    CPDFPage page = doc.PageAtIndex(0);
+                    Size size = doc.GetPageSize(0);
+
+                    byte[] bmpData = new byte[(int)(size.Width * size.Height * 4)];
+                    WriteableBitmap WirteBitmap = new WriteableBitmap((int)size.Width, (int)size.Height, 96, 96, PixelFormats.Bgra32, null);
+                    page.RenderPageBitmap(0, 0, (int)size.Width, (int)size.Height, 0xFFFFFFFF, bmpData, 1);
+                    WirteBitmap.WritePixels(new Int32Rect(0, 0, (int)size.Width, (int)size.Height), bmpData, WirteBitmap.BackBufferStride, 0);
+                    WirteBitmap.Freeze();
+                    mergeObject.DocThumbnail = WirteBitmap;
+
+                    doc.Release();
                 }
-                mergeObject.SDKPageCount = doc.PageCount;
-                mergeObject.DocSize = CommonHelper.GetFileSize(mergeObject.FilePath);
-
-                //获取第一页缩略图
-                CPDFPage page = doc.PageAtIndex(0);
-                Size size = doc.GetPageSize(0);
-
-                byte[] bmpData = new byte[(int)(size.Width * size.Height * 4)];
-                WriteableBitmap WirteBitmap = new WriteableBitmap((int)size.Width, (int)size.Height, 96, 96, PixelFormats.Bgra32, null);
-                page.RenderPageBitmap(0, 0, (int)size.Width, (int)size.Height, 0xFFFFFFFF, bmpData, 1);
-                WirteBitmap.WritePixels(new Int32Rect(0, 0, (int)size.Width, (int)size.Height), bmpData, WirteBitmap.BackBufferStride, 0);
-                WirteBitmap.Freeze();
-                mergeObject.DocThumbnail = WirteBitmap;
-
-                doc.Release();
+                MergeObjectlist.Add(mergeObject);
+                UpDataMergeObjectIndex();
             }
-            MergeObjectlist.Add(mergeObject);
-            UpDataMergeObjectIndex();
-        }
 
             if (showDialog)
             {
@@ -582,8 +606,8 @@ namespace PDF_Master.ViewModels.Dialog.ToolsDialogs
                 if (alertsMessage.result == ContentResult.Ok)
                 {
                 }
-            }
-}
+            }
+        }
         #endregion
     }
 }