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& ...
随机推荐
- mui slider禁止滑动
网上方法: mui('.mui-slider').slider().setStopped(true); 实际使用 mui('.mui-slider').slider().stopped = true; ...
- JDBC url连接字符串错误1
String url="jdbc:mysql://127.0.0.1:3306/northwind?useUnicode=true&characterEncoding=utf-8&a ...
- 范围for循环
1.C++使用如下方法遍历一个容器: #include "stdafx.h" #include<iostream> #include<vector> int ...
- hackerrank杂记
https://www.hackerrank.com/challenges/py-set-discard-remove-pop/forum 知识点: *list:将list中的值取出,取出的数据大小是 ...
- WPF自定义轮播控件
闲得蛋疼做了一个WPF制作轮播动画(随机动画),勉强可以看,写个随笔留个脚印. 效果图:
- react-native自定义TextInput光标颜色
<TextInput defaultValue="Highlight Color is red" selectionColor={'red'} style={styles.s ...
- Spring 下使用Junit4 单元测试
package platform; import java.util.List; import java.util.UUID; import javax.annotation.Resource; im ...
- ES6躬行记(15)——箭头函数和尾调用优化
一.箭头函数 箭头函数(Arrow Function)是ES6提供的一个很实用的新功能,与普通函数相比,不但在语法上更为简洁,而且在使用时也有更多注意点,下面列出了其中的三点: (1)由于不能作为构造 ...
- 分布式存储ceph理论
一.ceph简介 Ceph是一种具有优秀性能,可靠性和可扩展性,统一的分布式文件系统.ceph 的统一体现在可以提供文件系统.块存储和对象存储,分布式体现在可以动态扩展.在国内一些公司的云环境中,通常 ...
- Git ignore文件的用法
这周为了往自己个人代码仓库里囤货,把在公司写的一些东西上传到了自己的GitHub代码仓库,手抖把测试用的日志也一并上传了.上传没多长时间就被运维找上门了,说commit里包含内网相关信息,要求删除.当 ...