如何保存已编辑的pdf?

时间:2011-06-02 19:53:09

标签: c# .net asp.net pdf itextsharp

我想保存已编辑的pdf模板。我该怎么做?

using (MemoryStream ms = new MemoryStream())  
{    
    PdfReader reader = new PdfReader("~/Content/Documents/Agreement.pdf");
    PdfStamper formFiller = new PdfStamper(reader, ms);
    AcroFields formFields = formFiller.AcroFields;
    formFields.SetField("Name", formData.Name);
    formFields.SetField("Location", formData.Address);
    formFields.SetField("Date", DateTime.Today.ToShortDateString());
    formFields.SetField("Email", formData.Email);
    formFiller.FormFlattening = true;
    formFiller.Close();
}

2 个答案:

答案 0 :(得分:3)

this example一样,您只需要使用FileStream而不是MemoryStream。

代码应如下所示:

string newFile = @".\FormDocument_out.pdf";    
FileStream fs = new FileStream(newFile, FileMode.Create);    
try
{
        PdfReader reader = new PdfReader("~/Content/Documents/Agreement.pdf");
        PdfStamper formFiller = new PdfStamper(reader, fs);  
        AcroFields formFields = formFiller.AcroFields;
        formFields.SetField("Name", formData.Name);
        formFields.SetField("Location", formData.Address);
        formFields.SetField("Date", DateTime.Today.ToShortDateString());
        formFields.SetField("Email", formData.Email);
        formFiller.FormFlattening = true;
        formFiller.Close();
}
catch(Exception)
{
    throw;
}
finally
{
    fs.Close();
}

答案 1 :(得分:0)

以下是用于下载PDF的模板类。

public abstract class PdfTemplateHandler : IHttpHandler
{
    public virtual bool DownloadAsAttachment
    {
        get
        {
            return true;
        }
    }

    protected virtual TimeSpan PdfTemplateCacheDuration
    {
        get
        {
            return TimeSpan.FromMinutes(30);
        }
    }

    protected virtual string PdfTemplateCacheKey
    {
        get
        {
            return string.Format("__PDF_Template[{0}]", TemplatePath);
        }
    }

    protected string DownloadFileName { get; set; }

    protected HttpContext Context { get; private set; }

    protected HttpResponse Response { get; private set; }

    protected HttpRequest Request { get; private set; }

    #region IHttpHandler Members

    public bool IsReusable
    {
        get { return false; }
    }

    public void ProcessRequest(HttpContext context)
    {

        Context = context;
        Response = context.Response;
        Request = context.Request;

        try
        {
            LoadDataInternal();
        }
        catch (ThreadAbortException)
        {
            // no-op
        }
        catch (Exception ex) {            
            Logger.LogError(ex);
            Response.Write("Error");
            Response.End();
        }

        Response.BufferOutput = true;
        Response.ClearHeaders();
        Response.ContentType = "application/pdf";

        if (DownloadAsAttachment)
        {
            Response.AddHeader("Content-Disposition", "attachment; filename=" +
                (string.IsNullOrEmpty(DownloadFileName) ? context.Session.SessionID + ".pdf" : DownloadFileName));
        }

        PdfStamper pst = null;
        try
        {
            PdfReader reader = new PdfReader(GetTemplateBytes());
            pst = new PdfStamper(reader, Response.OutputStream);
            var acroFields = pst.AcroFields;

            pst.FormFlattening = true;
            pst.FreeTextFlattening = true;
            pst.SetFullCompression();

            SetFieldsInternal(acroFields);
            pst.Close();
        }
        finally
        {
            if (pst != null)
                pst.Close();
        }
    }

    #endregion

    #region Abstract Members for overriding and providing functionality

    protected abstract string TemplatePath { get; }

    protected abstract void LoadDataInternal();

    protected abstract void SetFieldsInternal(AcroFields acroFields);

    #endregion

    protected virtual byte[] GetTemplateBytes()
    {
        var data = Context.Cache[PdfTemplateCacheKey] as byte[];
        if (data == null)
        {
            data = File.ReadAllBytes(Context.Server.MapPath(TemplatePath));
            Context.Cache.Insert(PdfTemplateCacheKey, data,
                null, DateTime.Now.Add(PdfTemplateCacheDuration), Cache.NoSlidingExpiration);
        }
        return data;
    }

}