zoukankan      html  css  js  c++  java
  • wpf编写一个简单的PDF转换的程序

    wpf 调用Spire.Pdf将PDF文件转换为其他文件模式

    1. 首先在Nuget里下载该第三方包Spire.Pdf。
    2. 然后可以编写程序
    //这里我调用的是解析成流模式,这是因为我要使用ProgressBar
      public void PdfTrasformOther(string fileName, string storeFileName, FileFormat fileFormat, MainWindow mainWindow)
           {
               this.mainWindow = mainWindow;
               PdfDocument document = new PdfDocument();
               document.LoadFromFile(fileName);
               using (MemoryStream stream = new MemoryStream())
               {
                   document.SaveToStream(stream, fileFormat);
                   document.Close();
                   byte[] data = stream.ToArray();
                   long len = stream.Length;
                   saveFile(storeFileName, mainWindow, data, len);
               }
           }
    //这里我使用了线程池来调用界面控件,因此得使用Dispatcher方法来避免线程问题。
           private static void saveFile(string storeFileName, MainWindow mainWindow, byte[] data, long len)
           {
               using (FileStream file = File.Create(storeFileName))
               {
                   ParmData pdata = new ParmData();
                   long everylen = 0;
                   if (len % 100 == 0)
                   {
                       everylen = len / 100;
                   }
                   else
                   {
                       everylen = (len / 100) + 1;
    
                   }
                   mainWindow.progressSpeed.Myprogress.Dispatcher.Invoke(() =>
                   {
                       mainWindow.progressSpeed.dataText.DataContext = pdata;
                       mainWindow.progressSpeed.percent.Text = "%";
                   });
                   int sendlen = 0;
                   for (long i = 0; i <= 100; i++)
                   {
                       if (data.Length - everylen >= 0)
                       {
                           sendlen = (int)everylen;
                       }
                       else
                       {
                           sendlen = data.Length;
                       }
                       file.Write(data, 0, (int)sendlen);
                       byte[] copyData = new byte[data.Length - sendlen];
                       Array.Copy(data, sendlen, copyData, 0, copyData.Length);
                       data = copyData;
                       mainWindow.progressSpeed.Myprogress.Dispatcher.Invoke(() =>
                       {
                           mainWindow.progressSpeed.Myprogress.Value = i;
                           pdata.ValueText = (int)i;
                       });
                       if (i == 100)
                       {
                           mainWindow.progressSpeed.Myprogress.Dispatcher.Invoke(() =>
                           {
                               mainWindow.progressSpeed.trasformText.Text = "";
                           });
                           SharParm.flage = false;
                       }
                   }
               }
           }
    

    3.在主线程处,使用一个线程池来避免主线程在进度条刷新的时候不能操作其他控件

    private void Btn_tarsform_Click(object sender, RoutedEventArgs e)
            {
                if (txb_FileName.Text == "")
                {
                    MessageBox.Show("请先导入PDF文件");
                    return;
                }
                if (SharParm.flage)
                {
                    MessageBox.Show("正在转换文件,请转换完毕再执行下一个文件转换!");
                    return;
                }
                this.progressSpeed.Myprogress.Value = 0;
                progressSpeed.bindtxt.Text = "0";
                progressSpeed.percent.Text = "%";
                FileFormat fileFormat = (FileFormat)cob_Format.SelectedIndex;
                if (!SharParm.IsSave)
                {
                    MessageBox.Show("暂时不能提供!");
                    SharParm.flage = false;
                    return;
                    //SharParm.flage = true;
                    //string fileName = txb_FileName.Text;
                    //string saveFileName = "";
                    //ThreadPool.QueueUserWorkItem((o) =>
                    //{
                    //    this.progressSpeed.trasformText.Dispatcher.Invoke(() =>
                    //    {
                    //        this.progressSpeed.trasformText.Text = "正在转换中";
                    //    });
                    //    TrasformFile trasform = new TrasformFile();
                    //    trasform.PdfTrasformOther(fileName, saveFileName, fileFormat, this);
                    //});
                }
                else
                {
                    SaveFileDialog saveFile = new SaveFileDialog();
                    saveFile.FileName = name + "." + cob_Format.SelectedValue;
                    if ((bool)saveFile.ShowDialog())
                    {
                        SharParm.flage = true;
                        string fileName = txb_FileName.Text;
                        string saveFileName = saveFile.FileName;
                        ThreadPool.QueueUserWorkItem((o) =>
                        {
                            this.progressSpeed.trasformText.Dispatcher.Invoke(() =>
                            {
                                this.progressSpeed.trasformText.Text = "正在转换中";
                            });
                            TrasformFile trasform = new TrasformFile();
                            trasform.PdfTrasformOther(fileName, saveFileName, fileFormat, this);
                        });
                    }
                }
    
            }
    

    4.结果显示
    enter image description here

    enter image description here

    enter image description here

    5.结果很简单,模式也很多,可以解析成不同的模式并保存到本地文址,对于简单使用已经很方便了。

  • 相关阅读:
    Android安装apk
    Android获取应用程序版本信息
    Handler消息传递机制
    Activity的启动模式
    cocopods的使用
    ios9 的新特性
    静态库的制作详解
    真机调试
    时间差计算(给定两时间,转换为时间差)
    socket 通信机制的实现
  • 原文地址:https://www.cnblogs.com/Jack-S-Wang/p/10790677.html
Copyright © 2011-2022 走看看