using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using License = SolidFramework.License;
namespace PdfConvert
{
class Program
{
public enum TransType
{
txt=1,
doc=2,
xls=3
}
static void Main(string[] args)
{
string pdfPath = @"C:\Users\qingping.li\Desktop\002217-20170425-测试报告:2016年年度报告_3962K.pdf";
string transFilePath = @"C:\Users\qingping.li\Desktop";
//Solid Framework是收费软件,需要输入协议才可以;(这是破解协议) SolidFramework.dll, v9.1.7276.1
License.Import("Generic Solid Framework License", "generic@soliddocuments.com", "Solid Documents", "QZRP", "NOCALL");
string erroMessage = string.Empty;
TransPdf.TrasnsPdf(pdfPath, transFilePath, TransType.xls, ref erroMessage);
bool checkSucess = true;
//PDF放置路径
string PdfPath = string.Empty;
//转换后文件存储路径
string storagePath = string.Empty;
//转换为何种格式 数字1表示转为txt格式;数字2表示转换为Word格式;输入3表示转换为Excel格式
string transType = string.Empty;
//错误信息
//string Input = Console.ReadLine();
string[] typeArray = { "txt", "doc", "xls" };
if (args.Length != 3)
{
erroMessage = "Error:请输入三个参数!";
checkSucess = false;
}
else
{
PdfPath = args[0];
storagePath = args[1];
transType =args[2];
if (!File.Exists(PdfPath))
{
erroMessage = "Error:输入pdf文件不存在,请重新输入pdf存储合法路径。";
checkSucess = false;
}
if (!Directory.Exists(storagePath))
{
erroMessage = "Error:输出路径不存在,请重新输入转换后文件存储合法路径。";
checkSucess = false;
}
if (!typeArray.Contains(transType))
{
erroMessage = "Error:请重新输入正确的转换格式。";
checkSucess = false;
}
}
//checkSucess = TransPdf.Check(Input, ref PdfPath, ref storagePath, ref transType, ref erroMessage);
if (!checkSucess)
{
Console.WriteLine(erroMessage);
}
else
{
TransPdf.TrasnsPdf(PdfPath, storagePath, (TransType)Enum.Parse(typeof(TransType), transType), ref erroMessage);
string[] files = PdfPath.Replace(@"\", "○").Split('○');
if (erroMessage == "")
{
Console.WriteLine("pdf文件{0}转换为{1}成功!", PdfPath.Replace(@"\", "○").Split('○')[files.Length - 1].Split('.')[0] + ".pdf", PdfPath.Replace(@"\", "○").Split('○')[files.Length - 1].Split('.')[0] + TransPdf.GetFormat(transType));
}
else
{
Console.WriteLine("pdf{0}文件转换为{1}失败!失败原因:{2}", TransPdf.GetType(transType), PdfPath.Replace(@"\", "○").Split('○')[files.Length - 1].Split('.')[0], erroMessage);
}
}
}
}
}
#class TransPdf
using SolidFramework.Converters;
using SolidFramework.Converters.Plumbing;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading;
namespace PdfConvert
{
class TransPdf
{
//SolidFramework.dll, v9.1.7276.1
public static void TrasnsPdf(string PathPdf, string storagePath, Program.TransType transType, ref string erroMessage)
{
try
{
if (PathPdf.Substring(PathPdf.LastIndexOf(".") + 1, (PathPdf.Length - PathPdf.LastIndexOf(".") - 1)) == "pdf")
{
//获得文件信息
string[] files = PathPdf.Replace(@"\", "○").Split('○');
string path1f;
var f = new PagesArray();
int start = 1;
int end = 10;
for (int i = start; i < end; i++)
{
//添加要更改的页数
f.Add(i);
}
if (transType == Program.TransType.txt) //转txt
{
//转换后文件的全名称(不包含扩展名)
path1f = string.Format("{0}/{1}", storagePath, files[files.Length - 1].Split('.')[0]);
//实例化转换TXT类
PdfToTextConverter fsTxt = new PdfToTextConverter();
//添加需要转换的文件
fsTxt.AddSourceFile(PathPdf);
//输出路径(可以不要)
fsTxt.OutputDirectory = storagePath;
//设置要转换的范围
fsTxt.PageRange = new SolidFramework.PageRange(f);
//最关键的转换方法
fsTxt.ConvertTo(path1f, true);
//清理文件来源
fsTxt.ClearSourceFiles();
//释放资源
fsTxt.Dispose();
//休眠一下抓换相当耗费资源
Thread.Sleep(500);
}
else if (transType == Program.TransType.doc)
{//转Word
path1f = string.Format("{0}/{1}", storagePath, files[files.Length - 1].Split('.')[0]);
PdfToWordConverter fsWord = new PdfToWordConverter();
//添加需要转换的文件
fsWord.AddSourceFile(PathPdf);
//输出路径(可以不要)
fsWord.OutputDirectory = storagePath;
//定义输出格式
fsWord.OutputType = WordDocumentType.Doc;
//设置要转换的范围
fsWord.PageRange = new SolidFramework.PageRange(f);
//最关键的转换方法
fsWord.ConvertTo(path1f, true);
//清理文件来源
fsWord.ClearSourceFiles();
//释放资源
fsWord.Dispose();
//休眠一下抓换相当耗费资源
Thread.Sleep(500);
}
else
{//转excel
path1f = string.Format("{0}/{1}", storagePath, files[files.Length - 1].Split('.')[0]);
PdfToExcelConverter fsExcel = new PdfToExcelConverter();
//添加需要转换的文件
fsExcel.AddSourceFile(PathPdf);
//输出路径(可以不要)
fsExcel.OutputDirectory = storagePath;
//定义输出格式
fsExcel.OutputType = ExcelDocumentType.XlsX;
//设置要转换的范围
fsExcel.PageRange = new SolidFramework.PageRange(f);
//转换为单个sheet
fsExcel.SingleTable = ExcelTablesOnSheet.PlaceAllTablesOnSingleSheet;
fsExcel.TablesFromContent = true;
//最关键的转换方法
fsExcel.ConvertTo(path1f, true);
//清理文件来源
fsExcel.ClearSourceFiles();
//释放资源
fsExcel.Dispose();
//休眠一下抓换相当耗费资源
Thread.Sleep(500);
}
erroMessage = "";
}
}
catch (Exception e)
{
erroMessage = e.ToString();
}
}
public static string GetType(string transType)
{
string type = "";
switch (transType)
{
case "1":
type = "Txt";
break;
case "2":
type = "Word";
break;
case "3":
type = "Excel";
break;
}
return type;
}
public static string GetFormat(string transType)
{
string type = "";
switch (transType)
{
case "txt":
type = ".txt";
break;
case "doc":
type = ".docx";
break;
case "xls":
type = ".xlsx";
break;
}
return type;
}
public static bool Check(string Input, ref string PathPdf, ref string storagePath, ref string transType, ref string ErroMessage)
{
string[] typeArray = { "txt", "doc", "xls" };
string[] paraMeter = Input.Split(' ');
if (paraMeter.Length != 3)
{
ErroMessage = "Error:请输入三个参数!";
return false;
}
else
{
PathPdf = paraMeter[0];
storagePath = paraMeter[1];
transType = paraMeter[2];
if (!File.Exists(PathPdf))
{
ErroMessage = "Error:输入pdf文件不存在,请重新输入合法路径";
return false;
}
if (!Directory.Exists(storagePath))
{
ErroMessage = "Error:输出路径不存在,请重新输入输出合法路径";
return false;
}
if (!typeArray.Contains(transType))
{
ErroMessage = "请输入要转换的格式";
return false;
}
return true;
}
}
}
}