合并或合并来自几个内存流的pdf

时间:2014-07-08 02:13:50

标签: c# pdf-generation itextsharp memorystream

我是c#和itextsharp的新手,并且在使用itextsharp合并2个或更多pdf时出现问题。我有2个方法,每个方法填充一个pdf模板,并将它们保存到内存流和downloadAsPDF方法,以将组合的pdf返回到broswer。 downloadAsPDF工作时,我只添加一个内存流,但当我将两个MemoryStream添加到列表中时,我收到消息...如果此消息最终没有被文档的正确内容替换,则PDF 查看器可能无法显示此类文档。任何帮助将非常感激。感谢

public byte[] fillTemplateA()
{
    String NfoodAllergyTemplate = "~/pdfTemplateA.pdf";

    //setting readers and stamper and memorystream
    PdfReader ReadPdf = new PdfReader(NfoodAllergyTemplate);
    MemoryStream msNfoodAllergyTemplate = new MemoryStream();
    PdfStamper StampPdf = new PdfStamper(ReadPdf, msNfoodAllergyTemplate,'\0',true);
    AcroFields NFoodAlF = StampPdf.AcroFields;

    NFoodAlF.SetField("stdFullNameNFAAP", testObject);

    StampPdf.FormFlattening = false;
    StampPdf.Close();
    ReadPdf.Close();


    byte[] ms1 = msNfoodAllergyTemplate.ToArray();
    return ms1;
}

这是我的第二个填充第二个pdf的方法

public byte[] fillTemplateB()
{
    String NfoodAllergyTemplate = "~/pdfTemplateB.pdf";


    //setting readers and stamper and memorystream
    PdfReader ReadPdf = new PdfReader(NfoodAllergyTemplate);
    MemoryStream msNfoodAllergyTemplate = new MemoryStream();
    PdfStamper StampPdf = new PdfStamper(ReadPdf, msNfoodAllergyTemplate,'\0',true);
    AcroFields NFoodAlF = StampPdf.AcroFields;

    NFoodAlF.SetField("stdFullNameNFAAP", testObject);

    StampPdf.FormFlattening = false;
    StampPdf.Close();
    ReadPdf.Close();


    byte[] ms2 = msNfoodAllergyTemplate.ToArray();
    return ms2;
}

合并pdf方法

public MemoryStream MergePDFs(List<byte[]> pdfFiles) 
{
    if(pdfFiles.Count >1)
    {
        PdfReader finalPdf;
        Document pdfContainer;
        PdfWriter pdfCopy;
        MemoryStream msFinalPdf = new MemoryStream();

        finalPdf = new PdfReader(pdfFiles[0]);
        pdfContainer = new Document();
        pdfCopy = new PdfSmartCopy(pdfContainer, msFinalPdf);

        pdfContainer.Open();

        for (int k = 0; k < pdfFiles.Count; k++)
        {
            finalPdf = new PdfReader(pdfFiles[k]);
            for (int i = 1; i < finalPdf.NumberOfPages + 1; i++)
            {
                ((PdfSmartCopy)pdfCopy).AddPage(pdfCopy.GetImportedPage(finalPdf, i));
            }
            pdfCopy.FreeReader(finalPdf);

        }
        finalPdf.Close();
        pdfCopy.Close();
        pdfContainer.Close();

        return msFinalPdf;
    }
    else if(pdfFiles.Count==1)
    {
        return new MemoryStream(pdfFiles[0]);
    }
    return null;
}
                       }

在最后一个方法中,我合并了来自FillTemplate A和B

的内存流中的pdf
public void finalPdf()
{
    //List to collect all pdfs in memorystream that have been assinged to a byte array

    System.Collections.Generic.List<byte[]> collectAllForms = new List<byte[]>();
    //when i add only one of the line below it works...having both give error message...Please wait...
    collectAllForms.Add(fillTemplateA());
    collectAllForms.Add(fillTemplateB());

    //Call the downloadAsPDF method 
    DownloadAsPDF(MergePDFs(collectAllForms), "QVSD_Packet");
}

这是downloadAsPDF方法

public void DownloadAsPDF(MemoryStream msFinal,String theFileName)
{
    theFileName = "attachment; filename =" + theFileName + ".pdf";

    HttpContext.Current.Response.Clear();
    HttpContext.Current.Response.ClearContent();
    HttpContext.Current.Response.ClearHeaders();
    HttpContext.Current.Response.AddHeader("Content-Disposition", theFileName);
    HttpContext.Current.Response.ContentType = "application/pdf";
    HttpContext.Current.Response.BinaryWrite(msFinal.ToArray());
    HttpContext.Current.Response.OutputStream.Flush();
    HttpContext.Current.Response.OutputStream.Close();
    HttpContext.Current.Response.End();
    msFinal.Close();
}

0 个答案:

没有答案