PDF转换成Word,ppt转换成word
pdf与word我没找到直接转换的方式,不过可以用间接方式嘛!
pdf ==》picture ==》word!ppt转word的原理也是先把ppt转成图片,再把图片插入word!
先准备好几个程序集:fontbox-0.1.0-dev.dll,IKVM.GNU.Classpath.dll,IKVM.Runtime.dll,Interop.Microsoft.Office.Core.dll,PDFBox-0.7.3.dll,Spire.Doc.dll,Spire.License.dll,Spire.Pdf.dll
一个小工具:gswin32.exe
pdf转word主要代码:
/// <summary>
/// 将PDF文档转换成图片
/// </summary>
/// <param name="pdfFile">PDF文档物理路径</param>
/// <param name="imgPath">转换成的图片文件的存放物理路径</param>
/// <param name="isDeletePDF">转换成图片文件以后是否删除原PDF文档</param>
/// <returns>返回转换成的图片文件物理路径的集合</returns>
public List<string> PdfToImages(string pdfFile, string imgPath, bool isDeletePDF)
{
List<string> imgList = new List<string>(); PDDocument doc = PDDocument.load(pdfFile); int pageCount = doc.getDocumentCatalog().getAllPages().size();//计算pdf文档的总页数 string pdfFileName = Path.GetFileName("");
int index = pdfFileName.LastIndexOf('.');
if (index != -)
pdfFileName = pdfFileName.Substring(, index); string imgFile = Path.Combine(imgPath, pdfFileName);//转换成的图片文件 if (pageCount == ) return null;
if (pageCount == )
{
imgFile += ".jpg";
imgList.Add(imgFile);
if (File.Exists(imgFile)) File.Delete(imgFile);
}
else
{
for (int i = ; i < pageCount; i++)
{
string _imgFile = imgFile + (i + ).ToString() + ".jpg";
imgList.Add(_imgFile);
if (File.Exists(_imgFile)) File.Delete(_imgFile);
}
imgFile += "%d.jpg";
}
//此调用方法有命令框出现
// System.Diagnostics.Process.Start(System.Configuration.ConfigurationManager.AppSettings["GhostScriptView"] +"\\gswin32.exe", System.Configuration.ConfigurationManager.AppSettings["GhostScriptArguments"] + @" -sOutputFile=" + imgFile + " " + pdfFile); ProcessStartInfo startinfo = new ProcessStartInfo();
startinfo.CreateNoWindow = true;
startinfo.FileName = System.Configuration.ConfigurationManager.AppSettings["GhostScriptView"] + "\\gswin32.exe";
startinfo.Arguments = System.Configuration.ConfigurationManager.AppSettings["GhostScriptArguments"] + @" -sOutputFile=" + imgFile + " " + pdfFile;
startinfo.WindowStyle = ProcessWindowStyle.Hidden;
startinfo.CreateNoWindow = true;
startinfo.UseShellExecute = true;
Process p = new Process();
p.StartInfo = startinfo;
p.Start();
if (p.WaitForExit(**))
{
Thread.Sleep();
p.Close();
} if (isDeletePDF)
{
File.Delete(pdfFile);
} return imgList;
}
/// <summary>
/// 图片插入word
/// </summary>
/// <param name="ImagesURL">图片路径集合</param>
public void PicToWord(List<string> ImagesURL)
{
//Thread.Sleep(1000);
//Process[] thepro = Process.GetProcessesByName("gswin32");
//if (thepro.Length > 0)
////如果进程曾在或者不止一个
//{
// //逐个结束
// for (int i = 0; i < thepro.Length; i++)
// {
// //如果还没有结束就关闭他
// if (!thepro[i].CloseMainWindow()) thepro[i].Kill();
// }
//} for (int i = ; i < ImagesURL.Count; i++)
{
if (File.Exists(ImagesURL[i].ToString() + ".jpg"))
{
File.Delete(ImagesURL[i].ToString() + ".jpg");
}
if (File.Exists(ImagesURL[i].ToString()))
{
File.Copy(ImagesURL[i].ToString(), ImagesURL[i].ToString() + ".jpg");
}
}
//ThreadPool.QueueUserWorkItem( //使用线程池
//(P_temp) => //使用lambda表达式
//{
word.Application newapp = new word.Application(); //创建Word应用程序对象
object nothing = System.Reflection.Missing.Value;
word.Document newdoc = newapp.Documents.Add(ref nothing, ref nothing, ref nothing, ref nothing);//生成一个word文档
if (newdoc != null)
{
try
{
for (int i = (ImagesURL.Count - ); i >= ; i--)
{
string a = ImagesURL[i].ToString() + ".jpg";
newdoc.InlineShapes.AddPicture(a);
File.Delete(ImagesURL[i].ToString());
File.Delete(ImagesURL[i].ToString() + ".jpg");
} }
catch (Exception ex)
{
this.TextBox1.Text = ex.Message;
} // newdoc.Close();
newdoc.SaveAs("F:\\Test.docx");
newapp.Quit();
}
// });
}
ppt转word主要代码:
/// <summary>
/// PPT转Word
/// </summary>
/// <param name="pptPath">ppt文件路径</param>
/// <param name="imgPath">图片路径</param>
/// <param name="WordPath">存放word文档路径</param>
/// <returns></returns>
public bool PPTToWord(string pptPath,string imgPath,string WordPath)
{
bool bo=true;
List<string> image = new List<string>();
try
{
var app = new PowerPoint.Application();
var ppt = app.Presentations.Open(pptPath, Core.MsoTriState.msoFalse, Core.MsoTriState.msoFalse, Core.MsoTriState.msoFalse);
var index = ;
var fileName = System.IO.Path.GetFileNameWithoutExtension(pptPath);
foreach (PowerPoint.Slide slid in ppt.Slides)
{
++index;
//设置图片大小
slid.Export(imgPath + string.Format("page{0}.png", index.ToString()), "png", , );
image.Add(imgPath + string.Format("page{0}.png", index.ToString()));
}
//释放资源
ppt.Close();
app.Quit();
GC.Collect();
}
catch (Exception ex)
{
bo = false ;
} Word.Application newapp = new Word.Application(); //创建Word应用程序对象
object nothing = System.Reflection.Missing.Value;
Word.Document newdoc = newapp.Documents.Add(ref nothing, ref nothing, ref nothing, ref nothing);//生成一个word文档
if (newdoc != null)
{
try
{
for (int i = (image.Count - ); i >= ; i--)
{
string a = image[i].ToString();
newdoc.InlineShapes.AddPicture(a);
File.Delete(image[i].ToString());
}
}
catch (Exception ex)
{
bo = false;
}
newdoc.SaveAs(WordPath);
newapp.Quit();
}
return bo;
}
这就ok了!
但是!当小女子放到服务器(win2008)上发现,天呐!bug!bug!bug!pdf能转成图片,但是在插入图片的时候报错,未将对象引用到实例!再看看ppt转word。Oh my god!ppt不能open!
奇怪,在我的电脑上没问题呀,这是怎么回事呢!仔细看看规律。pdf与office不是同一回事,出问题的都是office!抱着试试的心态,给office赋予权限去!
comexp.msc -32,打开32位的组件服务。"组件服务"->"计算机"->"我的电脑"->"DCOM配置"。找到office,给每一个office配置都赋予权限。
1、右键==》属性==》安全==》启动和激活权限==》自定义==》编辑==》添加Everyone==》本地启动、本地激活。
2、右键==》属性==》安全==》访问权限==》自定义==》编辑==》添加Everyone==》本地访问。
3、右键==》属性==》安全==》配置权限==》自定义==》编辑==》添加Everyone==》完全控制。
4、右键==》属性==》标识==》交互使用户。
看看我们的程序,这个时候已经完全成功了!!!!
PDF转换成Word,ppt转换成word的更多相关文章
- word ppt excel文档转换成pdf
1.把word文档转换成pdf (1).添加引用 using Microsoft.Office.Interop.Word; 添加引用 (2).转换方法 /// <summary> /// ...
- C#.net word excel powerpoint (ppt) 转换成 pdf 文件
using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using Sy ...
- Java通过openOffice实现word,excel,ppt转成pdf实现在线预览
Java通过openOffice实现word,excel,ppt转成pdf实现在线预览 一.OpenOffice 1.1 下载地址 1.2 JodConverter 1.3 新建实体类PDFDemo ...
- 如何将WORD表格转换成EXCEL表格
WORD和EXCEL都可以制作表格,但WORD表格与EXCEL表格之间有着很明显的差距,所以在办公中经常会需要将WORD转换成EXCEL,今天小编就教大家一招将WORD表格转换成EXCEL表格. 操作 ...
- 用Python将word文件转换成html(转)
用Python将word文件转换成html 序 最近公司一个客户大大购买了一堆医疗健康方面的科普文章,希望能放到我们正在开发的健康档案管理软件上.客户大大说,要智能推送!要掌握节奏!要深度学习!要 ...
- python如何转换word格式、读取word内容、转成html
# python如何转换word格式.读取word内容.转成html? import docx from win32com import client as wc # 首先将doc转换成docx wo ...
- c# 将PPT转换成HTML
这只是一个小程序,就是将ppt转换成html,方法很多,为了以后备用,在此记录一下,也和大家分享 源码如下: using System; using System.Collections.Generi ...
- 杂项-PPT:如何把幻灯片ppt转换成视频
ylbtech-杂项-PPT:如何把幻灯片ppt转换成视频 1.返回顶部 2.返回顶部 3.返回顶部 4.返回顶部 5.返回顶部 1. https://jingyan.baidu.co ...
- java 将word转为PDF (100%与word软件转换一样)
jdk环境:jdk_8.0.1310.11_64 (64位) 1.引入pom文件 <!-- word转pdf(依赖windows本地的wps) --> <dependency& ...
随机推荐
- springMVC上传
1.页面 2.开始上传按钮对应的JS 3.添加文件按钮的方法,下图中1是从fast中取到的文件名称,2是文件图片的路径 下面就是后台的一些类.方法等 4.bean层,生成的get和set方法我就不写了 ...
- Javascrip动态添加样式,Dom操作,获取自定义属性
var layer=document.querySelector('.layer') 添加样式: 添加单个样式: layer.style.display="block" 添加多个样 ...
- Vue.js学习笔记(代码)
##v-cloak v-text v-html v-bind v-on的使用 <!DOCTYPE html> <html> <head> < ...
- TopN案例
准备三份数据 t1 2067 t2 2055 t3 2055 t4 1200 t5 2367 t6 255 t7 2555 t8 12100 t9 20647 t10 245 t11 205 t12 ...
- swoole框架基本总结
框架-Swoole扩展-Swoole文档中心 http://wiki.swoole.com/wiki/page/p-framework.html swoole有两个部分. 一个是PHP扩展,用C开发的 ...
- Django-model
Model:数据库操作 创建数据库的表: django不能自动创建数据库,但能创建表 在web的models里定义生成表 在project的settings里设置app定义和数据库信息 步骤: 1.创 ...
- vapor 生成xcode project 产生的错误解决方式
运行vapor xcode时报错: Could not generate Xcode project: error: terminated(72): xcrun --sdk macosx --find ...
- pyqt5与QML开发小结
遇见的坑 qt 5.11 与 qt 5.12 中Qquick的差异还是蛮大的,由开发环境:Pyqt5.11 + Qt5.12 部署到 Pyqt5.11 + Qt5.11时遇到以下问题: 1.当一个It ...
- nginx代理tcp协议连接mysql
阅读目录 一.mariadb安装及配置 1.1 在192.168.182.155安装mariadb 1.2 配置MariaDB的字符集 1.3 添加用户,设置权限 1.4 防火墙设置 二.nginx ...
- CentOs下手动升级node版本
查找对应的nodejs包,具体参考https://nodejs.org/download/release/ 切换到安装node的位置 此处为/usr/local/lib/nodejs 不存在可以建立 ...