String classpath = this.getClass().getResource("/").getPath().replaceFirst("/", "");
String webappRoot = classpath.replaceAll("WEB-INF/classes/", "");

System.out.println("classpath:"+classpath);
System.out.println("webappRoot:"+webappRoot);

String temppath = webappRoot + "asset/pdftemplate/mb.pdf";
String NewPDFPath = webappRoot + "asset/pdf/cd.pdf";
String[] data = { "8", "8", "8", "8", "8", "1", "1", "1", "1", "1", "2", "2", "1", "1", "1", "1", "1", "1" };
System.out.println("temppath:"+temppath);
System.out.println("NewPDFPath:"+NewPDFPath);
new Snippet().fillTemplate(temppath, NewPDFPath, data);               new Snippet().fillTemplate(temppath, NewPDFPath, data);
上面是根据一个模版pdf生成一个新的pdf
classpath:E:/wbc_workspace/.metadata/.plugins/org.eclipse.wst.server.core/tmp1/wtpwebapps/regulatorysystem/WEB-INF/classes/
webappRoot:E:/wbc_workspace/.metadata/.plugins/org.eclipse.wst.server.core/tmp1/wtpwebapps/regulatorysystem/
temppath:E:/wbc_workspace/.metadata/.plugins/org.eclipse.wst.server.core/tmp1/wtpwebapps/regulatorysystem/asset/pdftemplate/mb.pdf
NewPDFPath:E:/wbc_workspace/.metadata/.plugins/org.eclipse.wst.server.core/tmp1/wtpwebapps/regulatorysystem/asset/pdf/cd.pdf
路径是这样的 生成的方法如下:
public void fillTemplate(String TemplatePath,String NewFilePath, String[] data) {
// 模板路径
String templatePath = TemplatePath;
// 生成的新文件路径
String newPDFPath = NewFilePath;
PdfReader reader;
FileOutputStream out;
ByteArrayOutputStream bos;
PdfStamper stamper;
// System.out.println(newPDFPath);
try {
out = new FileOutputStream(newPDFPath);// 输出流
reader = new PdfReader(templatePath);// 读取pdf模板
bos = new ByteArrayOutputStream();
stamper = new PdfStamper(reader, bos);
AcroFields form = stamper.getAcroFields();


int sum = reader.getNumberOfPages() + 1;

String[] str = data;
int i = 0;
java.util.Iterator<String> it = form.getFields().keySet().iterator();
while (it.hasNext()) {
String name = it.next().toString();
// System.out.println(name);
// System.out.println(i);
form.setField(name, str[i++]);
}
stamper.setFormFlattening(true);// 如果为false那么生成的PDF文件还能编辑,一定要设为true
stamper.close();
Document doc = new Document();
PdfCopy copy = new PdfCopy(doc, out);
// File fpdf = new File(newPDFPath);
// PdfToHtml.PdfToImage(newPDFPath);
doc.open();
for (int j = 1; j < sum; j++) {
PdfImportedPage importPage = copy.getImportedPage(new PdfReader(bos.toByteArray()), j);
copy.addPage(importPage);
}
doc.close();
} catch (IOException e) {
System.out.println(e);
} catch (DocumentException e) {
System.out.println(e);
}
}
前台jsp取值是这样的 <%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%><%@ page import="java.util.Map"%>
<%@ page import="java.io.*" %><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>打印页面</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script src="asset/js/jquery-3.2.1.min.js"></script>
<style type="text/css">
.pdf-wrapper{
position: absolute;
top:0;
left:0;
bottom:0;
right:0;
}
</style>
</head>
<body>
<div class="pdf-wrapper">
<iframe width="100%" height="100%" src="<%=request.getContextPath()%>/asset/pdf/cd.pdf"></iframe>
</div>
</body>
</html>
工程目录如下:
webapp
     asset
         pdf
            cs.pdf
         pdftemplate
            mb.pdf
问题:
1、如何让生成的cd.pdf覆盖webapp/asset/pdf/cd.pdf2、本地测试正常显示编辑后的cd.pdf。放在服务器上报错找不到cd.pdf???这是为啥???