这是我写作和PDF的助手类---
public class PDFMerge {
private MergeDocument document = null;
public PDFMerge(File template) throws Exception {
if (templatePdfFile == null) {
throw new Exception( );
document = new MergeDocument(template.getAbsolutePath(), new MergeOptions(true));
}
}
public MergeDocument mergePDF(String pdfformid, String url,
) {
Calendar cal = Calendar.getInstance();
if (document != null) {
//hidden fields on PDF form
if (document.getForm().getFields().getFormField("url_to_submit") != null) {
document.getForm().getFields().getFormField("url_to_submit")
.setValue(url);
}
//Date fields to prepopulate before rendered on browser
if (document.getForm().getFields().getFormField("date.mm") != null) {
document
.getForm()
.getFields()
.getFormField("date.mm")
.setValue(
String.valueOf(cal.get(Calendar.MONTH) + 1));
}
if (document.getForm().getFields().getFormField("date.dd") != null) {
.....
}
if (document.getForm().getFields().getFormField("date.yyyy") != null) {
....
}
}
}
//document is drawn into byte array in servlet to send to output stream.
return document;
}
}
在 servlet/caller(struts 动作)方面----
PDFMerge pdfmerge = new PDFMerge(form.getTemplateFile());
MergeDocument mergedPDF = printTool.mergePDF(String
.valueOf(form.getFormId()), URL);
byte[] pdfArray = mergedPDF.draw();
这个字节数组被转发到 JSP 上进行渲染。它没有正确显示按钮。