Using System. Web;
Using System. Text;
Public static class PSD2swfHelper
{
/// <Summary>
/// Convert all pages with an image quality of 80%
/// </Summary>
/// <Param name = "pdfPath"> pdf file address </param>
/// <Param name = "swfPath"> the generated SWF file address </param>
Public static bool extends 2swf (string pdfPath, string swfPath)
{
Return ipv2swf (pdfPath, swfPath, 1, GetPageCount (HttpContext. Current. Server. MapPath (pdfPath), 80 );
}
/// <Summary>
/// N pages before conversion, image quality 80%
/// </Summary>
/// <Param name = "pdfPath"> pdf file address </param>
/// <Param name = "swfPath"> the generated SWF file address </param>
/// <Param name = "page"> page number </param>
Public static bool defaults 2swf (string pdfPath, string swfPath, int page)
{
Return limit 2swf (pdfPath, swfPath, 1, page, 80 );
}
/// <Summary>
/// Convert the PDF format to SWF
/// </Summary>
/// <Param name = "pdfPath"> pdf file address </param>
/// <Param name = "swfPath"> the generated SWF file address </param>
/// <Param name = "beginpage"> conversion start page </param>
/// <Param name = "endpage"> conversion end page </param>
Private static bool rj2swf (string pdfPath, string swfPath, int beginpage, int endpage, int photoQuality)
{
String exe = HttpContext. Current. Server. MapPath ("~ /Bin/tools/pdf2swf-0.9.1.exe ");
PdfPath = HttpContext. Current. Server. MapPath (pdfPath );
SwfPath = HttpContext. Current. Server. MapPath (swfPath );
If (! System. IO. File. Exists (exe) |! System. IO. File. Exists (pdfPath) | System. IO. File. Exists (swfPath ))
{
Return false;
}
StringBuilder ** = new StringBuilder ();
**. Append ("\" "+ pdfPath + "\"");
**. Append ("-o \" "+ swfPath + "\"");
**. Append ("-s flashversion = 9 ");
If (endpage> GetPageCount (pdfPath) endpage = GetPageCount (pdfPath );
**. Append ("-p" + "\" "+ beginpage +" "+"-"+ endpage + "\"");
**. Append ("-j" + photoQuality );
String Command = **. ToString ();
System. Diagnostics. Process p = new System. Diagnostics. Process ();
P. StartInfo. FileName = exe;
P. StartInfo. Arguments = Command;
P. StartInfo. WorkingDirectory = HttpContext. Current. Server. MapPath ("~ /Bin /");
P. StartInfo. UseShellExecute = false;
P. StartInfo. RedirectStandardError = true;
P. StartInfo. CreateNoWindow = false;
P. Start ();
P. BeginErrorReadLine ();
P. WaitForExit ();
P. Close ();
P. Dispose ();
Return true;
}
/// <Summary>
/// Number of returned pages
/// </Summary>
/// <Param name = "pdfPath"> pdf file address </param>
Private static int GetPageCount (string pdfPath)
{
Byte [] buffer = System. IO. File. ReadAllBytes (pdfPath );
Int length = buffer. Length;
If (buffer = null)
Return-1;
If (buffer. Length <= 0)
Return-1;
String shorttext = 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 (plain Text );
Return matches. Count;
}
}