当前位置: 首页 > 工具软件 > JavaSWF2 > 使用案例 >

JAVA文件转PFD文件,pdf文件转swf文件

冷吉星
2023-12-01

swftools官网

http://www.swftools.org/

windows环境 swftools工具下载

http://www.swftools.org/swftools-0.9.0.exe

maven依赖

<dependency>
    <groupId>com.artofsolving</groupId>
    <artifactId>jodconverter</artifactId>
    <version>2.2.1</version>
	<exclusions>
		<exclusion>
			<groupId>org.slf4j</groupId>
			<artifactId>slf4j-api</artifactId>
		</exclusion>
	</exclusions>
</dependency>
<dependency>
	<groupId>org.slf4j</groupId>
	<artifactId>slf4j-api</artifactId>
	<version>1.7.7</version>
</dependency>

java代码


import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;

import com.artofsolving.jodconverter.DocumentConverter;
import com.artofsolving.jodconverter.openoffice.connection.OpenOfficeConnection;
import com.artofsolving.jodconverter.openoffice.connection.SocketOpenOfficeConnection;
import com.artofsolving.jodconverter.openoffice.converter.OpenOfficeDocumentConverter;

/**
 * 需要本机安装好openoffice,并且开启服务
 */
public class DocConverter {

	private String SWFTools_Windows = "C:/SWFTools/pdf2swf.exe ";
	/** 环境1:windows,2:LINUX */
	private int environment = 1;

	public int getEnvironment() {
		return environment;
	}

	public void setEnvironment(int environment) {
		this.environment = environment;
	}

	private String fileName;
	private File pdfFile;
	private File swfFile;
	private File docFile;
	private File odtFile;

	public DocConverter(String fileString) {
		ini(fileString);
	}

	public DocConverter(String fileString, int environment) {
		this.environment = environment;
		ini(fileString);
	}

	/**
	 * 初始化
	 */
	private void ini(String fileString) {
		try {
			fileName = fileString.substring(0, fileString.lastIndexOf("/"));
			docFile = new File(fileString);
			String s = fileString.substring(fileString.lastIndexOf("/") + 1, fileString.lastIndexOf("."));
			fileName = fileName + "/" + s;
			// 用于处理TXT文档转化为PDF格式乱码,获取上传文件的名称(不需要后面的格式)
			String txtName = fileString.substring(fileString.lastIndexOf("."));
			// 判断上传的文件是否是TXT文件
			if (txtName.equalsIgnoreCase(".txt")) {
				// 定义相应的ODT格式文件名称
				odtFile = new File(fileName + ".odt");
				// 将上传的文档重新copy一份,并且修改为ODT格式,然后有ODT格式转化为PDF格式
				this.copyFile(docFile, odtFile);
				pdfFile = new File(fileName + ".pdf"); // 用于处理PDF文档
			} else if (txtName.equals(".pdf") || txtName.equals(".PDF")) {
				pdfFile = new File(fileName + ".pdf");
			} else {
				pdfFile = new File(fileName + ".pdf");
			}
			swfFile = new File(fileName + ".swf");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 复制文件
	 * @param sourceFile 源文件
	 * @param targetFile 目标文件
	 * @throws Exception
	 */
	private void copyFile(File sourceFile, File targetFile) throws Exception {
		// 新建文件输入流并对它进行缓冲 
		FileInputStream input = new FileInputStream(sourceFile);
		BufferedInputStream inBuff = new BufferedInputStream(input);
		//  新建文件输出流并对它进行缓冲
		FileOutputStream output = new FileOutputStream(targetFile);
		BufferedOutputStream outBuff = new BufferedOutputStream(output);
		//  缓冲数组 
		byte[] b = new byte[1024 * 5];
		int len;
		while ((len = inBuff.read(b)) != -1) {
			outBuff.write(b, 0, len);
		}
		//  刷新此缓冲的输出流
		outBuff.flush();
		//  关闭流
		inBuff.close();
		outBuff.close();
		output.close();
		input.close();
	}

	/**
	 * 文件文件转为PDF文件
	 */
	private void doc2pdf() throws Exception {
		if (docFile.exists()) {
			if (!pdfFile.exists()) {
				// 默认会链接localhost:8100,本地hosts文件需配置localhost指向127.0.0.1
				OpenOfficeConnection connection = new SocketOpenOfficeConnection(8100);
				try {
					connection.connect();
					DocumentConverter converter = new OpenOfficeDocumentConverter(connection);
					converter.convert(docFile, pdfFile);
					// close the connection
					connection.disconnect();
				} catch (java.net.ConnectException e) {
					e.printStackTrace();
					throw e;
				} catch (com.artofsolving.jodconverter.openoffice.connection.OpenOfficeException e) {
					e.printStackTrace();
					throw e;
				} catch (Exception e) {
					e.printStackTrace();
					throw e;
				}
			}
		}
	}

	/**
	 * pdf文件转换成swf
	 */
	private void pdf2swf() throws Exception {
		if (!swfFile.exists()) {
			if (pdfFile.exists()) {
				Runtime r = Runtime.getRuntime();
				if (environment == 1) {// windows环境处理
					Process p = null;
					try {
						String[] cmd = new String[7];
						cmd[0] = SWFTools_Windows;
						cmd[1] = "-i";
						cmd[2] = pdfFile.getPath().trim();
						cmd[3] = "-o";
						cmd[4] = swfFile.getPath().trim();
						cmd[5] = "-s";
						cmd[6] = "languagedir=C:\\xpdf";
						p = Runtime.getRuntime().exec(cmd);

						InputStream is2 = p.getErrorStream();
						BufferedReader br2 = new BufferedReader(new InputStreamReader(is2));
						while (br2.readLine() != null)
							;
						p.waitFor();
						p.exitValue();
						// // 如果不读取流则targetFile.exists() 文件不存在,但是程序没有问题
						if (pdfFile.exists()) {
							pdfFile.delete();
						}
					} catch (Exception e) {
						e.printStackTrace();
						throw e;
					} finally {
						if (p != null) {
							p.destroy();
						}
						p = null;
					}
				} else if (environment == 2) {// linux环境处理
					try {
						r.exec("pdf2swf " + pdfFile.getPath() + " -o " + swfFile.getPath() + " -T 9");
					} catch (Exception e) {
						e.printStackTrace();
						throw new RuntimeException();
					}
				}
			}
		}

	}

	/**
	 * 转换主方法
	 */
	public boolean conver() {
		if (swfFile.exists()) {
			return true;
		}

		try {
			doc2pdf();
			pdf2swf();
		} catch (Exception e) {
			return false;
		}
		if (swfFile.exists()) {
			return true;
		} else {
			return false;
		}
	}

	/**
	 * 返回文件路径
	 */
	public String getswfPath() {
		if (swfFile.exists()) {
			return swfFile.getPath();
		} else {
			return "";
		}
	}

	/**
	 * 设置输出路径
	 */
	public void setOutputPath(String outputPath) {
		if (!outputPath.equals("")) {
			String realName = fileName.substring(fileName.lastIndexOf("/"), fileName.lastIndexOf("."));
			if (outputPath.charAt(outputPath.length()) == '/') {
				swfFile = new File(outputPath + realName + ".swf");
			} else {
				swfFile = new File(outputPath + realName + ".swf");
			}
		}
	}

	public static void main(String[] args) {
		DocConverter dc = new DocConverter("C:/Users/administrator/Desktop/a.txt", 1);
		dc.conver();
		System.out.println(dc.getswfPath());
	}

}

 

 类似资料: