博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
wpf编写一个简单的PDF转换的程序
阅读量:7236 次
发布时间:2019-06-29

本文共 4965 字,大约阅读时间需要 16 分钟。

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

转载于:https://www.cnblogs.com/Jack-S-Wang/p/10790677.html

你可能感兴趣的文章
Hibernate(十五):QBC检索、本地SQL检索和HQL删除
查看>>
BZOJ 1091([SCOI2003]分割多边形-分割直线)
查看>>
<html>
查看>>
淘宝中间的一像素线(手机端)
查看>>
iOS开发 之 不要告诉我你真的懂isEqual与hash!
查看>>
vscode: Visual Studio Code 常用快捷键1
查看>>
多线程(7)多线程中的异常处理
查看>>
(转)基于MVC4+EasyUI的Web开发框架经验总结(12)--利用Jquery处理数据交互的几种方式...
查看>>
java多线程之 ---- 线程死锁
查看>>
APP三种开发模式
查看>>
solr入门之solr的拼写检查功能的应用级别尝试
查看>>
《分水岭:看清中国科技和互联网未来五年的趋势》出版 腾讯科技出品
查看>>
UIButton的图片和文字相对位置调整
查看>>
mark 百度Echarts统计图表
查看>>
Java多线程基础(二)
查看>>
eclipse中javadoc给项目生成api文档
查看>>
IOS UIPickView+sqlite 选择中国全部城市案例
查看>>
Cocos2d-x 3.0的启动流程
查看>>
ES6模板字面量
查看>>
使用SpannableString实现一个load小动画
查看>>