1. 程式人生 > 其它 >(JAVA)word轉換pdf最簡方法!!!

(JAVA)word轉換pdf最簡方法!!!

技術標籤:JAVA

TESTTOPDF PDF = new TESTTOPDF();
Map<String, Object> dataMap = new HashMap<String, Object>();
//userObj 根據word自定義格式中給欄位命名的字首
dataMap.put(“userObj”, mx);
//userList根據word自定義格式中給欄位命名的字首,迴圈獲取列表
dataMap.put(“userList”, userList);
try {
//word輸入路徑
String inFileWordPath = “E:/test1.doc”;

//pdf輸入路徑
String inFilePdfPath = “E://test2.pdf”;
//pdf輸出路徑
String outFilePdfPath = “E://test3.pdf”;

		// 建立word
		ParseWordData parseWordData = new ParseWordData();
		CreateWord.generateWord(dataMap,inFileWordPath);
		// word轉換pdf
		CreateWordToPdf.word2Pdf(inFileWordPath, inFilePdfPath);
		// 加簽
		String path = inFilePdfPath;
		File file = new File(path);
		FileInputStream inputFile = new FileInputStream(file);
		byte[] buffer = new byte[(int) file.length()];
		inputFile.read(buffer);
		inputFile.close();
		String strBase64 = new BASE64Encoder().encode(buffer);
		PDF.base64StringToPdf(strBase64, outFilePdfPath);
	} catch (Exception e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}

1.TESTTOPDF.java:
package com.dcits.utils;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

/*
*

  • 轉換成pdf檔案

*/
public class TESTTOPDF {
public static void base64StringToPdf(String base64Content,String filePath){
BASE64Decoder decoder = new BASE64Decoder();
BufferedInputStream bis = null;
FileOutputStream fos = null;
BufferedOutputStream bos = null;

    try {
        byte[] bytes = decoder.decodeBuffer(base64Content);//base64編碼內容轉換為位元組陣列
        ByteArrayInputStream byteInputStream = new ByteArrayInputStream(bytes);
        bis = new BufferedInputStream(byteInputStream);
        File file = new File(filePath);
        File path = file.getParentFile();
        if(!path.exists()){
            path.mkdirs();
        }
        fos = new FileOutputStream(file);
        bos = new BufferedOutputStream(fos);

        byte[] buffer = new byte[1024];
        int length = bis.read(buffer);
        while(length != -1){
            bos.write(buffer, 0, length);
            length = bis.read(buffer);
        }
        bos.flush();
    } catch (Exception e) {
        e.printStackTrace();
    }finally{
        closeStream(bis, fos, bos);
    }
}

private static void closeStream(BufferedInputStream bis,
		FileOutputStream fos, BufferedOutputStream bos) {
	// TODO Auto-generated method stub
	try {
		bis.close();
		fos.close();
		bos.close();
	} catch (IOException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
}

}

2.CreateWord.java:
package com.dcits.utils;

import freemarker.template.Configuration;
import freemarker.template.Template;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.Map;

public class CreateWord {
/**
* 使用FreeMarker自動生成Word文件
*
* @param dataMap
* 生成Word文件所需要的資料
* @param fileName
* 生成Word文件的全路徑名稱
*/
public static void generateWord(Map<String, Object> dataMap, String fileName)
throws Exception {
// 設定FreeMarker的版本和編碼格式
Configuration configuration = new Configuration();
configuration.setDefaultEncoding(“UTF-8”);

	// 設定FreeMarker生成Word文件所需要的模板的路徑
	configuration.setClassForTemplateLoading(CreateWord.class, "/properties");
	// 設定FreeMarker生成Word文件所需要的模板
	Template t = configuration.getTemplate("test4.xml", "UTF-8");
	
	
	// 建立一個Word文件的輸出流
	Writer out = new BufferedWriter(new OutputStreamWriter(
			new FileOutputStream(new File(fileName)), "UTF-8"));
	// FreeMarker使用Word模板和資料生成Word文件
	t.process(dataMap, out);
	out.flush();
	out.close();
}

}

附:
1.轉換為xml後,欄位命名圖例:
在這裡插入圖片描述
在這裡插入圖片描述