zoukankan      html  css  js  c++  java
  • PPTFontsExtractor.cs



    using System;
    using System.IO;
    using System.Collections.Generic;
    using System.Linq;
    using System.Data;
    using System.Data.SqlClient;
    using System.Text;
    using System.Xml.Linq;
    using System.Windows.Forms;
    using Microsoft.Win32;
    using PowerPoint = Microsoft.Office.Interop.PowerPoint;
    using Office = Microsoft.Office.Core;

    namespace FontsExtractor
    {
        /// <summary>
        /// This extractor class opens a PowerPoint file and extracts all the fonts used
        /// in it.
        /// These font files will be saved on ".\fonts" directory with respect to the input
        /// PowerPoint file.
        /// An install.bat file will be generated in the same directory for easy installing
        /// of these extracted font files. Just run it on the target PC!
        /// </summary>
        class PPTFontsExtractor
        {
            // The input ppt file's name.
            private string _fileName;
            // The input ppt file's path.
            private string _path;
            // The output path for extracted font files.
            private string _fontOutputPath;

            // The font names used in the input file.
            private List<string> _fontNames = new List<string>();
            // The corresponding font files of the used fonts.
            private List<string> _fontFileNames = new List<string>();

            /// <summary>
            /// Initializes the input ppt file name and path.
            /// Makes a new directory for font files extracted.
            /// </summary>
            /// <param name="fileName">The input ppt file's name.</param>
            PPTFontsExtractor(string fileName)
            {
                _fileName = fileName;
                
                int splitIndex = fileName.LastIndexOf('\\');
                _path = fileName.Substring(0, splitIndex);
                
                _fontOutputPath = _path + @"\fonts";
                Directory.CreateDirectory(_fontOutputPath);
            }

            /// <summary>
            /// Extracts the fonts used in the input ppt file and saves the corresponding
            /// font files to the output directory.
            /// A .bat file for fonts installing will also be generated in the same directory.
            /// </summary>
            public void ExtractFonts()
            {
                Console.WriteLine("parsing fonts...");
                GetAllUsedFontNames();
                GetCorrespondingFontFiles();

                Console.WriteLine("extracting fonts...");
                ExportFontFilesOut();

                Console.WriteLine("generating intalling script...");
                GenerateInstallingScript();
            }

            /// <summary>
            /// Exports all the corresponding font files out from system's fonts directory.
            /// </summary>
            private void ExportFontFilesOut()
            {
                string fontDir = Environment.GetEnvironmentVariable("systemroot") + @"\fonts";
                _fontFileNames.ForEach(s => File.Copy(fontDir + '\\' + s, _fontOutputPath + '\\' + s, true));
            }

            /// <summary>
            /// Generates the installing script for the extracted fonts.
            /// </summary>
            private void GenerateInstallingScript()
            {
                StreamWriter writer = File.CreateText(_fontOutputPath + @"\install.bat");
                _fontFileNames.ForEach(s => writer.WriteLine("copy \"" + s + "\"" + @" %systemroot%\fonts\"));
                writer.Close();
            }

            /// <summary>
            /// Gets all the corresponding font file names through windows registry.
            /// </summary>
            private void GetCorrespondingFontFiles()
            {
                RegistryKey fonts = Registry.LocalMachine.OpenSubKey(@"Software\Microsoft\Windows NT\CurrentVersion\Fonts");
                string[] valueNames = fonts.GetValueNames();

                List<KeyValuePair<string, string>> fontMappingTable = valueNames
                    .Select(s => new KeyValuePair<string, string>(s, (string)fonts.GetValue(s)))
                    .ToList();

                foreach (string fontName in _fontNames)
                {
                    _fontFileNames.AddRange(fontMappingTable
                        .FindAll(p => p.Key.Contains(fontName))
                        .Select(p => p.Value)
                        .ToList());
                }
            }

            /// <summary>
            /// Gets all the font names used in the input ppt file.
            /// Each character in the text will be checked for integrity.
            /// </summary>
            private void GetAllUsedFontNames()
            {
                PowerPoint.Application pptApplication = new PowerPoint.ApplicationClass();
                PowerPoint.Presentation presentation = pptApplication.Presentations.Open2007(
                    _fileName,
                    Office.MsoTriState.msoTrue,
                    Office.MsoTriState.msoTrue,
                    Office.MsoTriState.msoFalse,
                    Office.MsoTriState.msoFalse);

                foreach (PowerPoint.Slide slide in presentation.Slides)
                {
                    foreach (PowerPoint.Shape shape in slide.Shapes)
                    {
                        if (shape.HasTextFrame == Office.MsoTriState.msoTrue &&
                            shape.TextFrame.HasText == Office.MsoTriState.msoTrue)
                        {
                            PowerPoint.TextRange textRange = shape.TextFrame.TextRange;
                            for (int i = 0; i < textRange.Length; ++i)
                            {
                                PowerPoint.Font font = textRange.Characters(i, 1).Font;
                                if (!_fontNames.Contains(font.Name))
                                    _fontNames.Add(font.Name);
                            }
                        }
                    }
                }

                presentation.Close();
                pptApplication.Quit();
            }

            [STAThread]
            static void Main(string[] args)
            {
                //OpenFileDialog dlg = new OpenFileDialog();
                //if (dlg.ShowDialog() == DialogResult.OK)
                //{
                //    PPTFontsExtractor fontExtractor = new PPTFontsExtractor(dlg.FileName);
                //    fontExtractor.ExtractFonts();
                //}


                DirectoryInfo di = new DirectoryInfo("D://My Documents//EfficientPIM AutoBackup");
                    FileInfo[] fi = di.GetFiles();

                    foreach (FileInfo fiTmp in fi)
                    {

                        String fileName =  fiTmp.Name.ToString();
                        fileName = fileName.Substring(0, fileName.Length - 4);
                        PPTFontsExtractor.Parse("D://My Documents//EfficientPIM AutoBackup//" + fiTmp.Name.ToString(), fileName);
                        //PptReader("F://tttttttt//" + fiTmp.Name.ToString(), fileName);
                
                    }
            }



            //public string PptReader(string DocPath,string filename)
            //{
            //    string fullname = DocPath + filename; //绝对路径
            //    PowerPoint.Application papp = new PowerPoint.Application();
            //    PowerPoint.Presentation ppr = papp.Presentations.Open(fullname, Microsoft.Office.Core.MsoTriState.msoCTrue,
            //    Microsoft.Office.Core.MsoTriState.msoFalse, Microsoft.Office.Core.MsoTriState.msoFalse);
            //    string doc = "";
            //    foreach (PowerPoint.Slide slide in ppr.Slides)
            //    {
            //        foreach (PowerPoint.Shape shape in slide.Shapes)
            //        {
            //            if (shape.HasTextFrame == Microsoft.Office.Core.MsoTriState.msoTrue)
            //            {
            //                if (shape.TextFrame.HasText == Microsoft.Office.Core.MsoTriState.msoTrue)
            //                {
            //                    doc += shape.TextFrame.TextRange.Text.ToString();
            //                    doc += "\n";
            //                }
            //            }

            //        }
            //    }

            //    ppr.Close();
            //    System.Runtime.InteropServices.Marshal.ReleaseComObject(ppr);
            //    ppr = null;
            //    papp.Quit();
            //    System.Runtime.InteropServices.Marshal.ReleaseComObject(papp);
            //    papp = null;
            //    Console.WriteLine(doc);
            //    return doc;
            //}

            public static  void Parse(String fileName,string pptName)
            {
                SqlConnection con = new SqlConnection(@"Data Source=.;Initial Catalog=WordProject;Integrated Security=True");
                con.Open();
                try
                {
                    PowerPoint.Application pa = new PowerPoint.ApplicationClass();
                    PowerPoint.Presentation pp = pa.Presentations.Open(fileName,
                          Microsoft.Office.Core.MsoTriState.msoTrue,
                          Microsoft.Office.Core.MsoTriState.msoFalse,
                          Microsoft.Office.Core.MsoTriState.msoFalse);
                    Console.WriteLine("Open Success");
                    PowerPoint.TextFrame frame;

                    String text="";

                    foreach (PowerPoint.Slide slide in pp.Slides)
                    {
                        foreach (PowerPoint.Shape shape in slide.Shapes)
                        {
                            if (shape.HasTextFrame == Microsoft.Office.Core.MsoTriState.msoTrue)
                            {
                                frame = shape.TextFrame;
                                if (frame.HasText == Microsoft.Office.Core.MsoTriState.msoTrue)
                                {
                                    text += frame.TextRange.Text.ToString();
                                    text += "\n";
                                }
                            }

                        }
        
                    }
                    string sqlins = @"Insert into Word_Content(Title,Content,Remark) values ('" + pptName + "','" + text + "','jjj')";
                    SqlCommand cmd = new SqlCommand(sqlins, con);
                    cmd.ExecuteNonQuery();
                    con.Close();

                }

                catch (Exception e)
                {
                    Console.WriteLine(e.Message);

                }
               
            }  
            


        }

    }
  • 相关阅读:
    【Tomcat】使用Eclipse发布项目时,项目启动路径错误。
    Unsupported major.minor version 52.0
    【转载】设置event.cancelBubble,使触发子元素的onclick不同时触发父元素的onclick
    【MG】测试
    【Tomcat】使用Eclipse运行Tomcat7源码
    【微信公众号】WeixinJSBridge.call('closeWindow')无效
    【微信公众号】使用a标签打开链接显示空白
    【JAVA】导出jar包时,Class files on classpath not found
    【JAVA】修改项目包名
    lesson3.1:java公平锁和非公平锁及读写锁
  • 原文地址:https://www.cnblogs.com/shihao/p/2498641.html
Copyright © 2011-2022 走看看