111,092
社区成员




using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;
using System.Text;
public class toSwf
{
private void Exec(string arguments, string exePath)
{
System.Diagnostics.Process proc = new System.Diagnostics.Process();
proc.StartInfo.FileName = exePath;
proc.StartInfo.Arguments = arguments;
proc.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
proc.Start();
proc.WaitForExit();
proc.Close();
}
/// <summary>
/// 返回页数
/// </summary>
/// <param name="pdfPath">PDF文件地址</param>
public int GetPageCount(string pdfPath)
{
try
{
byte[] buffer = File.ReadAllBytes(pdfPath);
int length = buffer.Length;
if (buffer == null)
return -1;
if (buffer.Length <= 0)
return -1;
string pdfText = Encoding.Default.GetString(buffer);
System.Text.RegularExpressions.Regex rx1 = new System.Text.RegularExpressions.Regex(@"/Type\s*/Page[^s]");
System.Text.RegularExpressions.MatchCollection matches = rx1.Matches(pdfText);
return matches.Count;
}
catch (Exception ex)
{
throw ex;
}
}
/// <summary>
/// 将PDF转换为SWF文件
/// </summary>
/// <param name="pdfPath">PDF文件路径</param>
/// <param name="swfPath">SWF文件路径</param>
/// <param name="page"></param>
public void ConvertToSwf(string pdfPath, string swfPath, int page)
{
try
{
string pdf2swf_exe = @"C:\Program Files (x86)\SWFTools\pdf2swf.exe";
string swfcombine_exe = @"C:\Program Files (x86)\SWFTools\swfcombine.exe";
if (!File.Exists(pdf2swf_exe))
{
throw new ApplicationException("Can not find: " + pdf2swf_exe);
}
StringBuilder sb = new StringBuilder();
sb.Append(" -o \"" + swfPath + "\"");//output
sb.Append(" -z");
sb.Append(" -T 9");//flash version
sb.Append(" -s disablelinks");//禁止PDF里面的链接
sb.Append(" -p " + "1" + "-" + page);//page range
sb.Append(" -j 100");//Set quality of embedded jpeg pictures to quality. 0 is worst (small), 100 is best (big). (default:85)
sb.Append(" \"" + pdfPath + "\"");//input
//执行swf转换
this.Exec(sb.ToString(),pdf2swf_exe);
string rfxview = @"C:\Program Files (x86)\SWFTools\swfs\rfxview.swf";
string arguments = string.Format("\"{0}\" viewport={1} -o {2}", rfxview, swfPath, swfPath);
this.Exec(arguments, swfcombine_exe);
}
catch (Exception ex)
{
throw ex;
}
}
}