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& ...
随机推荐
- 增加swap分区
起因:开发人员说tomcat关闭了,然后我排查了下,发现内存耗尽,然后临时用swap分区,以供当前运行的程序使用. 先用free -h查看一下swap的大小 1.添加swap分区 使用dd命令创建/h ...
- luoguP1131
时态同步 ...这道题我也不知道咋\(A\)的. 思路: \(anst\) 距离 \(s\) 的最长距离,\(ansp\) 某一节点到祖先所有边的权值和这些些加过的权值和 先求出到\(s\)距离最长的 ...
- redis安装教程 windows环境
redis开始入坑啦 安装: gayhub地址:https://github.com/MicrosoftArchive/redis/releases 我装的是win7 64系统 选了第二个 在E盘新 ...
- layui报错 "Layui hint: 模块名 xxx 已被占用" 的问题解决方案
由于扩展模块数量众多, 于是我需要将扩展模块分类到二级文件夹中, 我在页面中是这么写的 <script> layui.extend({ courseTask: 'task/courseTa ...
- spring boot jpa 使用update 报错解决办法
在spring boot jpa 中自定义sql,执行update操作报错解决办法: 在@Query(...)上添加 @Modifying@Transactional注解
- python3与mysql交互
1.安装pymysql模块 pip3 install pymysql3 2.pymysql的简单使用: # /usr/bin/env python3 import pymysql class Mysq ...
- Centos安装Consul微服务
一.简介 Consul([ˈkɒnsl],康搜)是注册中心,服务提供者.服务消费者等都要注册到Consul中,这样就可以实现服务提供者.服务消费者的隔离.除了Consul之外,还有Eureka.Zoo ...
- C#通过文件路径获取文件名小技巧
string fullPath = @"\WebSite1\Default.aspx"; string filename = System.IO.Path.GetFileName( ...
- GO语言一行代码实现反向代理
本文,介绍了什么是反向代理,如何用go语言实现反向代理. 至于他的标题, "GO语言一行代码实现反向代理 | Writing a Reverse Proxy in just one line ...
- JetBrains系IDE的设置Pycharm PHPStorm
一.换号边界线 File -> Settings ->Editor -> Code Style -> Right margin (columns) 二.代码自动完成快捷键 ...