我正在从Domino Server浏览网络用户pdf文件。我在我的Java包上有一个template.pdf和一个字体文件来生成这些pdf文件而不在服务器上保存它们。但是PdfStamper要求我使用需要路径的OutputStream。
Domino Server是否有临时空间?有没有不同的方法来实现这个?有没有设置的假路径?
现在作为测试我将它保存在我的本地机器上。
package de.vogella.itext.write;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.pdf.AcroFields;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfStamper;
public class FillUpPDF {
// Get the files from jar/package
InputStream streamTemplate = getClass().getResourceAsStream("Template1.pdf");
InputStream streamFont = getClass().getResourceAsStream("Ruritania.ttf");
// Files
public static final String sTemplate = "Template1.pdf";
public static final String sResultPDF = "C:/Test/Bob Info.pdf";
public static final String sResultFont = "Ruritania.ttf";
// Manipulates a PDF file source with the file destination as result
public void manipulatePdf(String src, String dest) throws IOException, DocumentException {
// Read the Template
PdfReader reader = new PdfReader(src);
// Copy the template and output it to the destination
PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest)); //Where to fileoutputstream on Domino Server temporarily
// Gets the fields on the form
AcroFields form = stamper.getAcroFields();
// Create and set the font
BaseFont newFont = BaseFont.createFont(sResultFont, BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
form.addSubstitutionFont(newFont);
/* Filling up the fields on the form */
form.setFieldProperty("text_1", "textfont", newFont, null);
form.setFieldProperty("text_1", "textsize", new Float(9), null);
form.setField("text_1", "Bob");
form.setFieldProperty("text_2", "textfont", newFont, null);
form.setFieldProperty("text_2", "textsize", new Float(9), null);
form.setField("text_2", "Gates");
form.setFieldProperty("text_3", "textfont", newFont, null);
form.setFieldProperty("text_3", "textsize", new Float(9), null);
form.setField("text_3", "Oct 17, 2013 at 11:00am");
form.setFieldProperty("text_4", "textfont", newFont, null);
form.setFieldProperty("text_4", "textsize", new Float(9), null);
form.setField("text_4", "Cloud and Smarter Infrastructure");
// Fixed the field
stamper.setFormFlattening(true);
stamper.setFreeTextFlattening(true);
// Close
stamper.close();
reader.close();
// Next Step: Send out the created pdf file to the user.
}
// Main method
public void main(String[] args) throws Exception {
FillUpPDF certification = new FillUpPDF();
//certification.manipulatePdf(sTemplate, sResultPDF);
certification.manipulatePdf(sTemplate, sResultPDF);
}
}
答案 0 :(得分:2)
您不需要临时文件路径。您可以使用响应中的输出流,从而将PDF直接发送到浏览器。
示例:
XspHttpServletResponse response = (XspHttpServletResponse) JSFUtil.getFacesContext().getExternalContext().getResponse();
ServletOutputStream out = response.getOutputStream();
PdfStamper stamper = new PdfStamper(reader, out);