zoukankan      html  css  js  c++  java
  • 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) + @"Fontssimkai.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) + @"Fontssimkai.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

  • 相关阅读:
    js公共时间戳方法es6
    vue权限路由
    vue中websocket心跳写法
    vue引入全局组件和局部组件的区别以及全局注册公共函数
    [干货]关于vue作用域插槽的新的深入理解
    vue系统指令v-text(前端网备份)
    浏览器兼容html头部<meta>标签主要内容详情
    如何设置td中溢出内容的隐藏显示
    7-4列表/元组/字典/集合的内置方法
    7-3 整型/浮点型/字符串/列表常用方法
  • 原文地址:https://www.cnblogs.com/allen0118/p/8497031.html
Copyright © 2011-2022 走看看