Program.cs
//程序集 Aspose.Pdf.dll, v9.4.0.0
using Aspose.Pdf;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Configuration;
namespace TestPdfTranslateExcle
{
class Program
{
static void Main(string[] args)
{
try
{
string pdfFileName = ConfigurationManager.AppSettings["pdfFileName"];
string saveExcelName = ConfigurationManager.AppSettings["saveExcelName"];
string saveDocName = ConfigurationManager.AppSettings["saveDocName"];
string saveHtmlName = ConfigurationManager.AppSettings["saveHtmlName"];
string translateType = "1";
//加载license
new Aspose.Pdf.License().SetLicense(License.LStream);
Stream fileStream = FileToStream(pdfFileName);
Document document = new Document(fileStream, true);
//设置转换为excel属性
ExcelSaveOptions excelSaveOption = new ExcelSaveOptions()
{
//最是使用sheet
MinimizeTheNumberOfWorksheets = true,
//同时也转换列
InsertBlankColumnAtFirst = true
};
//设置转换为Word属性
DocSaveOptions docSaveOption = new DocSaveOptions()
{
//转换格式
Format = DocSaveOptions.DocFormat.Doc,
Mode = DocSaveOptions.RecognitionMode.Flow
};
//转换为html需要研究
HtmlSaveOptions htmlSaveOption = new HtmlSaveOptions(HtmlDocumentType.Html5);
Console.WriteLine("开始转换!");
//保存为excel文件
if (translateType == "1")
document.Save(saveExcelName, excelSaveOption);
else if (translateType == "2")
document.Save(saveDocName, docSaveOption);
else if (translateType == "3")
{
//继续转换为pdf
string outFile = "output.pdf";
Document doc = new Document(pdfFileName);
doc.Convert(new MemoryStream(), PdfFormat.PDF_A_1A, ConvertErrorAction.Delete);
doc.Save(outFile);
}
else if (translateType == "4")
document.Save(saveHtmlName, SaveFormat.Html);
Console.WriteLine("转换完成!");
}
catch (Exception ex)
{
throw (ex);
}
#region 转换为PDF测试
//使用 Microsoft.Office
string strpath = @"C:\Users\qingping.li\Desktop\000025-20170408-测试报告:2016年年度报告_4737K.pdf";
string targetPath = @"C:\Users\qingping.li\Desktop\1.word";
WordToPDF(strpath, targetPath);
#endregion
}
/// <summary>
/// 将文件转换为steam
/// </summary>
/// <param name="fileName"></param>
/// <returns></returns>
public static Stream FileToStream(string fileName)
{
// 打开文件
FileStream fileStream = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read);
// 读取文件的 byte[]
byte[] bytes = new byte[fileStream.Length];
fileStream.Read(bytes, 0, bytes.Length);
fileStream.Close();
// 把 byte[] 转换成 Stream
Stream stream = new MemoryStream(bytes);
return stream;
}
public static bool WordToPDF(string sourcePath, string targetPath)
{
bool result = false;
Microsoft.Office.Interop.Word.Application application = new Microsoft.Office.Interop.Word.Application();
Microsoft.Office.Interop.Word.Document document = null;
try
{
application.Visible = false;
document = application.Documents.Open(sourcePath);
document.ExportAsFixedFormat(targetPath, Microsoft.Office.Interop.Word.WdExportFormat.wdExportFormatPDF);
result = true;
}
catch (Exception e)
{
Console.WriteLine(e.Message);
result = false;
}
finally
{
document.Close();
}
return result;
}
}
}
License.cs
using System;
using System.IO;
namespace TestPdfTranslateExcle
{
public static class License
{
//可用的licence
public const string Key =
"PExpY2Vuc2U+DQogIDxEYXRhPg0KICAgIDxMaWNlbnNlZFRvPkFzcG9zZSBTY290bGFuZCB" +
"UZWFtPC9MaWNlbnNlZFRvPg0KICAgIDxFbWFpbFRvPmJpbGx5Lmx1bmRpZUBhc3Bvc2UuY2" +
"9tPC9FbWFpbFRvPg0KICAgIDxMaWNlbnNlVHlwZT5EZXZlbG9wZXIgT0VNPC9MaWNlbnNlV" +
"HlwZT4NCiAgICA8TGljZW5zZU5vdGU+TGltaXRlZCB0byAxIGRldmVsb3BlciwgdW5saW1p" +
"dGVkIHBoeXNpY2FsIGxvY2F0aW9uczwvTGljZW5zZU5vdGU+DQogICAgPE9yZGVySUQ+MTQ" +
"wNDA4MDUyMzI0PC9PcmRlcklEPg0KICAgIDxVc2VySUQ+OTQyMzY8L1VzZXJJRD4NCiAgIC" +
"A8T0VNPlRoaXMgaXMgYSByZWRpc3RyaWJ1dGFibGUgbGljZW5zZTwvT0VNPg0KICAgIDxQc" +
"m9kdWN0cz4NCiAgICAgIDxQcm9kdWN0PkFzcG9zZS5Ub3RhbCBmb3IgLk5FVDwvUHJvZHVj" +
"dD4NCiAgICA8L1Byb2R1Y3RzPg0KICAgIDxFZGl0aW9uVHlwZT5FbnRlcnByaXNlPC9FZGl" +
"0aW9uVHlwZT4NCiAgICA8U2VyaWFsTnVtYmVyPjlhNTk1NDdjLTQxZjAtNDI4Yi1iYTcyLT" +
"djNDM2OGYxNTFkNzwvU2VyaWFsTnVtYmVyPg0KICAgIDxTdWJzY3JpcHRpb25FeHBpcnk+M" +
"jAxNTEyMzE8L1N1YnNjcmlwdGlvbkV4cGlyeT4NCiAgICA8TGljZW5zZVZlcnNpb24+My4w" +
"PC9MaWNlbnNlVmVyc2lvbj4NCiAgICA8TGljZW5zZUluc3RydWN0aW9ucz5odHRwOi8vd3d" +
"3LmFzcG9zZS5jb20vY29ycG9yYXRlL3B1cmNoYXNlL2xpY2Vuc2UtaW5zdHJ1Y3Rpb25zLm" +
"FzcHg8L0xpY2Vuc2VJbnN0cnVjdGlvbnM+DQogIDwvRGF0YT4NCiAgPFNpZ25hdHVyZT5GT" +
"zNQSHNibGdEdDhGNTlzTVQxbDFhbXlpOXFrMlY2RThkUWtJUDdMZFRKU3hEaWJORUZ1MXpP" +
"aW5RYnFGZkt2L3J1dHR2Y3hvUk9rYzF0VWUwRHRPNmNQMVpmNkowVmVtZ1NZOGkvTFpFQ1R" +
"Hc3pScUpWUVJaME1vVm5CaHVQQUprNWVsaTdmaFZjRjhoV2QzRTRYUTNMemZtSkN1YWoyTk" +
"V0ZVJpNUhyZmc9PC9TaWduYXR1cmU+DQo8L0xpY2Vuc2U+";
public static Stream LStream = (Stream)new MemoryStream(Convert.FromBase64String(TestPdfTranslateExcle.License.Key));
public static void SetPdfLicense()
{
var l = new Aspose.Pdf.License();
l.SetLicense(@"D:\Documents\Visual Studio 2013\Projects\PDF格式转换\TestPdfTranslateExcle\NewFolder1\Aspose.Pdf.lic");
}
}
}