显示pdf而不保存?

时间:2016-11-18 04:28:58

标签: c# asp.net pdf

所以我试图调整此功能,而不是使用新字段值创建新的PDF文件,然后向用户显示新文件,然后在退出时删除。 只使用新字段值创建此模板的流或字节数组,并将其显示给用户..是否可能?

private void fillPDFForm()
{
    string formFile = @"C:\fw4.pdf";
    string newFile = @"C:\New_fw4.pdf";

    PdfReader reader = new PdfReader(formFile);

    using (PdfStamper stamper = new PdfStamper(reader, new FileStream(newFile, FileMode.Create)))
    {
        AcroFields fields = stamper.AcroFields;

        // set form fields
        fields.SetField("name", "John Doe");
        fields.SetField("address", "xxxxx, yyyy");
        fields.SetField("postal_code", "12345");
        fields.SetField("email", "johndoe@xxx.com");

        // flatten form fields and close document
        stamper.FormFlattening = true;
        stamper.Close();
    }
}

2 个答案:

答案 0 :(得分:0)

是的当然可以使用MemoryStream代替FileStream,因此您的代码可能如下所示:

using (MemoryStream memStream = new MemoryStream()) 
{
    using (PdfStamper stamper = new PdfStamper(reader, memStream, '\0', true))
    {
        AcroFields fields = stamper.AcroFields;

        // set form fields
        fields.SetField("name", "John Doe");
        fields.SetField("address", "xxxxx, yyyy");
        fields.SetField("postal_code", "12345");
        fields.SetField("email", "johndoe@xxx.com");

        // flatten form fields and close document
        stamper.FormFlattening = true;
        stamper.Close();
    }
    return memStream.ToArray();
}

答案 1 :(得分:0)

您是否尝试将响应的内容类型设置为application / pdf?

using (PdfStamper stamper = new PdfStamper(reader, new FileStream(newFile, FileMode.Create)))

using (PdfStamper stamper = new PdfStamper(reader, new FileStream(newFile, FileMode.Create), "application/pdf"))
using (PdfStamper stamper = new PdfStamper(reader, new FileStream(newFile, FileMode.Create)))

    private void fillPDFForm()
    {
        string formFile = @"C:\fw4.pdf";
        string newFile = @"C:\New_fw4.pdf";

        PdfReader reader = new PdfReader(formFile);

        using (PdfStamper stamper = new PdfStamper(reader, new FileStream(newFile, FileMode.Create), "application/pdf"))
        {
            AcroFields fields = stamper.AcroFields;

            // set form fields
            fields.SetField("name", "John Doe");
            fields.SetField("address", "xxxxx, yyyy");
            fields.SetField("postal_code", "12345");
            fields.SetField("email", "johndoe@xxx.com");

            // flatten form fields and close document
            stamper.FormFlattening = true;
            stamper.Close();
        }
    }