using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using Microsoft.Office.Interop.PowerPoint;
using Microsoft.Office.Interop.Word;
using Microsoft.Office.Core;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
//convertDoc();
convertPpt();
}
static void convertDoc()
{
// Create a new Microsoft Word application object
Microsoft.Office.Interop.Word.Application word = new Microsoft.Office.Interop.Word.Application();
// C# doesn't have optional arguments so we'll need a dummy value
object oMissing = System.Reflection.Missing.Value;
// Get list of Word files in specified directory
DirectoryInfo dirInfo = new DirectoryInfo(@"E:\copy");
string fileType = "doc";
FileInfo[] wordFiles = dirInfo.GetFiles("*." + fileType, SearchOption.AllDirectories);
word.Visible = false;
word.ScreenUpdating = false;
System.Console.WriteLine("total " + wordFiles.Length);
int count = 0;
StringBuilder strBuilder = new StringBuilder();
foreach (FileInfo wordFile in wordFiles)
{
try
{
// Cast as Object for word Open method
Object filename = (Object)wordFile.FullName;
strBuilder.Append("start " + count + "/" + wordFiles.Length + " " + wordFile.FullName + Environment.NewLine);
System.Console.WriteLine("start " + count + "/" + wordFiles.Length + " " + wordFile.FullName);
// Use the dummy value as a placeholder for optional arguments
Document doc = word.Documents.Open(ref filename, ref oMissing,
ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
ref oMissing, ref oMissing, ref oMissing, ref oMissing);
//doc.Activate();
object outputFileName = wordFile.FullName.Replace("." + fileType, ".pdf");
object fileFormat = WdSaveFormat.wdFormatPDF;
// Save document into PDF Format
doc.SaveAs(ref outputFileName,
ref fileFormat, ref oMissing, ref oMissing,
ref oMissing, ref oMissing, ref oMissing, ref oMissing,
ref oMissing, ref oMissing, ref oMissing, ref oMissing,
ref oMissing, ref oMissing, ref oMissing, ref oMissing);
strBuilder.Append("end " + count + "/" + wordFiles.Length + " " + wordFile.FullName + Environment.NewLine);
System.Console.WriteLine("end " + count + "/" + wordFiles.Length + " " + wordFile.FullName);
count++;
// Close the Word document, but leave the Word application open.
// doc has to be cast to type _Document so that it will find the
// correct Close method.
object saveChanges = WdSaveOptions.wdDoNotSaveChanges;
((_Document)doc).Close(ref saveChanges, ref oMissing, ref oMissing);
doc = null;
}
catch (Exception ex)
{
strBuilder.Append(wordFile.FullName + " " + ex.Message);
System.Console.WriteLine(wordFile.FullName + " " + ex.Message);
}
// word has to be cast to type _Application so that it will find
// the correct Quit method.
}
((Microsoft.Office.Interop.Word.Application)word).Quit(ref oMissing, ref oMissing, ref oMissing);
word = null;
File.WriteAllText(@"E:\convert-" + fileType + ".txt", strBuilder.ToString());
System.Console.WriteLine("finish");
System.Console.ReadLine();
}
static void convertPpt()
{
// Create a new Microsoft Word application object
Microsoft.Office.Interop.PowerPoint.Application word = new Microsoft.Office.Interop.PowerPoint.Application();
// C# doesn't have optional arguments so we'll need a dummy value
object oMissing = System.Reflection.Missing.Value;
// Get list of Word files in specified directory
DirectoryInfo dirInfo = new DirectoryInfo(@"E:\copy");
dirInfo = new DirectoryInfo(@"E:\copy\hệ điều hành_đỗ văn uy_dhbkhn\lecture");
string fileType = "pptx";
FileInfo[] wordFiles = dirInfo.GetFiles("*." + fileType, SearchOption.AllDirectories);
System.Console.WriteLine("total " + wordFiles.Length);
int count = 0;
//word.Visible = false;
//word.ScreenUpdating = false;
StringBuilder strBuilder = new StringBuilder();
foreach (FileInfo wordFile in wordFiles)
{
Presentation doc = null;
try
{
// Cast as Object for word Open method
Object filename = (Object)wordFile.FullName;
strBuilder.Append("start " + count + "/" + wordFiles.Length + " " + wordFile.FullName + Environment.NewLine);
System.Console.WriteLine("start " + count + "/" + wordFiles.Length + " " + wordFile.FullName);
doc = word.Presentations.Open(wordFile.FullName, MsoTriState.msoFalse, MsoTriState.msoFalse, MsoTriState.msoFalse);
//word.Activate();
string outputFileName = wordFile.FullName.Replace("." + fileType, ".pdf");
object fileFormat = WdSaveFormat.wdFormatPDF;
Microsoft.Office.Interop.PowerPoint.PpSaveAsFileType targetFileType = Microsoft.Office.Interop.PowerPoint.PpSaveAsFileType.ppSaveAsPDF;
// Save document into PDF Format
doc.SaveCopyAs(outputFileName, targetFileType, MsoTriState.msoCTrue);
strBuilder.Append("end " + count + "/" + wordFiles.Length + " " + wordFile.FullName + Environment.NewLine);
System.Console.WriteLine("end " + count + "/" + wordFiles.Length + " " + wordFile.FullName);
count++;
object saveChanges = WdSaveOptions.wdDoNotSaveChanges;
//doc.Close();
//((_Document)word).Close(ref saveChanges, ref oMissing, ref oMissing);
}
catch (Exception ex)
{
strBuilder.Append(wordFile.FullName + " " + ex.Message);
System.Console.WriteLine(wordFile.FullName + " " + ex.Message);
}
finally
{
if (doc != null)
{
doc.Close();
doc = null;
}
}
}
File.WriteAllText(@"E:\convert-" + fileType + ".txt", strBuilder.ToString());
// word has to be cast to type _Application so that it will find
// the correct Quit method.
((Microsoft.Office.Interop.PowerPoint.Application)word).Quit();
word = null;
System.Console.WriteLine("finish");
System.Console.ReadLine();
}
}
}
//add reference
Không có nhận xét nào:
Đăng nhận xét