@ -33,14 +33,16 @@ public final class PDFConverUtil {
final private static Map < TypeEnum , List < String > > types = new HashMap < > ( ) ;
public static enum TypeEnum {
WORD , EXCEL , PPT , PPTX
WORD , EXCEL , PPT , PPTX , OTHER
}
static {
types . put ( WORD , Arrays . asList ( "DOC" , "DOCX" , "OOXML" , "RTF HTML" , "OpenDocument" , "PDF" , "EPUB" , "XPS" , "SWF" )) ;
types . put ( WORD , Arrays . asList ( "DOC" , "DOCX" )) ;
types . put ( EXCEL , Arrays . asList ( "XLS" , "XLSX" ) ) ;
types . put ( PPT , Arrays . asList ( "PPT" ) ) ;
types . put ( PPTX , Arrays . asList ( "PPTX" ) ) ;
types . put ( OTHER , Arrays . asList ( "OOXML" , "RTF HTML" , "OpenDocument" , "PDF" , "EPUB" , "XPS" , "SWF" ) ) ;
}
/ * *
@ -95,13 +97,8 @@ public final class PDFConverUtil {
e . printStackTrace ( ) ;
return false ;
} finally {
try {
if ( document ! = null ) {
document . close ( ) ;
}
} catch ( Exception e ) {
e . printStackTrace ( ) ;
}
closeOutput ( outputStream ) ;
closeInput ( inputStream ) ;
}
return true ;
@ -129,18 +126,50 @@ public final class PDFConverUtil {
e . printStackTrace ( ) ;
return false ;
} finally {
if ( outputStream ! = null ) {
try {
outputStream . flush ( ) ;
outputStream . close ( ) ;
} catch ( IOException e ) {
e . printStackTrace ( ) ;
}
}
closeOutput ( outputStream ) ;
closeInput ( inputStream ) ;
}
return true ;
}
/ * *
* 关 闭 输 出 流
* @param outputStream
* /
private static void closeOutput ( OutputStream outputStream ) {
if ( outputStream ! = null ) {
try {
outputStream . flush ( ) ;
outputStream . close ( ) ;
} catch ( IOException e ) {
e . printStackTrace ( ) ;
}
}
}
/ * *
* 关 闭 输 出 流
* @param inputStream
* /
private static void closeInput ( InputStream inputStream ) {
if ( inputStream ! = null ) {
try {
inputStream . close ( ) ;
} catch ( IOException e ) {
e . printStackTrace ( ) ;
}
}
}
private static void close ( Document document , PdfWriter pdfWriter ) {
if ( document ! = null ) {
document . close ( ) ;
}
if ( pdfWriter ! = null ) {
pdfWriter . close ( ) ;
}
}
// 官方文档的要求 无需理会
public static boolean getLicense ( ) {
boolean result = false ;
@ -173,7 +202,7 @@ public final class PDFConverUtil {
* * /
public static boolean excelToPdf ( InputStream inputStream , OutputStream outputStream ) {
// 验证License 若不验证则转化出的pdf文档会有水印产生
if ( ! getEx e clLicense( ) ) {
if ( ! getEx ce lLicense( ) ) {
return false ;
}
try {
@ -183,7 +212,6 @@ public final class PDFConverUtil {
com . aspose . cells . PdfSaveOptions pdfSaveOptions = new com . aspose . cells . PdfSaveOptions ( ) ;
pdfSaveOptions . setOnePagePerSheet ( false ) ;
int [ ] autoDrawSheets = { 3 } ;
//当excel中对应的sheet页宽度太大时, 在PDF中会拆断并分页。此处等比缩放。
autoDraw ( wb , autoDrawSheets ) ;
@ -192,13 +220,14 @@ public final class PDFConverUtil {
//隐藏workbook中不需要的sheet页。
printSheetPage ( wb , showSheets ) ;
wb . save ( outputStream , pdfSaveOptions ) ;
outputStream . flush ( ) ;
outputStream . close ( ) ;
System . out . println ( "excel转换完毕" ) ;
} catch ( IOException e ) {
e . printStackTrace ( ) ;
} catch ( Exception e ) {
e . printStackTrace ( ) ;
} finally {
closeOutput ( outputStream ) ;
closeInput ( inputStream ) ;
}
return true ;
}
@ -237,10 +266,21 @@ public final class PDFConverUtil {
}
}
public static boolean getEx e clLicense( ) {
public static boolean getEx ce lLicense( ) {
boolean result = false ;
try {
String s = "<License><Data><Products><Product>Aspose.Total for Java</Product><Product>Aspose.Words for Java</Product></Products><EditionType>Enterprise</EditionType><SubscriptionExpiry>20991231</SubscriptionExpiry><LicenseExpiry>20991231</LicenseExpiry><SerialNumber>8bfe198c-7f0c-4ef8-8ff0-acc3237bf0d7</SerialNumber></Data><Signature>sNLLKGMUdF0r8O1kKilWAGdgfs2BvJb/2Xp8p5iuDVfZXmhppo+d0Ran1P9TKdjV4ABwAgKXxJ3jcQTqE/2IRfqwnPf8itN8aFZlV3TJPYeD3yWE7IT55Gz6EijUpC7aKeoohTb4w2fpox58wWoF3SNp6sK6jDfiAUGEHYJ9pjU=</Signature></License>" ;
String s = "<License>\n" +
" <Data>\n" +
" <Products>\n" +
" <Product>Aspose.Cells for Java</Product>\n" +
" </Products>\n" +
" <EditionType>Enterprise</EditionType>\n" +
" <SubscriptionExpiry>29991231</SubscriptionExpiry>\n" +
" <LicenseExpiry>29991231</LicenseExpiry>\n" +
" <SerialNumber>evilrule</SerialNumber>\n" +
" </Data>\n" +
" <Signature>evilrule</Signature>\n" +
"</License>" ;
ByteArrayInputStream is = new ByteArrayInputStream ( s . getBytes ( ) ) ;
com . aspose . cells . License aposeLic = new com . aspose . cells . License ( ) ;
aposeLic . setLicense ( is ) ;
@ -284,11 +324,13 @@ public final class PDFConverUtil {
HSLFSlide hslfSlide = hslfSlideList . get ( i ) ;
// 设置字体, 解决中文乱码
for ( HSLFShape shape : hslfSlide . getShapes ( ) ) {
HSLFTextShape textShape = ( HSLFTextShape ) shape ;
if ( shape instanceof HSLFTextShape ) {
HSLFTextShape textShape = ( HSLFTextShape ) shape ;
for ( HSLFTextParagraph textParagraph : textShape . getTextParagraphs ( ) ) {
for ( HSLFTextRun textRun : textParagraph . getTextRuns ( ) ) {
textRun . setFontFamily ( "宋体" ) ;
for ( HSLFTextParagraph textParagraph : textShape . getTextParagraphs ( ) ) {
for ( HSLFTextRun textRun : textParagraph . getTextRuns ( ) ) {
textRun . setFontFamily ( "宋体" ) ;
}
}
}
}
@ -315,12 +357,9 @@ public final class PDFConverUtil {
e . printStackTrace ( ) ;
return false ;
} finally {
if ( document ! = null ) {
document . close ( ) ;
}
if ( pdfWriter ! = null ) {
pdfWriter . close ( ) ;
}
close ( document , pdfWriter ) ;
closeOutput ( outputStream ) ;
closeInput ( inputStream ) ;
}
System . out . println ( "ppt转换完毕" ) ;
return true ;
@ -362,11 +401,13 @@ public final class PDFConverUtil {
// 设置字体, 解决中文乱码
for ( XSLFShape shape : slide . getShapes ( ) ) {
XSLFTextShape textShape = ( XSLFTextShape ) shape ;
if ( shape instanceof XSLFTextShape ) {
XSLFTextShape textShape = ( XSLFTextShape ) shape ;
for ( XSLFTextParagraph textParagraph : textShape . getTextParagraphs ( ) ) {
for ( XSLFTextRun textRun : textParagraph . getTextRuns ( ) ) {
textRun . setFontFamily ( "宋体" ) ;
for ( XSLFTextParagraph textParagraph : textShape . getTextParagraphs ( ) ) {
for ( XSLFTextRun textRun : textParagraph . getTextRuns ( ) ) {
textRun . setFontFamily ( "宋体" ) ;
}
}
}
}
@ -393,12 +434,9 @@ public final class PDFConverUtil {
e . printStackTrace ( ) ;
return false ;
} finally {
if ( document ! = null ) {
document . close ( ) ;
}
if ( pdfWriter ! = null ) {
pdfWriter . close ( ) ;
}
close ( document , pdfWriter ) ;
closeOutput ( outputStream ) ;
closeInput ( inputStream ) ;
}
System . out . println ( "pptx转换完毕" ) ;
return true ;
@ -419,14 +457,17 @@ public final class PDFConverUtil {
if ( types . get ( WORD ) . contains ( fileSuffix ) ) {
return wordToPdfByAspose ( inputStream , outputStream ) ;
}
if ( types . get ( EXCEL ) . contains ( fileSuffix ) ) {
return excelToPdf ( inputStream , outputStream ) ;
}
if ( types . get ( PPT ) . contains ( fileSuffix ) ) {
return pptToPdf ( inputStream , outputStream ) ;
}
if ( types . get ( PPTX ) . contains ( fileSuffix ) ) {
return pptxToPdf ( inputStream , outputStream ) ;
}
if ( types . get ( EXCEL ) . contains ( fileSuffix ) ) {
return excelToPdf ( inputStream , outputStream ) ;
if ( types . get ( OTHER ) . contains ( fileSuffix ) ) {
return wordToPdfByAspose ( inputStream , outputStream ) ;
}
return false ;
}