word 相关文件转PDf和SWF(内含TXT文件编码处理)

/// <summary>
/// 支持word、excel、ppt转化为pdf,以及pdf转化为swf文件格式。同时要考虑关于图片文件处理
/// 该类的引用地址:http://www.2cto.com/kf/201301/186201.html
/// pdf转化为swf地址:http://outofmemory.cn/code-snippet/1930/c-jiang-pdf-file-zhuanhuancheng-flash-swf
/// flexPaper概述地址:http://www.cnblogs.com/sobne/articles/1822479.html
/// http://wingfly.net/blog/post/22.html
/// </summary>
public class FileUtil
{
/// <summary>
/// 把Word文件转换成为PDF格式文件
/// </summary>
/// <param name="sourcePath">源文件路径</param>
/// <param name="targetPath">目标文件路径</param>
/// <returns>true=转换成功</returns>
public static bool WordToPDF(string sourcePath, string targetPath)
{
bool result = false;
Microsoft.Office.Interop.Word.WdExportFormat exportFormat = Microsoft.Office.Interop.Word.WdExportFormat.wdExportFormatPDF;
Microsoft.Office.Interop.Word.Application application = null;
Microsoft.Office.Interop.Word.Document document = null;
try
{
application = new Microsoft.Office.Interop.Word.Application();
application.Visible = false;
document = application.Documents.Open(sourcePath, ReadOnly: true);
//write("E:\\log\\log.txt", "打开文件" + sourcePath);
//document.SaveAs();
//write("E:\\log\\log.txt", "保存文件" + targetPath);
document.ExportAsFixedFormat(targetPath, exportFormat);
result = true;
}
catch (Exception ex)
{
result = false;
//write("E:\\log\\log.txt", ex.Message);
}
finally
{
if (document != null)
{
document.Close();
document = null;
}
if (application != null)
{
application.Quit();
application = null;
}
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
GC.WaitForPendingFinalizers();
}

return result;
}

/// <summary>
/// 把Microsoft.Office.Interop.Excel文件转换成PDF格式文件
/// </summary>
/// <param name="sourcePath">源文件路径</param>
/// <param name="targetPath">目标文件路径</param>
/// <returns>true=转换成功</returns>
public static bool ExcelToPDF(string sourcePath, string targetPath)
{
bool result = false;
XlFixedFormatType targetType = XlFixedFormatType.xlTypePDF;
object missing = Type.Missing;
Application application = null;
Workbook workBook = null;
try
{
application = new Application();
application.Visible = false;
workBook = application.Workbooks.Open(sourcePath, ReadOnly: true);
workBook.SaveAs();
workBook.ExportAsFixedFormat(targetType, targetPath);
result = true;
}
catch (Exception ex)
{
// Console.WriteLine(e.Message);
result = false;
throw;
}
finally
{
if (workBook != null)
{
workBook.Close(true, missing, missing);
workBook = null;
}
if (application != null)
{
application.Quit();
application = null;
}
GC.Collect();
GC.WaitForPendingFinalizers();
//GC.Collect();
//GC.WaitForPendingFinalizers();
}
return result;
}
/// <summary>
/// 把PowerPoint文件转换成PDF格式文件
/// </summary>
/// <param name="sourcePath">源文件路径</param>
/// <param name="targetPath">目标文件路径</param>
/// <returns>true=转换成功</returns>
public static bool PowerPointToPDF(string sourcePath, string targetPath)
{
bool result;
Microsoft.Office.Interop.PowerPoint.PpSaveAsFileType targetFileType = Microsoft.Office.Interop.PowerPoint.PpSaveAsFileType.ppSaveAsPDF;
object missing = Type.Missing;
Microsoft.Office.Interop.PowerPoint.Application application = null;
Microsoft.Office.Interop.PowerPoint.Presentation persentation = null;
try
{
application = new Microsoft.Office.Interop.PowerPoint.Application();
//application.Visible = MsoTriState.msoFalse;
persentation = application.Presentations.Open(sourcePath, MsoTriState.msoTrue, MsoTriState.msoFalse, MsoTriState.msoFalse);
persentation.SaveAs(targetPath, targetFileType, Microsoft.Office.Core.MsoTriState.msoTrue);

result = true;
}
catch (Exception ex)
{
// Console.WriteLine(e.Message);
result = false;
throw;
}
finally
{
if (persentation != null)
{
persentation.Close();
persentation = null;
}
if (application != null)
{
application.Quit();
application = null;
}
GC.Collect();
GC.WaitForPendingFinalizers();
//GC.Collect();
//GC.WaitForPendingFinalizers();
}
return result;
}
/// <summary>
/// 把PowerPoint文件转换成PDF格式文件
/// </summary>
/// <param name="sourcePath">源文件路径</param>
/// <param name="targetPath">目标文件路径</param>
/// <returns>true=转换成功</returns>
public static bool TxtToPDF(string sourcePath, string targetPath)
{
try
{
Encoding edStr = GetType(sourcePath);
//第一个参数是txt文件物理路径
string[] lines = System.IO.File.ReadAllLines(sourcePath, edStr);
//iTextSharp.text.PageSize.A4 自定义页面大小
iTextSharp.text.Document doc = new iTextSharp.text.Document(iTextSharp.text.PageSize.A4, 50, 20, 20, 20);
PdfWriter.GetInstance(doc, new FileStream(targetPath, FileMode.Create));
doc.Open();
//创建我的基本字体
BaseFont baseFont = BaseFont.CreateFont("c:\\windows\\fonts\\simkai.ttf", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
//创建字体 字体大小,字体粗細 字体颜色
iTextSharp.text.Font font = new iTextSharp.text.Font(baseFont, 11, iTextSharp.text.Font.NORMAL, BaseColor.BLACK);

iTextSharp.text.Paragraph paragraph;
foreach (string line in lines)
{
paragraph = new iTextSharp.text.Paragraph(line, font);
doc.Add(paragraph);
}

//关闭文件
doc.Close();
return true;

}
catch (Exception ex)
{
throw;
}

}


/// <summary>
/// 给定文件的路径,读取文件的二进制数据,判断文件的编码类型
/// </summary>
/// <param name=“FILE_NAME“>文件路径</param>
/// <returns>文件的编码类型</returns>
public static System.Text.Encoding GetType(string FILE_NAME)
{
FileStream fs = new FileStream(FILE_NAME, FileMode.Open, FileAccess.Read);
Encoding r = GetType(fs);
fs.Close();
return r;
}

/// <summary>
/// 通过给定的文件流,判断文件的编码类型
/// </summary>
/// <param name=“fs“>文件流</param>
/// <returns>文件的编码类型</returns>
public static System.Text.Encoding GetType(FileStream fs)
{
byte[] Unicode = new byte[] { 0xFF, 0xFE, 0x41 };
byte[] UnicodeBIG = new byte[] { 0xFE, 0xFF, 0x00 };
byte[] UTF8 = new byte[] { 0xEF, 0xBB, 0xBF }; //带BOM
Encoding reVal = Encoding.Default;

BinaryReader r = new BinaryReader(fs, System.Text.Encoding.Default);
int i;
int.TryParse(fs.Length.ToString(), out i);
byte[] ss = r.ReadBytes(i);
if (IsUTF8Bytes(ss) || (ss[0] == 0xEF && ss[1] == 0xBB && ss[2] == 0xBF))
{
reVal = Encoding.UTF8;
}
else if (ss[0] == 0xFE && ss[1] == 0xFF && ss[2] == 0x00)
{
reVal = Encoding.BigEndianUnicode;
}
else if (ss[0] == 0xFF && ss[1] == 0xFE && ss[2] == 0x41)
{
reVal = Encoding.Unicode;
}
r.Close();
return reVal;

}

/// <summary>
/// 判断是否是不带 BOM 的 UTF8 格式
/// </summary>
/// <param name=“data“></param>
/// <returns></returns>
private static bool IsUTF8Bytes(byte[] data)
{
int charByteCounter = 1; //计算当前正分析的字符应还有的字节数
byte curByte; //当前分析的字节.
for (int i = 0; i < data.Length; i++)
{
curByte = data[i];
if (charByteCounter == 1)
{
if (curByte >= 0x80)
{
//判断当前
while (((curByte <<= 1) & 0x80) != 0)
{
charByteCounter++;
}
//标记位首位若为非0 则至少以2个1开始 如:110XXXXX...........1111110X
if (charByteCounter == 1 || charByteCounter > 6)
{
return false;
}
}
}
else
{
//若是UTF-8 此时第一位必须为1
if ((curByte & 0xC0) != 0x80)
{
return false;
}
charByteCounter--;
}
}
if (charByteCounter > 1)
{
throw new Exception("非预期的byte格式");
}
return true;
}

/// <summary>
/// 把PDF文件转化为SWF文件
/// </summary>
/// <param name="toolPah">pdf2swf工具路径</param>
/// <param name="sourcePath">源文件路径</param>
/// <param name="targetPath">目标文件路径</param>
/// <returns>true=转化成功</returns>
public static bool PDFToSWF(string toolPah, string sourcePath, string targetPath)
{
Process pc = new Process();
bool returnValue = true;

string cmd = toolPah;
string args = " -t " + sourcePath + " -s flashversion=9 -o " + targetPath;
try
{
ProcessStartInfo psi = new ProcessStartInfo(cmd, args);
psi.WindowStyle = ProcessWindowStyle.Hidden;
pc.StartInfo = psi;
pc.Start();
pc.WaitForExit();
}
catch (Exception ex)
{

//write("E:\\log\\log.txt", ex.Message);
returnValue = false;
throw;
}
finally
{
pc.Close();
pc.Dispose();
}
return returnValue;
}

/// <summary>
/// png、jpg和jpeg文件的转化
/// </summary>
/// <param name="toolPah"></param>
/// <param name="sourcePath"></param>
/// <param name="targetPath"></param>
/// <returns></returns>
public static bool PicturesToSwf(string toolPah, string sourcePath, string targetPath)
{
Process pc = new Process();
bool returnValue = true;

string cmd = toolPah;
string args = " " + sourcePath + " -o " + targetPath + " -T 9";
//如果是多个图片转化为swf 格式为 ..jpeg2swf.exe C:\1.jpg C:\2.jpg -o C:\swf1.swf
try
{
ProcessStartInfo psi = new ProcessStartInfo(cmd, args);
psi.WindowStyle = ProcessWindowStyle.Hidden;
pc.StartInfo = psi;
pc.Start();
pc.WaitForExit();
}
catch (Exception ex)
{
returnValue = false;
throw;
}
finally
{
pc.Close();
pc.Dispose();
}
return returnValue;
}
/// <summary>
/// Gif文件转化为swf
/// </summary>
/// <param name="toolPah"></param>
/// <param name="sourcePath"></param>
/// <param name="targetPath"></param>
/// <returns></returns>
public static bool GifPicturesToSwf(string toolPah, string sourcePath, string targetPath)
{
Process pc = new Process();
bool returnValue = true;

string cmd = toolPah;
string args = " " + sourcePath + " -o " + targetPath;
try
{
ProcessStartInfo psi = new ProcessStartInfo(cmd, args);
psi.WindowStyle = ProcessWindowStyle.Hidden;
pc.StartInfo = psi;
pc.Start();
pc.WaitForExit();
}
catch (Exception ex)
{
returnValue = false;
throw;
}
finally
{
pc.Close();
pc.Dispose();
}
return returnValue;
}
public static void write(string path, string msg)
{
try
{
File.AppendAllText(path, msg + "\r\n");
}
catch (Exception ex)
{

}
}
}

posted @ 2021-08-26 13:55  静静随笔  阅读(312)  评论(0编辑  收藏  举报