いつも参考にさせて頂いており、大変お世話になっています。
掲題の通り、Javaバッチで「Excelファイル」から「PDFファイル」へ変換する際に、詳細な設定が正しく反映しないのです。
【開発環境】
OS:Windows7
IDE:Eclipse(ver.4.3)
言語:jdk 1.7
ツール:Apache OpenOffice(Ver.4.1.3)、POI(Ver.3.16)、jodConverter(Ver.2.2.2)
【Javaソース】
public static Boolean conversionExcelToPDFWithOption ( String _excelFile, String _pdfFile ) throws IOException { boolean _return = false; InputStream excelData = new FileInputStream ( _excelFile ); Workbook wb = null; File file = new File( _excelFile ); if ( file.getPath().endsWith( ".xls" ) ) { wb = new HSSFWorkbook( excelData ); } else if ( file.getPath().endsWith( ".xlsx" ) ) { wb = new XSSFWorkbook( excelData ); } else { System.out.println( "当該ファイルの拡張子が、エクセルの拡張子(\".xls\"、\".xlsx\")ではありませんので、処理を中止します。" ); System.exit( 16 ); // 処理中止!!! } // PDF用の customPdfFormat インスタンス作成 DocumentFormat customPdfFormat = new DefaultDocumentFormatRegistry().getFormatByFileExtension( ConversionUtil.isExtension( _pdfFile ) ); Map<String, Object> pdfOptions = new HashMap<String, Object>(); //zoom指定 pdfOptions.put("Magnification", new Integer(0)); // ExcelならSPREADSHEET、wordならDRAWING、pptならPRESENTATION、テキストならTEXTかと思われる customPdfFormat.setExportOption( DocumentFamily.SPREADSHEET, "FilterData", pdfOptions ); // バッファ内にExcelの内容を書き出す ByteArrayOutputStream buff = new ByteArrayOutputStream(); // PDF変換時に渡すInputStreamを用意 ByteArrayInputStream in = null; // PDFの出力先を開く FileOutputStream out=null; // OpenOfficeに接続(localhostの8100番ポート) SocketOpenOfficeConnection con = new SocketOpenOfficeConnection( 8100 ); try { wb.write( buff ); in = new ByteArrayInputStream( buff.toByteArray() ); try { out = new FileOutputStream( _pdfFile ); con.connect(); // ExcelからPDFへ変換 DocumentConverter converter = new OpenOfficeDocumentConverter( con ); converter.convert( in, new DefaultDocumentFormatRegistry().getFormatByFileExtension( ConversionUtil.isExtension( _excelFile ) ), out, customPdfFormat ); // 正常終了。 _return = true; } catch ( Exception e ) { e.printStackTrace(); } finally { wb.close(); con.disconnect(); if ( null != out ) { try { out.flush(); out.close(); } catch ( IOException ioe ) { // ignore ioe.printStackTrace(); } } } } catch ( Exception e ) { e.printStackTrace(); } finally { if ( null != in ) { try { in.close(); } catch ( Exception e2 ) { e2.printStackTrace(); } } try { buff.close(); // OpenOfficeから切断 } catch ( Exception e3 ) { e3.printStackTrace(); } } return _return; } public static String isExtension( String _fileName ) { String _return = null; if ( ( _fileName != null ) && ( !_fileName.equals( "" ) ) ) { int point = _fileName.lastIndexOf( "." ); if ( point != -1 ) { _return = _fileName.substring( point + 1 ); } } return _return; }
上記のMap内pdfOptions.put("Magnification", new Integer(0));
に詳細な設定が出来るようですが、
"Magnification"を0に設定すると元のExcelファイルの設定で作成されるような仕様ですが、必ず100%で作成さ
れてしまい、1シートが2ページ(元々90%で設定されている)になってしまいます。
又、Excelの1Bookで複数シートあるものを全て連続で、PDF化したいのですが、
当該環境で実行するとなぜか1Bookで6シートあるものが、2シート分だけしかPDF化されません。
なぜ、このような現象になるのか、又、ここ間違っていないかなど、
皆様の貴重なご意見を頂戴出来ませんでしょうか。
大変お手数をお掛けして申し訳ありませんが、
どうぞ、宜しくお願い申し上げます。
【参考サイト】
リンク内容
以上、宜しくお願い申し上げます。
