前言
在iText 制作PDF這篇博文中只是簡單的介紹了如何制作PDF,為了能讓PDF在Web頁面中顯示,我還需要通過SWFTools工具將PDF文件轉換為SWF文件,然后通過SWF文件顯示在Web網頁中,本次主要是實踐SWFTools工具的簡單使用,可以在http://www.swftools.org/download.html網頁中下載工具,並安裝。但是要注意下載的版本,我是在Win7系統下開發的,所以安裝的工具就是如下圖所示
安裝完成后會生成pdf2swf.exe。並預先在PDF文件夾添加一個文件。
,此PDF文件也是由上節中生成的。
第一步
我先是創建了一個WinForm窗體應用程序,然后在配置文件中配置了兩個路徑,一個是PDF文件路徑,另外一個是生成的SWF文件的路徑
App.Config配置文件代碼
<?xml version=
"1.0"
?>
<configuration>
<appSettings>
<!--存放Pdf的目錄-->
<add key=
"PdfPath"
value="D:\PdfFiles\"/>
<!--存放轉換過后的Swf的目錄-->
<add key=
"SwfPath"
value="D:\SwfFiles\"/>
</appSettings>
<startup>
<supportedRuntime version=
"v4.0"
sku=
".NETFramework,Version=v4.0"
/>
</startup>
</configuration>
|
第二步
需要在PDF文件夾下進行尋找PDF文件
//掃描PDF文件
private
string
SearchPdf()
{
string
pdfFile =
""
;
string
pdfPath = AppConfiguration.PdfPath;
if
(!Directory.Exists(pdfPath))
{
Directory.CreateDirectory(pdfPath);
}
string
[] files = Directory.GetFiles(pdfPath);
for
(
int
i = 0; i < files.Length; i++)
{
if
(files[i].EndsWith(
".pdf"
))
{
pdfFile = files[i];
break
;
}
}
return
pdfFile;
}
|
先是取到配置文件的PDF文件夾,如果沒有此文件夾,則需要創建一個,然后進行查找該文件夾下的PDF類型的文件。
第三步
根據PDF文件夾,來查找或者生成相應的SWF文件夾
//獲取SWF存放目錄
private
string
GetSavePathFromName(
string
pdfFile)
{
string
swfBasePath = AppConfiguration.SwfPath;
string
swfPath = swfBasePath + pdfFile.Split(
'\\'
).Last().Replace(
".pdf"
,
""
) +
"\\"
;
if
(!Directory.Exists(swfPath))
{
Directory.CreateDirectory(swfPath);
}
return
swfPath;
}
|
第四步
執行將PDF文件通過pdf2swf.exe生成SWF文件。
private
void
Execute(
string
cmd,
string
args)
{
using
(Process p =
new
Process())
{
p.StartInfo.FileName = cmd;
p.StartInfo.Arguments = args;
p.StartInfo.UseShellExecute =
false
;
//此類提供的標准output流只有2k,不要重定向
p.StartInfo.RedirectStandardOutput =
false
;
p.StartInfo.CreateNoWindow =
true
;
p.Start();
p.PriorityClass = ProcessPriorityClass.Normal;
p.WaitForExit();
}
}
|
string
cmd =
"pdf2swf.exe"
;
string
args =
" -t \""
+ pdfFile +
"\" -o \""
+ savePath + pdfFile.Split(
'\\'
).Last().Replace(
".pdf"
,
""
)
+
"%.swf\" -s drawonlyshapes -s flashversion=9"
;
Execute(cmd, args);
|
那么執行后在相應的文件夾中生成文件如下。
到此簡單的將PDF文件轉換為SWF文件就成功了。
當然海域很重要的一步就是如何調用pdf2swf.exe文件,這里我是將此文件與winform的exe文件放在同一個目錄下進行調用的。