如何使用iText库中的PdfStamper使用临时空间作为"Destination"?



我正在从Domino Server浏览网络用户的pdf文件。我的 Java 包上有一个模板.pdf和一个字体文件来生成这些 pdf 文件,而无需将它们保存在服务器上。但是,PdfStamper要求我使用需要路径的OutputStream。

多米诺服务器有临时空间吗?有没有其他方法来实现这一点?有没有要设置的假路径?

作为现在的测试,我将其保存在本地计算机上。


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);
    }
}

您不需要临时文件路径。您可以使用响应的输出流,从而将 PDF 直接发送到浏览器。

例:

XspHttpServletResponse response = (XspHttpServletResponse) JSFUtil.getFacesContext().getExternalContext().getResponse();
ServletOutputStream out = response.getOutputStream();
PdfStamper stamper = new PdfStamper(reader, out); 

最新更新