word,excel,ppt,txt转换为 PDF

/// <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 = 0;
        int paramEndPage = 0;
        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

原文地址:https://www.cnblogs.com/allen0118/p/8497031.html

时间: 2024-08-10 00:52:52

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

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

承上启下,可折叠 上一篇说的是:服务器是windows server时,用jacob将msoffice(指的是word,excel,ppt)转换成pdf. 若被部署项目的服务器是centOS等linux server时,就不能用之前的上述说的那种方式了. 在上一篇说到openoffice将msoffice转成pdf的时候会存在排版错位的问题,或者有的内容消失了,这是因为msoffice中的一些特有格式,openoffice不识别解析不了导致的.当然大部分的普通msoffice文档转换成pdf时,

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

Atitit.office word  excel  ppt pdf 的web在线预览方案与html转换方案 attilax 总结 1. office word  excel pdf 的web预览要求1 1.1. 显示效果要好1 1.2. 可以自定义显示界面1 1.3. 不需要控件,兼容性好1 1.4. 支持编辑操作1 2. 纯html预览解决之道(自由的格式)1 3. 转换swf flash方案2 4. 转换pdf方式..更多的浏览器已经直接支持pdf格式查看2 5. 控件方式2 6. Hyb

ppt怎么转换为pdf

ppt怎么转换为pdf最近朋友遇到了件麻烦事,公司领导让他将去年每个月编写的的仓库货物文件整理出来,制定要转成PDF格式文件,而大部分的文件都是PPT格式的,如何实现PPT转PDF?有没有好PPT转换成PDF转换器软件帮助解决两者的格式转换问题.答案就是迅捷PPT转PDF转换器. 转换器是实现文件格式之间的相互转换,最为高效便捷的一种方式.如何选择一款高质量的PPT转换PDF转换器呢?那当属PPT转PDF转换软件,笔者这么肯定回答是有根据的,首先PPT转换软件具有高效的智能识别能力,能够识别出P

Aspose是一个很强大的控件,可以用来操作word,excel,ppt等文件

Aspose是一个很强大的控件,可以用来操作word,excel,ppt等文件,用这个控件来导入.导出数据非常方便.其中Aspose.Cells就是用来操作Excel的,功能有很多.我所用的是最基本的功能,读取Excel的数据并导入到Dataset或数据库中.读取Excel表格数据的代码如下: 首先要引入命名空间:using Aspose.Cells; Workbook workbook = new Workbook(); workbook.Open("C:\\test.xlsx");

PHP 实现 word/excel/ppt 转换为 PDF

前段时间负责公司内部文件平台的设计,其中有一个需求是要能够在线浏览用户上传的 office 文件. 我的思路是先将 office 转换成 PDF,再通过 pdf.js 插件解析 PDF 文件,使其能在任何浏览器下查看. 可以通过 PHP 的 COM 组件,调用其它能够处理 office 文件的应用程序,利用提供的接口来转换 PDF 文件. OpenOffice OpenOffice 是一套开源跨平台的办公软件,由许多自由软件人士共同来维持,让大家能在 Microsoft Office 之外,还能

操作word,Excel,PPT

//C#编程,将Word转PDF,该方法有一定弊端,但是比起网路上的其他方法来说,还算比较好的,弊端是需要客户机上安装有WORD 2007或更高的版本.       //1.添加引用: Microsoft.Office.Interop.Word版本12.0.0.0:       // 2.在开头添加命名空间引用:using Microsoft.Office.Interop.Word;       //3.具体实现方法如下:       //Word转换成pdf       ///<summary

lucent检索技术之创建索引:使用POI读取txt/word/excel/ppt/pdf内容

在使用lucent检索文档时,必须先为各文档创建索引.索引的创建即读出文档信息(如文档名称.上传时间.文档内容等),然后再经过分词建索引写入到索引文件里.这里主要是总结下读取各类文档内容这一步. 一.之前做过一个小工具也涉及到读取word和excel内容,采用的是com组件的方式来读取.即导入COM库,引入命名空间(using Microsoft.Office.Interop.Word;using Microsoft.Office.Interop.Excel;),然后读代码如下: 读取word

asp.net 文件下载(txt,rar,pdf,word,excel,ppt)

aspx 文件下载说起来一点都不难,但是在做的过程中还是遇到了一些小小的问题,就是因为这些小小的问题,导致解决起来实在是太难了,其中一个就是Response.End();导致下载文件出现线程终止的情况... 正确的下载文件的方法 1 //获取对应文件的内容,这里主要取comm.FileURL的文件保存动态路径,也就是20150825/5e7af276b7754363a1e78b496e1d1603文本文档.txt 2 CommNoticeModel comm = CommNoticeBLL.Ge

关于在线预览word,excel,ppt,pdf的需求处理方法。

参考文档:http://www.cnblogs.com/wolf-sun/p/3574278.html 我选用的方案:先用office com组件生成pdf,然后使用pdf.js在线预览pdf文档.在自己写demo的过程下遇到如下两个问题,在此记录一下,希望能帮助到遇到同类问题的兄弟姐妹们.   1.在服务器上安装Office Plus 2010后,使用其com组件生成pdf时出现权限错误 System.UnauthorizedAccessException: Retrieving the CO