/// <summary>
/// 将word文档转换成PDF格式
/// </summary>
/// <param name="sourcePath"></param>
/// <param name="targetPath"></param>
/// <returns></returns>
public static bool ConvertWord2Pdf(string sourcePath, string targetPath)
{
bool result;
Word.WdExportFormat exportFormat= Word.WdExportFormat.wdExportFormatPDF;
object paramMissing = Type.Missing;
Word.Application wordApplication = new Word.Application();
Word.Document wordDocument = null;
try
{
object paramSourceDocPath = sourcePath;
string paramExportFilePath = targetPath;
Word.WdExportFormat paramExportFormat = exportFormat;
Word.WdExportOptimizeFor paramExportOptimizeFor =
Word.WdExportOptimizeFor.wdExportOptimizeForPrint;
Word.WdExportRange paramExportRange = Word.WdExportRange.wdExportAllDocument;
int paramStartPage = ;
int paramEndPage = ;
Word.WdExportItem paramExportItem = Word.WdExportItem.wdExportDocumentContent;
Word.WdExportCreateBookmarks paramCreateBookmarks =
Word.WdExportCreateBookmarks.wdExportCreateWordBookmarks; wordDocument = wordApplication.Documents.Open(
ref paramSourceDocPath, ref paramMissing, ref paramMissing,
ref paramMissing, ref paramMissing, ref paramMissing,
ref paramMissing, ref paramMissing, ref paramMissing,
ref paramMissing, ref paramMissing, ref paramMissing,
ref paramMissing, ref paramMissing, ref paramMissing,
ref paramMissing);
if (wordDocument != null)
wordDocument.ExportAsFixedFormat(paramExportFilePath,
paramExportFormat, false,
paramExportOptimizeFor, paramExportRange, paramStartPage,
paramEndPage, paramExportItem, true,
true, paramCreateBookmarks, true,
true, false,
ref paramMissing);
result = true;
}
finally
{
if (wordDocument != null)
{
wordDocument.Close(ref paramMissing, ref paramMissing, ref paramMissing);
wordDocument = null;
}
if (wordApplication != null)
{
wordApplication.Quit(ref paramMissing, ref paramMissing, ref paramMissing);
wordApplication = null;
}
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
GC.WaitForPendingFinalizers();
}
return result;
}
/// <summary>
/// 将excel文档转换成PDF格式
/// </summary>
/// <param name="sourcePath"></param>
/// <param name="targetPath"></param>
/// <returns></returns>
public static bool ConvertExcel2Pdf(string sourcePath, string targetPath)
{
bool result;
object missing = Type.Missing;
Excel.XlFixedFormatType targetType= Excel.XlFixedFormatType.xlTypePDF;
Excel.Application application = null;
Excel.Workbook workBook = null;
try
{
application = new Excel.Application();
object target = targetPath;
workBook = application.Workbooks.Open(sourcePath, missing, missing, missing, missing, missing,
missing, missing, missing, missing, missing, missing, missing, missing, missing);
workBook.ExportAsFixedFormat(targetType, target, Excel.XlFixedFormatQuality.xlQualityStandard, true, false, missing, missing, missing, missing);
result = true;
}
catch
{
result = false;
}
finally
{
if (workBook != null)
{
workBook.Close(true, missing, missing);
workBook = null;
}
if (application != null)
{
application.Quit();
application = null;
}
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
GC.WaitForPendingFinalizers();
}
return result;
}
/// <summary>
/// 将ppt文档转换成PDF格式
/// </summary>
/// <param name="sourcePath"></param>
/// <param name="targetPath"></param>
/// <returns></returns>
public static bool ConvertPowerPoint2Pdf(string sourcePath, string targetPath)
{
bool result;
PowerPoint.PpSaveAsFileType targetFileType= PowerPoint.PpSaveAsFileType.ppSaveAsPDF;
PowerPoint.Application application = null;
PowerPoint.Presentation persentation = null;
try
{
application = new PowerPoint.Application();
persentation = application.Presentations.Open(sourcePath, MsoTriState.msoTrue, MsoTriState.msoFalse, MsoTriState.msoFalse);
persentation.SaveAs(targetPath, targetFileType, MsoTriState.msoTrue);
result = true;
}
catch
{
result = false;
}
finally
{
if (persentation != null)
{
persentation.Close();
persentation = null;
}
if (application != null)
{
application.Quit();
application = null;
}
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
GC.WaitForPendingFinalizers();
}
return result;
} /// <summary>
/// 将Txt转换为PDF
/// </summary>
/// <param name="sourcePath"></param>
/// <param name="targetPath"></param>
/// <returns></returns>
public static bool ConvertText2Pdf(string sourcePath, string targetPath)
{
var text = FileHelper.ReadTextFile(sourcePath);
Document document = new Document(PageSize.A4);
try
{
//step 2:创建一个writer用于监听Document以及通过PDF-stream指向一个文件
PdfWriter.GetInstance(document, new FileStream(targetPath, FileMode.Create));
// step 3: 打开document
document.Open();
var f = GetFont();
// step 4: 添加一段话到document中
document.Add(new Paragraph(text, f));
}
catch (Exception ex)
{
return false;
}
finally
{
if (document.IsOpen())
// step 5: 关闭document
document.Close();
}
return true;
}
private static Font GetFont()
{
var fontPath = (string) ConfigurationManager.AppSettings["FontPath"];
if (string.IsNullOrEmpty(fontPath))//没有指定字体就用楷体
{
var fontName = "楷体";
if (!FontFactory.IsRegistered(fontName))
{
fontPath = Environment.GetFolderPath(Environment.SpecialFolder.Windows) + @"\Fonts\simkai.ttf";
FontFactory.Register(fontPath);
}
return FontFactory.GetFont(fontName, BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
}
BaseFont bfChinese = BaseFont.CreateFont(fontPath,BaseFont.IDENTITY_H,BaseFont.NOT_EMBEDDED);
Font fontChinese = new Font(bfChinese, 16f, Font.NORMAL);
return fontChinese;
} public static bool ConvertHtml2Pdf(string text, string pdfPath)
{
Document document = new Document(PageSize.A4);
try
{
PdfWriter.GetInstance(document, new FileStream(pdfPath, FileMode.Create));
document.Open(); var fontName = "楷体";
if (!FontFactory.IsRegistered(fontName))
{
var fontPath = Environment.GetFolderPath(Environment.SpecialFolder.Windows) + @"\Fonts\simkai.ttf";
FontFactory.Register(fontPath);
}
var elements = iTextSharp.tool.xml.XMLWorkerHelper.ParseToElementList(text, @"body {
font-size: 16px;
color: #F00;
font-family: 楷体;
}");
//iTextSharp.text.
foreach (var element in elements)
{
document.Add(element);
}
}
catch (DocumentException de)
{
Console.Error.WriteLine(de.Message);
}
catch (IOException ioe)
{
Console.Error.WriteLine(ioe.Message);
}
document.Close();
return true;
}

from: https://www.cnblogs.com/studyzy/p/5338398.html

word,excel,ppt,txt转换为 PDF的更多相关文章

  1. java 如何将 word,excel,ppt如何转pdf --openoffice (1)

    承上启下,可折叠 上一篇说的是:服务器是windows server时,用jacob将msoffice(指的是word,excel,ppt)转换成pdf. 若被部署项目的服务器是centOS等linu ...

  2. windows环境下 php 将office文件(word/excel/ppt)转化为pdf(转)

    将office文件转化为pdf的方法有 1.利用openoffice提供的服务 (比较简单,但是转化的效果不太好) 2.使用office提供的服务 (注:这在windows服务器上,并且服务器上面安装 ...

  3. php 将office文件(word/excel/ppt)转化为pdf(windows和linux只要安装对应组件应该就行)

    一.配置环境 (1)配置php.ini 添加:extension=php_com_dotnet.dll com.allow_dcom = true  // 去掉号,改为true 重启环境 (2) 安装 ...

  4. PDF/WORD/EXCEL/PPT 文档在线阅读

    查资料看了2种解决方法: 1.通过办公软件dll转换,用flans去看 2.通过Aspose转换成pdf格式,在用js前台读pdf(我用的pdf.js) 今天我解决的就是WORD/EXCEL/PPT ...

  5. java 如何将 word,excel,ppt如何转pdf--jacob

    问题:java 如果将 word,excel,ppt如何转pdf 我个人的观点:windows server下用 jacob; linux server下 用openoffice.   PS:1.本文 ...

  6. 在线文档转换API word,excel,ppt等在线文件转pdf、png

    在线文档转换API提供word,excel,ppt等在线文件转pdf.png等,文档:https://www.juhe.cn/docs/api/id/259 接口地址:http://v.juhe.cn ...

  7. word/excel/ppt 2 PDF

    PHP 实现 word/excel/ppt 转换为 PDF 一般最常见的就是利用OpenOffice来转换,来看看实现的核心代码: class PDFConverter { private $com; ...

  8. Atitit.office word  excel  ppt pdf 的web在线预览方案与html转换方案 attilax 总结

    Atitit.office word  excel  ppt pdf 的web在线预览方案与html转换方案 attilax 总结 1. office word  excel pdf 的web预览要求 ...

  9. 微信小程序云开发-云存储-下载并打开文件文件(word/excel/ppt/pdf)

    一.wxml文件 1.写文本框,用来获取文件链接. 2.按钮,点击下载文件 <!-- 下载文件(word/excel/ppt/pdf等) --> <view class=" ...

随机推荐

  1. 在Java中,关于.Class()与.getCalss与.getClass().getName()的区别

    ** * Java反射 *所谓反射,可以理解为在运行时期获取对象类型信息的操作. *传统的编程方法要求程序员在编译阶段决定使用的类型,但是在反射的帮助下, *编程人员可以动态获取这些信息,从而编写更加 ...

  2. 大数据环境完全分布式搭建 hadoop2.4.1

    (如果想要安装视频及相关软件可以博私聊我 qq 731487514) hadoop2.0已经发布了稳定版本了,增加了很多特性,比如HDFS HA.YARN等.最新的hadoop-2.4.1又增加了YA ...

  3. 双11线上压测netty内存泄露

    最近线上压测,机器学习模型第一次应用到线上经历双11大促.JSF微服务有报错 LEAK: ByteBuf.release() was not called before it's garbage-co ...

  4. 网络基础配置--usg系统升级

    1.usg2000系统升级 1.1.TFTP设置 这里用到一个工具:3CDeamon, 是在由3Com开发类别 Web Development Freeware 软件,是一个简易服务器工具,含TFTP ...

  5. BZOJ.4825.[AHOI/HNOI2017]单旋(线段树)

    BZOJ LOJ 洛谷 这题不难啊,我怎么就那么傻,拿随便一个节点去模拟.. 我们只需要能够维护,将最小值或最大值转到根.模拟一下发现,对于最小值,它的右子树深度不变(如果存在),其余节点深度全部\( ...

  6. java获取一年的周数和间隔天数

    java获取一年的周数和间隔天数 import java.text.ParseException; import java.text.SimpleDateFormat; import java.uti ...

  7. JavaScript基础笔记(三) 引用类型

    引用类型 引用类型的值(对象)是引用类型的一个实例. 一.Object类型 创建Object实例: //方法一:通过new操作符创建 var a = new Object(); a.neme = &q ...

  8. 潭州课堂25班:Ph201805201 django 项目 第七课 用户模型设计 (课堂笔记

    在 user 的应用中的 models.py: 导入 dango 自带的用户模型 from django.contrib.auth.models import AbstractUser,UserMan ...

  9. hihoCoder 1143 : 骨牌覆盖问题·一(递推,矩阵快速幂)

    [题目链接]:click here~~ 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 骨牌,一种古老的玩具.今天我们要研究的是骨牌的覆盖问题: 我们有一个2xN的长条形 ...

  10. MyBatis初探

    首先下载MyBatis jar包, 可以去MyBatis中文官网下载 项目中导入MyBatis jar包和JDBC jar包(此处用的MySQL) 新建conf.xml 内容如下: <?xml ...