---
title: "pdf转swf"
author: "sd1992585"
date: "2010-09-19T08:59:51Z"
tags: ["Blog", "C", "C#", "C++"]
categories: ["编程语言"]
slug: "pdf-swf"
draft: false
lastmod: "2010-09-19T08:59:51Z"
id: 766904
url: "https://sd1992585.iteye.com/blog/766904"
---

通过代码将PDF转换成SWF来说，现在比较常用的一种方式就是利用SWFTools工具中的pdf2swf（[http://www.swftools.org/](http://www.swftools.org/)）。这个工具还是比较好用的。转换成的SWF文件质量也不错。

/\**

Java代码

01. * PDF转SWF工具
02. * @author tangs
03. \*
04. \*/
05. **public** **class** Converter {
06. **public** **static** **int** convertPDF2SWF(String sourcePath, String destPath, String fileName) **throws** IOException {
07. //目标路径不存在则建立目标路径
08. File dest = **new** File(destPath);
09. **if** (!dest.exists()) dest.mkdirs();
10. //源文件不存在则返回
11. File source = **new** File(sourcePath);
12. **if** (!source.exists()) **return** 0;
13. //调用pdf2swf命令进行转换
14. String command = "D:\\\\Program Files\\\\SWFTools\\\\pdf2swf.exe" + " -o \\"" + destPath + "\\\\" + fileName + "\\"  &lt;SPAN style="COLOR: #ff0000"&gt;-s languagedir=D:\\\\xpdf\\\\xpdf-chinese-simplified&lt;/SPAN&gt; -s flashversion=9 \\"" + sourcePath + "\\"";
15. Process pro = Runtime.getRuntime().exec(command);
16. BufferedReader bufferedReader = **new** BufferedReader(**new** InputStreamReader(pro.getInputStream()));
17. **while** (bufferedReader.readLine() != **null**);
18. **try** {
19. pro.waitFor();
20. } **catch** (InterruptedException e) {
21. // TODO Auto-generated catch block
22. e.printStackTrace();
23. }
24. **return** pro.exitValue();
25. }
26. **public** **static** **void** main(String \[]args) **throws** IOException {
27. String sourcePath = "c:\\\\test.pdf";
28. String destPath = "c:\\\\";
29. String fileName = "test.swf";
30. Converter.convertPDF2SWF(sourcePath, destPath, fileName);
31. }
32. }

```
 * PDF转SWF工具
 * @author tangs
 *
 */
public class Converter {
	public static int convertPDF2SWF(String sourcePath, String destPath, String fileName) throws IOException {
		//目标路径不存在则建立目标路径
		File dest = new File(destPath);
		if (!dest.exists()) dest.mkdirs();
		
		//源文件不存在则返回
		File source = new File(sourcePath);
		if (!source.exists()) return 0;
		
		//调用pdf2swf命令进行转换
		String command = "D:\\Program Files\\SWFTools\\pdf2swf.exe" + " -o \"" + destPath + "\\" + fileName + "\"  -s languagedir=D:\\xpdf\\xpdf-chinese-simplified -s flashversion=9 \"" + sourcePath + "\"";
		
		Process pro = Runtime.getRuntime().exec(command);
		
		BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(pro.getInputStream()));
		while (bufferedReader.readLine() != null); 
		
		try {
			pro.waitFor();
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return pro.exitValue();
		
	}
	
	public static void main(String []args) throws IOException {
		String sourcePath = "c:\\test.pdf";
		String destPath = "c:\\";
		String fileName = "test.swf";
		Converter.convertPDF2SWF(sourcePath, destPath, fileName);
	}
}
```

 

    就这么简单的几行代码就可以了。但是在程序中遇到中文就会出现意想不到的情况，这个也不例外。在转换中，我发现有些中文PDF文件转换后会出现乱码的现象，因此这里还要处理一下乱码的问题。看到上面代码中红色的一段了吗？这就是解决乱码的方法。这个方法是参考了[http://hi.baidu.com/xwx520/blog/item/1d0c423885b392fab311c72e.html](http://hi.baidu.com/xwx520/blog/item/1d0c423885b392fab311c72e.html)这篇文章，感谢作者。

1.下载XPDF：ftp://ftp.foolabs.com/pub/xpdf/xpdf-chinese-simplified.tar.gz，并解压到xpdf-chinese-simplified目录下。

 

2.下载字体：[http://blog.pjoke.com/wp-content/uploads/2009/02/font.zip](http://blog.pjoke.com/wp-content/uploads/2009/02/font.zip)，并解压到xpdf-chinese-simplified/CMap目录下。

 

3.修改xpdf-chinese-simplified目录下的add-to-xpdfrc文件。将里面的路径设为自己的路径：

![](http://dl.iteye.com/upload/picture/pic/50429/1dddc5a0-4c66-339c-91f6-93ca7697caa3.jpg)

 

4.参照上面的代码，在调用pdf2swf命令中加入“ -s languagedir=D:\\\\xpdf\\\\xpdf-chinese-simplified ”参数。

 

这样乱码的问题就解决了。