PDF与word互相转换

第一次写博客,一个刚刚入行Java开发的小白,只是希望能把自己平时 遇到的问题 和  解决 方法 分享给遇到同样问题的人,肯定会有不足的地方,如果大佬们看到希望能多多指正,谢谢啦

pdf转换word

用到的jar是pdfbox

String pdfFile = "C:/Users/FengJQ/Desktop/****.pdf";
PDDocument doc = PDDocument.load(new File(pdfFile));
int pagenumber = doc.getNumberOfPages();
pdfFile = pdfFile.substring(0, pdfFile.lastIndexOf("."));
String fileName = pdfFile + ".doc";
File file = new File(fileName);
if (!file.exists()) {
    file.createNewFile();

}
FileOutputStream fos = new FileOutputStream(fileName);
Writer writer = new OutputStreamWriter(fos, "UTF-8");
PDFTextStripper stripper = new PDFTextStripper();
stripper.setSortByPosition(true);// 排序
stripper.setStartPage(1);// 设置转换的开始页
stripper.setEndPage(pagenumber);// 设置转换的结束页
stripper.writeText(doc, writer);
writer.close();
doc.close();

word转pdf

有两种方法,一种是利用jacob架包,一种是OpenOffice软件

第一种,利用jacob架包

/**
	 * 
	 * @param source  要转换的word文件
	 * @param target  要转换成为的pdf文件
	 * @return
	 */
	public static boolean word2pdf(String source, String target) {
		System.out.println("Word转PDF开始启动...");
		long start = System.currentTimeMillis();
		ActiveXComponent app = null;
		try {
			app = new ActiveXComponent("Word.Application");
			app.setProperty("Visible", false);
			Dispatch docs = app.getProperty("Documents").toDispatch();
			System.out.println("打开文档:" + source);
			Dispatch doc = Dispatch.call(docs, "Open", source, false, true).toDispatch();
			System.out.println("转换文档到PDF:" + target);
			File tofile = new File(target);
			if (tofile.exists()) {
				tofile.delete();
			}
			Dispatch.call(doc, "SaveAs", target, wdFormatPDF);
			Dispatch.call(doc, "Close", false);
			long end = System.currentTimeMillis();
			System.out.println("转换完成,用时:" + (end - start) + "ms");
			return true;
		} catch (Exception e) {
			System.out.println("Word转PDF出错:" + e.getMessage());
			return false;
		} finally {
			if (app != null) {
				app.invoke("Quit", wdDoNotSaveChanges);
			}
		}
	}

第二种方法,,需要先安装openoffice软件

/**
	 * @param dirs doc转换文件夹,批量转换
	 */
	public static void word2Pdf(String dirs){
		 File dir = new File(dirs); 
		 File[] files = dir.listFiles(new WordFilenameFilter()); 
		   //遍历文件夹方式
		    if (files == null||files.length==0){
		    	throw new NullPointerException("该路径下没有doc文件或者docx文件");
		    }
		    for (int i = 0; i < files.length; i++) { 
		            String strFileName = files[i].getAbsolutePath().toLowerCase(); 
		            TestThread t1 = new WordToPDF.TestThread();
		            //输入文件名
		           t1.setInputFile(new File(strFileName));
		            //获得“.”前面的文件名并将其输入为PDF
		    		t1.setOutputFile(new File(strFileName.substring(0,strFileName.indexOf("."))+".pdf"));
		            t1.start();
		    }
    } 



	/**
	 * @param orgfileName 原始的word文件名
	 * @param descFileName 生成的pdf文件名
	 */
	public static void word2Pdf(String orgfileName, String descFileName) {
		if(!isWord(orgfileName)||descFileName==null){
			throw new IllegalArgumentException("原始word文件名不是word文档,或者descFileName为空");
		}else{
			TestThread t1 = new WordToPDF.TestThread();
			// 输入文件名
			t1.setInputFile(new File(orgfileName));
			// 获得"."前面的文件名并将其输入为PDF
			t1.setOutputFile(new File(descFileName.substring(0, descFileName
					.indexOf("."))
					+ ".pdf"));
			t1.start();
		}
	}
	static class WordFilenameFilter implements FilenameFilter{
		@Override
		//只转换word文档
		public boolean accept(File dir, String name) {
			return isWord(name);
		}
	}
	/**
	 * @param name 文件名
	 * @return 判断是否为doc或者word文档
	 */
	private static boolean isWord(String name){
		return name.endsWith(".doc")||name.endsWith(".docx");
		//return true;
	}


util
public class WordToPDF {

	public void docToPdf(File inputFile, File outputFile) {
		// 启动服务  
		String OpenOffice_HOME = "E:/Program Files (x86)/OpenOffice 4";//这里是OpenOffice的安装目录
		if (OpenOffice_HOME.charAt(OpenOffice_HOME.length() - 1) != '/') {
			OpenOffice_HOME += "/";
		}
		Process pro = null;
		OpenOfficeConnection connection = null;
		// 启动OpenOffice的服务
		String command = OpenOffice_HOME
				+ "program/soffice.exe -headless -accept=\"socket,host=127.0.0.1,port=8100;urp;\"";
		// connect to an OpenOffice.org instance running on port 8100

		try {
			pro = Runtime.getRuntime().exec(command);
			connection = new SocketOpenOfficeConnection(8100);
			connection.connect();

			// convert
			DocumentConverter converter = new OpenOfficeDocumentConverter(connection);
			System.out.println(inputFile + "=" + outputFile);

			converter.convert(inputFile, outputFile);
		} catch (Exception ex) {
			System.out.println("启动OpenOffice的服务");
			ex.printStackTrace();

		} finally {
			// close the connection
			if (connection != null) {
				connection.disconnect();
				connection = null;
			}
			pro.destroy();
		}
		System.out.println("生成: " + outputFile.getName());
	}

	// 生产pdf线程
	static class TestThread extends Thread {
		private File inputFile;
		private File outputFile;

		public void run() {
			WordToPDF t = new WordToPDF();
			t.docToPdf(inputFile, outputFile);
			System.out.println(outputFile.getName() + "文件已生成");
		}

		public void setInputFile(File inputFile) {
			this.inputFile = inputFile;
		}

		public void setOutputFile(File outputFile) {
			this.outputFile = outputFile;
		}

	}

}

 

你可能感兴趣的:(PDF与word互相转换)