pdf2swf

利用中间件,转换文档成PDF格式

(2011-01-04 09:50:44)
标签:

转换

pdf

it

分类: PDF

首先要用OPENOFFICE.然后安装1.6版JAVA。

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ConnectException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import com.artofsolving.jodconverter.DefaultDocumentFormatReg

istry;
import com.artofsolving.jodconverter.DocumentConverter;
import com.artofsolving.jodconverter.DocumentFormat;
import com.artofsolving.jodconverter.openoffice.connection.OpenOfficeConnection;
import com.artofsolving.jodconverter.openoffice.connection.SocketOpenOfficeConnection;
import com.artofsolving.jodconverter.openoffice.converter.OpenOfficeDocumentConverter;
import com.lowagie.text.Document;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfImportedPage;
import com.lowagie.text.pdf.PdfReader;
import com.lowagie.text.pdf.PdfWriter;


public class ConvertManager {
    
    public class FileModel {
        
        public FileModel(byte[] data, String type) {
            this.data = data;
            this.type = type;
        }
        
        private byte[] data;
        private String type;
        
        public byte[] getData() {
            return data;
        }
        public void setData(byte[] data) {
            this.data = data;
        }
        public String getType() {
            return type;
        }
        public void setType(String type) {
            this.type = type;
        }
        
    }
    
    private static final HashMap<String,String> validSuffixMap = new HashMap<String,String>();
    
    private static final String[] validSuffix = new String[]{"txt","doc","xls"};
    
    private OpenOfficeConnection connection = null;
    
    private DocumentConverter converter = null;
    
    private int port = 8100;
    
    static {
        for (String suffix : validSuffix) {
            validSuffixMap.put(suffix,"");
        }
    }
    
    public ConvertManager() {
        createConnectionObject();
    }
    
    public ConvertManager(int port) {
        this.port = port;
        createConnectionObject();
    }
    
    public void createConnectionObject() {
        connection =  new SocketOpenOfficeConnection(this.port);
    }
    
    public int getPort() {
        return port;
    }

    public void setPort(int port) {
        this.port = port;
    }
    
    
    public boolean testConnection() {
        boolean flag = false;
        try {
            if (connection == null) {
                connection = new SocketOpenOfficeConnection(port);
            }
            connection.connect();
            if(connection.isConnected()) {
                flag = true;
            }
            connection.disconnect();
        } catch (ConnectException e) {
            e.printStackTrace();
        }
        return flag;
    }
    
    
    public String getFileSuffix(String fileName) {
        
        String type = null;
        if (fileName != null && fileName.indexOf(".")>-1) {
            int index = fileName.lastIndexOf(".");
            type = fileName.substring(index+1);
        }
        return type;
    }
    
    public DocumentFormat getDocumentFormatByFileName(String fileName) {
        DocumentFormat df = null;
        String suffix = getFileSuffix(fileName);
        if (suffix != null) {
            df = getDocumentFormatBySuffix(suffix);
        }
        return df;
    }
    
    public DocumentFormat getDocumentFormatBySuffix(String suffix) {
        DocumentFormat df = null;
        if (suffix != null) {
            DefaultDocumentFormatRegistry registry = new DefaultDocumentFormatRegistry();
            df = registry.getFormatByFileExtension(suffix);
        }
        return df;
    }
    
    
    public void convert(String sourceFile, String newFile)
            throws ConnectException {
        connection.connect();
        DefaultDocumentFormatRegistry registry = new DefaultDocumentFormatRegistry();
        converter = new OpenOfficeDocumentConverter(connection, registry);
        File inputFile = new File(sourceFile);
        File outputFile = new File(newFile);
        converter.convert(inputFile, outputFile);
        connection.disconnect();
    }

    
    public void convert(String sourceFile,
            DocumentFormat inputDocumentFormat, String newFile,
            DocumentFormat outputDocumentFormat) throws ConnectException {
        connection.connect();
        File inputFile = new File(sourceFile);
        File outputFile = new File(newFile);
        DefaultDocumentFormatRegistry registry = new DefaultDocumentFormatRegistry();
        converter = new OpenOfficeDocumentConverter(connection, registry);
        converter.convert(inputFile, inputDocumentFormat, outputFile, outputDocumentFormat);
        connection.disconnect();
    }

    
    public void convert(String sourceFile, String newFile,
            DocumentFormat outputDocumentFormat) throws ConnectException {
        connection.connect();
        File inputFile = new File(sourceFile);
        File outputFile = new File(newFile);
        DefaultDocumentFormatRegistry registry = new DefaultDocumentFormatRegistry();
        converter = new OpenOfficeDocumentConverter(connection, registry);
        converter.convert(inputFile, outputFile, outputDocumentFormat);
        connection.disconnect();
    }
    
    
    public void convert(File sourceFile, File newFile) throws ConnectException {
        connection.connect();
        DefaultDocumentFormatRegistry registry = new DefaultDocumentFormatRegistry();
        converter = new OpenOfficeDocumentConverter(connection, registry);
        converter.convert(sourceFile, newFile);
        connection.disconnect();
    }
    
    
    public void convert(InputStream inputStream,
            String inputType, OutputStream outputStream,
            String outputType) throws ConnectException {
        connection.connect();
        DefaultDocumentFormatRegistry registry = new DefaultDocumentFormatRegistry();
        converter = new OpenOfficeDocumentConverter(connection, registry);
        
        DocumentFormat inDf = registry.getFormatByFileExtension(inputType);
        DocumentFormat outDf = registry.getFormatByFileExtension(outputType);
        
        converter.convert(inputStream, inDf, outputStream, outDf);
        connection.disconnect();
    }
    
    
    public void convert(InputStream inputStream,
            DocumentFormat inputDocumentFormat, OutputStream outputStream,
            DocumentFormat outputDocumentFormat) throws ConnectException {
        connection.connect();
        DefaultDocumentFormatRegistry registry = new DefaultDocumentFormatRegistry();
        converter = new OpenOfficeDocumentConverter(connection, registry);
        converter.convert(inputStream, inputDocumentFormat, outputStream,
                outputDocumentFormat);
        connection.disconnect();
    }
    
    
    public byte[] mergePdfFiles(List<byte[]> pdfInputs) {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        
            Document document = new Document();
            PdfWriter writer;
            try {
                writer = PdfWriter.getInstance(document, baos);
                document.open();
                PdfContentByte cb = writer.getDirectContent();
                PdfImportedPage newPage;
                PdfReader reader;
    
                for(byte[] b : pdfInputs) {
                    reader = new PdfReader(b);
                    int iPageNum = reader.getNumberOfPages();
                    for (int i = 1; i <= iPageNum; i++) {
                        document.newPage();
                        newPage = writer.getImportedPage(reader, i);
                        cb.addTemplate(newPage, 0, 0);
                    }
                }
                document.close();
                writer.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
            return baos.toByteArray();
    }
    
    public byte[] convertAndMerage(List<FileModel> list) {
        byte[] outputData = null;
        List<byte[]> byteList = new ArrayList<byte[]>();
        for(FileModel fModel : list) {
            ByteArrayInputStream bais = new ByteArrayInputStream(fModel.data);
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            try {
                convert(bais, fModel.type, baos, "pdf");
                byteList.add(baos.toByteArray());
            } catch (ConnectException e) {
                e.printStackTrace();
            }
        }
        outputData = mergePdfFiles(byteList);
        return outputData;
    }
    public static void main(String[] args) {
       //写测试
        test1();
    }

  
    private static void test1() {
        ConvertManager cm = new ConvertManager();
        if (cm.testConnection()) {
            //test 1
            String sourceFile = "C://11.xls";
            String newFile = "C://11.pdf";
            try {
                FileInputStream fis = new FileInputStream(new File(sourceFile));
                DocumentFormat inDf = cm.getDocumentFormatByFileName(sourceFile);
                DocumentFormat outDf = cm.getDocumentFormatBySuffix("pdf");
               
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                try {
                    cm.convert(fis, inDf, baos, outDf);
                } catch (ConnectException e) {
                    e.printStackTrace();
                }
               
                FileOutputStream fos = new FileOutputStream(newFile);
                fos.write(baos.toByteArray());
               
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        } else {
            System.out.println("服务器连接失败。检查端口"+cm.getPort());
        }
       
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值