1.PDF模板替换导出
PDF编辑工具:Adobe Acrobat DC 模板打印代码:
public void PDFTemplet ( HttpServletRequest request, HttpServletResponse response,
@RequestParam Map map, String type) throws Exception {
String id = ( String) map. get ( "id" ) ;
String rootPath = request. getSession ( ) . getServletContext ( ) . getRealPath ( "/" ) ;
String tempFilePath = "" ;
if ( "Check" . equals ( type) ) {
tempFilePath= "WEB-INF/templates/templates/Check.pdf" ;
}
Map< String, String> paramData = this . getDatePdf ( map, type) ;
PdfReader reader = new PdfReader ( rootPath+ tempFilePath) ;
ByteArrayOutputStream bos = new ByteArrayOutputStream ( ) ;
PdfStamper pdfStamper = new PdfStamper ( reader, bos) ;
BaseFont bf = BaseFont. createFont ( "STSong-Light" , "UniGB-UCS2-H" , BaseFont. EMBEDDED) ;
AcroFields s = pdfStamper. getAcroFields ( ) ;
pdfStamper. setFormFlattening ( true ) ;
Iterator< String> it = s. getFields ( ) . keySet ( ) . iterator ( ) ;
String nameValue = "" ;
while ( it. hasNext ( ) ) {
String name = it. next ( ) . toString ( ) ;
nameValue = paramData. get ( name) ;
s. setFieldProperty ( name. toString ( ) , "textfont" , bf, null) ;
if ( "commonSuperviseNoticeInfo" . equals ( type) ) {
s. setFieldProperty ( name. toString ( ) , "textsize" , ( float ) 13 , null) ;
} else {
s. setFieldProperty ( name. toString ( ) , "textsize" , ( float ) 10 , null) ;
}
s. setField ( name, nameValue) ;
}
pdfStamper. close ( ) ;
OutputStream fos = response. getOutputStream ( ) ;
response. setContentType ( "application/pdf" ) ;
response. setContentLength ( bos. size ( ) ) ;
fos. write ( bos. toByteArray ( ) ) ;
fos. flush ( ) ;
fos. close ( ) ;
bos. close ( ) ;
}
PDF模板替换JAR包概览:
< ! -- pdf 相关jar包 -- >
< dependency>
< groupId> com. itextpdf< / groupId>
< artifactId> itextpdf< / artifactId>
< version> 5.5 .9 < / version>
< / dependency>
< dependency>
< groupId> com. itextpdf< / groupId>
< artifactId> itext- pdfa< / artifactId>
< version> 5.5 .9 < / version>
< / dependency>
< dependency>
< groupId> com. itextpdf< / groupId>
< artifactId> itext- xtra< / artifactId>
< version> 5.4 .4 < / version>
< / dependency>
< dependency>
< groupId> com. itextpdf. tool< / groupId>
< artifactId> xmlworker< / artifactId>
< version> 5.5 .9 < / version>
< / dependency>
< dependency>
< groupId> com. itextpdf< / groupId>
< artifactId> itext- asian< / artifactId>
< version> 5.2 .0 < / version>
< / dependency>
2.WORD模板替换导出
代码概览:
public void createCheckIdea ( HttpServletRequest request, HttpServletResponse response, @RequestParam String docId) {
String path= request. getSession ( ) . getServletContext ( ) . getRealPath ( "/template/意见书.doc" ) ;
Map< String, String> entities= new HashMap < String, String> ( ) ;
entities. put ( "替换名称" , 替换数据) ;
HWPFDocument doc= null;
try {
FileInputStream input= new FileInputStream ( new File ( path) ) ;
doc= new HWPFDocument ( input) ;
Range bodyRange= doc. getRange ( ) ;
for ( Map. Entry< String, String> entry: entities. entrySet ( ) ) {
String a= entry. getValue ( ) ;
bodyRange. replaceText ( "${" + entry. getKey ( ) + "}" , entry. getValue ( ) ) ;
}
SimpleDateFormat sdf= new SimpleDateFormat ( "yyyyMMddHHmmss" ) ;
docTitle+= sdf. format ( new Date ( ) ) . toString ( ) + ".doc" ;
response. setContentType ( "application/x-msdownloadoctet-stream;charset=utf-8" ) ;
response. setHeader ( "Content-Disposition" , "attachment;filename=\""
+ new String ( docTitle. getBytes ( "gb18030" ) , "ISO8859-1" ) + "\"" ) ;
doc. write ( response. getOutputStream ( ) ) ;
response. getOutputStream ( ) . flush ( ) ;
response. getOutputStream ( ) . close ( ) ;
} catch ( Exception e) {
e. printStackTrace ( ) ;
}
}
WORD主要运用了POI和一个阿里巴巴的JOSN包
< dependency>
< groupId> com. alibaba< / groupId>
< artifactId> fastjson< / artifactId>
< version> 1.2 .8 < / version>
< / dependency>
< dependency>
< groupId> org. apache. poi< / groupId>
< artifactId> poi- scratchpad< / artifactId>
< version> 3.9 < / version>
< / dependency>
< dependency>
< groupId> org. apache. poi< / groupId>
< artifactId> poi< / artifactId>
< version> 3.9 < / version>
< / dependency>