Java tutorial
package com.whty.transform.common.utils; import java.awt.image.BufferedImage; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.util.ArrayList; import java.util.List; import javax.imageio.ImageIO; import org.apache.commons.io.FileUtils; import org.apache.log4j.Logger; import org.jpedal.PdfDecoder; import org.jpedal.exception.PdfException; 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; import com.aspose.cells.Workbook; import com.aspose.words.Document; import com.aspose.words.License; import com.aspose.words.SaveFormat; import com.itextpdf.text.DocumentException; import com.itextpdf.text.Font; import com.itextpdf.text.pdf.BaseFont; import com.itextpdf.text.pdf.PdfContentByte; import com.itextpdf.text.pdf.PdfCopy; import com.itextpdf.text.pdf.PdfDictionary; import com.itextpdf.text.pdf.PdfImportedPage; import com.itextpdf.text.pdf.PdfName; import com.itextpdf.text.pdf.PdfObject; import com.itextpdf.text.pdf.PdfReader; import com.itextpdf.text.pdf.PdfStamper; import com.whty.transform.common.constants.Constants; import com.whty.transform.common.domain.VideoFileInfo; /** * ? * * @author yangfan * */ public class TransformUtils { private static final Logger LOGGER = Logger.getLogger(TransformUtils.class); private static String transFileName = SysConf.getString("path.transformFileName"); private static String openOfficeHost = SysConf.getString("openoffice.host"); private static String swfToolsPath = SysConf.getString("swfTools.path"); private static String ffmpegPath = SysConf.getString("ffmpeg.path"); private static int openOfficePort = Integer.valueOf(SysConf.getString("openoffice.port")); private static final int pageNumber = 1; private static DocumentConverter converter; static { try { loadLicense(); OpenOfficeConnection connection = new SocketOpenOfficeConnection(openOfficeHost, openOfficePort); connection.connect(); converter = new OpenOfficeDocumentConverter(connection); } catch (Exception e) { LOGGER.error(e.getMessage(), e); } } public static boolean wordToPDF(String docpath) { try { Document doc = new Document(SysConf.getString("path.input") + docpath); File pdfPath = new File(SysConf.getString("path.output") + docpath + "/pdf/"); if (!pdfPath.exists()) { pdfPath.mkdirs(); } doc.save(pdfPath.getAbsolutePath() + "/" + transFileName + ".pdf", SaveFormat.PDF); return true; } catch (Exception e) { LOGGER.error(e.getMessage(), e); } return false; } public static boolean wordToPng(String docpath) { try { Document doc = new Document(SysConf.getString("path.input") + docpath); File pngPath = new File(SysConf.getString("path.output") + docpath + "/png/"); if (!pngPath.exists()) { pngPath.mkdirs(); } doc.save(pngPath.getAbsolutePath() + "/" + transFileName + ".png", SaveFormat.PNG); return true; } catch (Exception e) { LOGGER.error(e.getMessage(), e); } return false; } public static boolean pdfTopdf(String docpath) { File pdfPath = new File(SysConf.getString("path.output") + docpath + "/pdf/"); if (!pdfPath.exists()) { pdfPath.mkdirs(); } // pdf? try { PdfReader reader = new PdfReader(SysConf.getString("path.input") + docpath); com.itextpdf.text.Document document = new com.itextpdf.text.Document(reader.getPageSize(1)); PdfCopy copy = new PdfCopy(document, new FileOutputStream(pdfPath.getAbsoluteFile() + "/" + transFileName + ".pdf")); document.open(); for (int i = 1; i <= reader.getNumberOfPages(); i++) { document.newPage(); PdfImportedPage page = copy.getImportedPage(reader, i); copy.addPage(page); } document.close(); return true; } catch (IOException e) { LOGGER.error(e.getMessage(), e); } catch (DocumentException e) { LOGGER.error(e.getMessage(), e); } return false; } public static boolean txtorJavaToPDF(String txtpath) { try { File pdfPath = new File(SysConf.getString("path.output") + txtpath + "/pdf/"); String docpath = txtpath.replace(".txt", ".doc").replace(".java", ".doc"); File tempDoc = new File(SysConf.getString("path.input") + docpath); FileUtils.copyFile(new File(SysConf.getString("path.input") + txtpath), tempDoc); Document doc = new Document(SysConf.getString("path.input") + docpath); if (!pdfPath.exists()) { pdfPath.mkdirs(); } doc.save(pdfPath.getAbsolutePath() + "/" + transFileName + ".pdf", SaveFormat.PDF); tempDoc.delete(); return true; } catch (Exception e) { LOGGER.error(e.getMessage(), e); } return false; } public static boolean txtorJavaToPNG(String txtpath) { try { File pdfPath = new File(SysConf.getString("path.output") + txtpath + "/png/"); String docpath = txtpath.replace(".txt", ".doc").replace(".java", ".doc"); File tempDoc = new File(SysConf.getString("path.input") + docpath); FileUtils.copyFile(new File(SysConf.getString("path.input") + txtpath), tempDoc); Document doc = new Document(SysConf.getString("path.input") + docpath); if (!pdfPath.exists()) { pdfPath.mkdirs(); } doc.save(pdfPath.getAbsolutePath() + "/" + transFileName + ".png", SaveFormat.PNG); tempDoc.delete(); return true; } catch (Exception e) { LOGGER.error(e.getMessage(), e); } return false; } public static boolean excelToHTML(String excelPath) { try { Workbook workBook = new Workbook(SysConf.getString("path.input") + excelPath); File htmlPath = new File(SysConf.getString("path.output") + excelPath + "/html/"); if (!htmlPath.exists()) { htmlPath.mkdirs(); } workBook.save(htmlPath.getAbsolutePath() + "/" + transFileName + ".html", com.aspose.cells.SaveFormat.HTML); return true; } catch (Exception e) { LOGGER.error(e.getMessage(), e); } return false; } public static boolean officeToPDF(String officePath) { File officeFile = new File(SysConf.getString("path.input") + officePath); File pdfPath = new File(SysConf.getString("path.output") + officePath + "/pdf/"); if (!pdfPath.exists()) { pdfPath.mkdirs(); } File pdfFile = new File(pdfPath.getAbsolutePath() + "/" + transFileName + ".pdf"); try { converter.convert(officeFile, pdfFile); return true; } catch (Exception e) { LOGGER.error(e.getMessage(), e); } return false; } public static void pdfToPNG(String pdfPath, String pngPath) { PdfDecoder decode_pdf = new PdfDecoder(true); try { decode_pdf.openPdfFile(pdfPath); BufferedImage img = decode_pdf.getPageAsImage(pageNumber); ImageIO.write(img, "png", new File(pngPath)); decode_pdf.closePdfFile(); } catch (PdfException e) { LOGGER.error(e.getMessage(), e); } catch (IOException e) { LOGGER.error(e.getMessage(), e); } } public static boolean pdfToSWF(String officePath) { File pdfFile = new File(SysConf.getString("path.output") + officePath + "/pdf/" + transFileName + ".pdf"); if (!pdfFile.exists()) { LOGGER.info("?pdf?swf"); return false; } File swfPath = new File(SysConf.getString("path.output") + officePath + "/swf/"); if (!swfPath.exists()) { swfPath.mkdirs(); } File pngFile = new File(SysConf.getString("path.output") + officePath + "/png/"); if (!pngFile.exists()) { pngFile.mkdirs(); } pdfToPNG(pdfFile.getAbsolutePath(), pngFile.getAbsolutePath() + "/" + transFileName + ".png"); List<String> cmd = new ArrayList<String>(); cmd.add(swfToolsPath); cmd.add("-z"); cmd.add("-s"); if (pdfFile.length() <= 5 * 1024 * 1024) { cmd.add("flashversion=9"); } else { cmd.add("poly2bitmap"); } cmd.add(pdfFile.getAbsolutePath()); cmd.add("-o"); cmd.add(swfPath.getAbsolutePath() + "/" + transFileName + ".swf"); CommandExecuter command = new CommandExecuter(); int exeValut = command.execute(cmd, null, null); if (exeValut == -1) { return false; } return true; } public static boolean videoToFlv(String videoPath, boolean isPreview) { File flvPath = new File(SysConf.getString("path.output") + videoPath + "/flv/"); if (!flvPath.exists()) { flvPath.mkdirs(); } List<String> cmd = new ArrayList<String>(); cmd.add(ffmpegPath); // ?? cmd.add("-i"); cmd.add(SysConf.getString("path.input") + videoPath); /** * (??bit/s?kb/s)??-ac??? 192kbps?96? * ? ????160kbps80 */ cmd.add("-ab"); cmd.add("64"); if (isPreview) { cmd.add("-ss"); cmd.add("1"); // cmd.add("-itsoffset"); // cmd.add("00:01:03.80"); } /** * ?1??2 ???TVrip?1???? */ cmd.add("-ac"); cmd.add("1"); cmd.add("-ar"); /** * (??Hz) */ cmd.add("22050"); /** * (bits/s)ffmpegVBR? */ cmd.add("-b"); cmd.add("230"); /** * (720x480) */ cmd.add("-s"); cmd.add("720x480"); /** * (4:3, 16:9 or 1.3333, 1.7777) */ cmd.add("-qscale"); cmd.add("10"); /** * (fps) ?????1529.97 */ cmd.add("-r"); cmd.add("29.97"); /** * ?1.***?????? */ cmd.add("-y"); cmd.add(flvPath.getAbsolutePath() + "/" + transFileName + ".flv"); CommandExecuter command = new CommandExecuter(); Constants.videoFils.put(videoPath, new VideoFileInfo()); int exeValut = command.execute(cmd, null, videoPath); if (exeValut == -1) { return false; } return true; } public static boolean videoToFlv(String videoPath) { return videoToFlv(videoPath, false); } public static boolean videoToPNG(String videoPath) { File pngPath = new File(SysConf.getString("path.output") + videoPath + "/png/"); if (!pngPath.exists()) { pngPath.mkdirs(); } List<String> cmd = new ArrayList<String>(); cmd.add(ffmpegPath); cmd.add("-i"); cmd.add(SysConf.getString("path.input") + videoPath); /** * ? */ cmd.add("-f"); cmd.add("image2"); /** * (s) */ cmd.add("-ss"); cmd.add("1"); /** * (320x240) */ cmd.add("-s"); cmd.add("720x480"); /** * ?(frame) */ cmd.add("-vframes"); cmd.add("1"); cmd.add("-y"); cmd.add(pngPath.getAbsolutePath() + "/" + transFileName + ".png"); CommandExecuter command = new CommandExecuter(); int exeValut = command.execute(cmd, null, null); if (exeValut == -1) { return false; } return true; } public static void loadLicense() throws Exception { License license = new License(); InputStream is = TransformUtils.class.getResourceAsStream("/license.xml"); if (is == null) return; license.setLicense(is); is.close(); } /** * @param args */ public static void main(String[] args) { System.out.println(TransformUtils.pdfToSWF("test6.pdf")); } }