c# – iTextSharp从1页模板生成高效批量Pdf

我正在使用ITextSharp生成多页PDF,每个页面都有相同的模板.

问题是PDF按照模板的大小以Phisical Size增长.

我必须使用ACROFIELDS.

如何减小最终文件大小?

这是pdf处理程序的代码片段:

public void ProcessRequest(HttpContext context)
{
    Context = context;
    Response = context.Response;
    Request = context.Request;

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

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

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

    PdfCopyFields copy
        = new PdfCopyFields(Response.OutputStream);
    // add a document
    for (int i = 0; i < dataset.Tables["Model"].Rows.Count; i++)
    {
        copy.AddDocument(new PdfReader(renameFieldsIn(TemplatePath, i)));

        // add a document           
    }        
    copy.SetFullCompression();                   
    // Close the PdfCopyFields object        
    copy.Close();       
}

private byte[] renameFieldsIn(String datasheet, int i)

{
    MemoryStream baos = new MemoryStream();
    // Create the stamper
    PdfStamper stamper = new PdfStamper(new PdfReader(GetTemplateBytes()), baos);
    // Get the fields
    AcroFields form = stamper.AcroFields;
    // Loop over the fields
    List<String> keys = new List<String>(form.Fields.Keys);
    foreach (var key in keys) 
    {
        // rename the fields
        form.RenameField(key, String.Format("{0}_{1}", key, i));
    }
    stamper.FormFlattening = true;
    stamper.FreeTextFlattening = true;
    stamper.SetFullCompression();
    SetFieldsInternal(form, i);
    // close the stamper
    stamper.Close();
    return baos.ToArray();
}

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

最佳答案 我之前做过一些类似的事情,发现在组合所有页面之后,再次通过PDFStamper运行整个生成的文档会导致文件大小的明显压缩.

点赞