基本信息
源码名称:文档转换工具(源码)
源码大小:1.11M
文件格式:.zip
开发语言:C#
更新时间:2021-10-16
友情提示:(无需注册或充值,赞助后即可获取资源下载链接)
嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):78630559
本次赞助数额为: 2 元×
微信扫码支付:2 元
×
请留下您的邮箱,我们将在2小时内将文件发到您的邮箱
源码介绍
A File Dialog will open and you can select multiple files of doc, docx, ppt, pptx.
using System;
using System.Diagnostics;
using System.Threading;
using System.Windows.Forms;
using Microsoft.Office.Interop.PowerPoint;
using Microsoft.Office.Interop.Word;
using Task = System.Threading.Tasks.Task;
namespace PdfConverter
{
public class Program
{
public static int totalWork = 0;
public static int finishedWorkNumber = 0;
public static readonly object Lock = new object();
public static void FinishedWorkAddOne_ShowProgress(string targetPath)
{
lock (Lock) {
finishedWorkNumber ;
Console.WriteLine($"[{finishedWorkNumber} / {totalWork}] {targetPath}");
}
}
[STAThread]
static void Main(string[] args)
{
OpenFileDialog fileDialog = new OpenFileDialog
{
Multiselect = true,
Title = "Please select doc, docx, ppt, pptx files that need to be converted to pdf",
Filter = "MEOW? (*.doc, *.docx, *.ppt, *pptx)|*.doc;*.docx;*.ppt;*.pptx"
};
if (fileDialog.ShowDialog() != DialogResult.OK) { fileDialog.Dispose(); return; }
Stopwatch stopwatch = Stopwatch.StartNew();
string[] names = fileDialog.FileNames;
totalWork = names.Length;
Task[] tasks = new Task[totalWork];
for (int i = 0; i < totalWork; i )
{
string file = names[i];
string extension = System.IO.Path.GetExtension(file);
string sourcePath = file;
string targetPath = file.Substring(0, file.Length - extension.Length) ".pdf";
if (IsWord(extension))
{
tasks[i] = Task.Run(() => WordToPDF(sourcePath, targetPath));
}
else if (IsPowerPoint(extension))
{
tasks[i] = Task.Run(() => PowerPointToPDF(sourcePath, targetPath));
}
}
Thread.Sleep(2000);
Task.WhenAll(tasks).Wait();
fileDialog.Dispose();
stopwatch.Stop();
Console.WriteLine("\nAll work finsih! Spent " stopwatch.Elapsed.TotalSeconds " seconds");
Console.Write("Press any Enter to exit ...");
Console.ReadLine();
}
public static bool IsWord(string extension) => extension.Equals(".doc") || extension.Equals(".docx");
public static bool IsPowerPoint(string extension) => extension.Equals(".ppt") || extension.Equals(".pptx");
public static void WordToPDF(string sourcePath, string targetPath)
{
Microsoft.Office.Interop.Word.Application application = new Microsoft.Office.Interop.Word.Application();
Document document = null;
try
{
application.Visible = false;
document = application.Documents.Open(sourcePath);
document.ExportAsFixedFormat(targetPath, WdExportFormat.wdExportFormatPDF);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
finally
{
FinishedWorkAddOne_ShowProgress(targetPath);
document.Close();
application.Quit();
}
}
public static void PowerPointToPDF(string sourcePath, string targetPath)
{
Microsoft.Office.Interop.PowerPoint.Application application = new Microsoft.Office.Interop.PowerPoint.Application();
Presentation presentation = application.Presentations.Open(sourcePath, WithWindow: Microsoft.Office.Core.MsoTriState.msoFalse);
try
{
presentation.ExportAsFixedFormat(targetPath, PpFixedFormatType.ppFixedFormatTypePDF);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
finally
{
FinishedWorkAddOne_ShowProgress(targetPath);
presentation.Close();
application.Quit();
}
}
}
}