0

我正在浏览来自 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);
    }
}
4

1 回答 1

2

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

例子:

XspHttpServletResponse response = (XspHttpServletResponse) JSFUtil.getFacesContext().getExternalContext().getResponse();
ServletOutputStream out = response.getOutputStream();
PdfStamper stamper = new PdfStamper(reader, out); 
于 2013-10-21T15:35:41.457 回答